d1cor

sock_inet_stream_srv_fork.c

Oct 20th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. /*******************************************
  2. * Author     : Diego Cordoba / @d1cor      *
  3. * Purpose    : JuncoTIC / UM               *
  4. * Contact    : juncotic.com                *
  5. *******************************************/
  6.  
  7. /*
  8.     Uso:
  9.         ./servidor <puerto>
  10. */
  11.  
  12.  
  13. #include<stdio.h>
  14. #include<stdlib.h>
  15. #include<sys/types.h>
  16. #include<sys/stat.h>
  17. #include<unistd.h>
  18. #include<string.h>
  19. #include<fcntl.h>
  20. #include<errno.h>
  21. #include<sys/socket.h>
  22. #include<netinet/in.h>
  23.  
  24. #define BUF_SIZE 1024
  25.  
  26. int main(int argc, char** argv) {
  27.  
  28.     char *error;
  29.  
  30.     int sockid, conn_sock, count;
  31.     struct sockaddr_in direccion;
  32.     char buffer[BUF_SIZE];
  33.     int pid;
  34.  
  35.  
  36.     //creamos el socket inet-stream
  37.     if((sockid=socket(PF_INET,SOCK_STREAM,0))<0){
  38.         error="socket";
  39.         goto err;
  40.     }
  41.  
  42.  
  43.     //seteamos la direccion en la que va a escuchar
  44.     direccion.sin_family=AF_INET; //address family
  45.     direccion.sin_port=htons(atoi(*(argv+1)));  //atoi ascii to integer
  46.     direccion.sin_addr.s_addr=htonl(INADDR_ANY); //0.0.0.0
  47.  
  48.  
  49.     //asociamos el socket con la direccion (bind)
  50.     if((bind(sockid, (struct sockaddr *)&direccion, sizeof(direccion)))<0){
  51.         error="bind";
  52.         goto err;
  53.     }
  54.  
  55.  
  56.     // seteamos la cantidad de conexiones concurrentes en cola
  57.     listen(sockid,1);
  58.  
  59. /*
  60.        #include <sys/types.h>
  61.        #include <sys/socket.h>
  62.  
  63.        int listen(int sockfd, int backlog);
  64.             sockfd: socket ID
  65.             backlog: conex concurrentes en cola
  66.  
  67. */
  68.  
  69.     //dejamos escuchando al proceso en el socket ip:puerto
  70.     while(conn_sock=accept(sockid,NULL,NULL)){
  71.         if (conn_sock<0){
  72.             error="accept";
  73.             goto err;
  74.         }
  75.  
  76.         if (!(pid=fork())){     //proceso hijo
  77.             while(count=recv(conn_sock,buffer,BUF_SIZE,0)){
  78.                 if (count < 0){
  79.                     error="recv";
  80.                     goto err;
  81.                 }
  82.    
  83.                 *(buffer+count)='\0';
  84.                 printf("Recibiendo datos: %s\n",buffer);
  85.             }
  86.             close(sockid);
  87.             exit(0);
  88.         }
  89.  
  90.         printf("Conexion delegada al proceso hijo\n");
  91. //      waitpid(pid,NULL,WNOWAIT);
  92.     }
  93.  
  94.  
  95.  
  96.     // ya tenemos un conn_sock de datos asociado con el cliente conectado
  97.     return 0;
  98.  
  99. err:
  100.     fprintf(stderr,"%d %s %s\n",errno,error,strerror(errno));
  101.     exit(1);
  102.  
  103. }
  104.  
  105. /*
  106.  
  107. struct sockaddr_in {
  108.     short            sin_family;   // e.g. AF_INET
  109.     unsigned short   sin_port;     // e.g. htons(3490)
  110.     struct in_addr   sin_addr;     // see struct in_addr, below
  111.     char             sin_zero[8];  // zero this if you want to
  112. };
  113.  
  114. struct in_addr {
  115.     unsigned long s_addr;  // load with inet_aton()
  116. };
  117.  
  118.  
  119.  
  120. tcpip conexiones tcp:
  121.     client  server
  122.         > syn       \
  123.         < ack +syn  |   establecimiento de la conexion -> conn_sock
  124.         > ack       /
  125.         ...
  126.         trafico cliente-servidor usando el conn_sock
  127.         ...
  128.         < fin       \
  129.         > ack + fin |   fin de la conexion (circuito virtual tcp)
  130.         < ack       /
  131.  
  132.  
  133.  
  134. */
Add Comment
Please, Sign In to add comment