Advertisement
Gustavo_Inzunza

cliente.c

Dec 5th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <pthread.h>
  10. #include <string.h>
  11.  
  12. #define SERVER_PORT     4012
  13. #define SERVER_ADDRESS  "127.0.0.1"
  14. #define MAXLINE         512
  15.  
  16. void* recibir(void* p);
  17. void* enviar(void* p);
  18. int flag=0;
  19.  
  20. int main()
  21. {
  22.         struct sockaddr_in addr;
  23.         int sd,status;
  24.         pthread_t hilos[2];
  25.  
  26.         addr.sin_family = AF_INET;
  27.         addr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
  28.         addr.sin_port = htons(SERVER_PORT);
  29.  
  30.         if((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
  31.         {
  32.                 printf("Error al crear el socket\n");
  33.                 exit(0);
  34.         }
  35.         if(connect(sd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
  36.         {
  37.                 printf("Error al conectar\n");
  38.                 exit(0);
  39.         }
  40.         else
  41.         {
  42.                 if ( (status = pthread_create(&hilos[0],NULL,recibir,(void*)&sd)) )                             //La función pthread_create() devuelve 0 si todo ha ido bien. Un valor distinto de 0 si ha habido algún problema y no se ha creado el thread.
  43.                 {
  44.                         printf("Error al crear hilo para recibir\n");
  45.                         close(sd);
  46.                         exit(0);
  47.                 }
  48.                /*
  49.                 if ( (status = pthread_create(&hilos[1],NULL,enviar,(void*)&sd)) )
  50.                 {
  51.                         printf("Error al crear hilo para enviar\n");
  52.                         close(sd);
  53.                         exit(0);
  54.                 }*/
  55.                 pthread_join(hilos[0],NULL);
  56.                 //pthread_join(hilos[1],NULL);
  57.         }
  58.         return 1;
  59. }
  60.  
  61. void* recibir(void* p)
  62. {
  63.         int* id;
  64.         char buffer[MAXLINE];
  65.         id = (int*) p;
  66.         send(*id,"me conecte",MAXLINE,0);
  67.         while(1)
  68.         {
  69.                 recv(*id,buffer,MAXLINE,0);
  70.                
  71.                         printf("\n%s\n",buffer);
  72.                         memset(buffer,'\0',sizeof(buffer));
  73.                
  74.                
  75.                         printf("\nTexto: ");
  76.                         gets(buffer);
  77.                         send(*id,buffer,MAXLINE,0);
  78.                         memset(buffer,'\0',sizeof(buffer));      
  79.                
  80.         }
  81. }
  82.  
  83. void* enviar(void* p)
  84. {
  85.         int* id;
  86.         char buffer2[MAXLINE];
  87.         id = (int*) p;
  88.         while(1)
  89.         {
  90.                 printf("\nTexto: ");
  91.                 gets(buffer2);
  92.                 send(*id,buffer2,MAXLINE,0);
  93.                  memset(buffer2,'\0',sizeof(buffer2));
  94.         }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement