Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: d00ze13 en 28 Abril 2011, 18:38 pm



Título: socket cliente y servidor c++ / linux
Publicado por: d00ze13 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 ; )