elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  socket cliente y servidor c++ / linux
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: socket cliente y servidor c++ / linux  (Leído 4,710 veces)
d00ze13

Desconectado Desconectado

Mensajes: 78


Ver Perfil
socket cliente y servidor c++ / linux
« en: 28 Abril 2011, 18:38 pm »

Necesitaba ayuda a ver si alguien que supiera sobre sockets en c me pudiera hechar una mano.
Tengo un cliente y  un servidor hecho, me compilan bien pero luego no me establece la conexion.
SERVER
Código
  1. #include <netinet/in.h>
  2. #include <sys/socket.h>
  3. #include <sys/types.h>
  4. #include <iostream>
  5. using namespace std;
  6. int main(){
  7.  
  8. int sockfd, new_sock;
  9. sockfd=socket(AF_INET,SOCK_STREAM,0);
  10.        struct sockaddr_in sin={AF_INET,htons(900),INADDR_ANY};
  11.        char text;
  12.  
  13.        bind(sockfd,(struct sockaddr *) &sin,sizeof(sin));
  14.        listen(sockfd,5);
  15.        new_sock=accept(sockfd,NULL,NULL);
  16.        close(sockfd);
  17.  
  18.        while(read(new_sock,&text,1))
  19. write(1,&text,1);
  20.        cout<<endl;
  21. }
  22.  
CLIENTE
Código
  1. #include <netinet/in.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <iostream>
  8. using namespace std;
  9. void uso(char *prog){
  10.  
  11. cout<<"Uso:\n";
  12.        cout<<"\t"<<prog<<" <ip dest> [port dest] [msg]\n\n";
  13.        exit(0);
  14. }
  15. int main(int argc,char **argv){
  16.  
  17. int sockfd;
  18. sockfd=socket(AF_INET,SOCK_STREAM,0);
  19.        struct sockaddr_in sin={AF_INET,htons(900),INADDR_ANY};
  20.        char *texto;
  21. texto=new char[50];
  22.  
  23.        if(argc<2) uso(argv[0]);
  24.        if(argc>2) sin.sin_port=htons(atoi(argv[2]));
  25.        if(argc>3) strcpy(texto,argv[3]);
  26.        sin.sin_addr.s_addr=inet_addr(argv[1]);
  27.  
  28.        if (connect(sockfd,(struct sockaddr *) &sin,sizeof(sin))==-1){
  29.                perror("connect()");
  30.                exit(0);
  31.        }
  32.  
  33.        send(sockfd,texto,strlen(texto),0);
  34. cout<<texto<<endl;
  35.  
  36.        close(sockfd);
  37.        cout<<endl;
  38. }
  39.  

Lo siento por el tocho!!
1 saludo ; )


« Última modificación: 28 Abril 2011, 18:42 pm por d00ze13 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
cliente/servidor socket « 1 2 »
Programación Visual Basic
mig5ueles 11 4,717 Último mensaje 22 Enero 2007, 20:47 pm
por mig5ueles
Socket / cliente servidor
Java
danielo- 4 14,212 Último mensaje 2 Octubre 2009, 23:19 pm
por Debci
Socket C++ cliente/servidor con ip de internet: no me va
Programación C/C++
ellentejilla 1 3,789 Último mensaje 27 Octubre 2010, 21:22 pm
por Horricreu
cliente del socket (ayuda)
Java
andaluz 2 2,754 Último mensaje 3 Abril 2011, 11:33 am
por andaluz
Socket java Servidor Cliente
Java
momo1234 2 4,181 Último mensaje 12 Mayo 2012, 16:57 pm
por momo1234
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines