Untitled

By celio, 2 Weeks ago, written in c.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<arpa/inet.h>
  6. #include<sys/types.h>
  7. #include<netinet/in.h>
  8. #include<sys/socket.h>
  9.  
  10. #define porta 6666
  11. #define tmb   1024
  12.  
  13. void sigchld_handler(int s)
  14. {
  15.     while(wait(NULL) &gt; 0);
  16. }
  17.  
  18. int main(void)
  19. {
  20.     int sockfd, nova_fd;                             //escutar em fd, novas conexões em new_fd
  21.     struct sockaddr_in meu_end;                      //minha informação de endereço
  22.     struct sockaddr_in outro_end;                    //informação de endereço externo
  23.     int sin_size;
  24.     struct sigaction sa;
  25.     int yes=1;
  26.  
  27.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  28.         {
  29.                 perror("socket");
  30.                 exit(1);
  31.         }
  32.  
  33.     if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
  34.         {
  35.                 perror("setsockopt");
  36.                 exit(1);
  37.         }
  38.  
  39.     meu_end.sin_family = AF_INET;                    // host byte order
  40.     meu_end.sin_port = htons( porta );               // converte para short, network byte order
  41.     meu_end.sin_addr.s_addr = INADDR_ANY;            // automaticamente preenche meu IP
  42.     bzero(&(meu_end.sin_zero), 8);                   // zera o resto da estrutura
  43.  
  44.     if (bind(sockfd,(struct sockaddr *)&meu_end,sizeof(struct sockaddr)) == -1)
  45.         {
  46.                 perror("bind");
  47.                 exit(1);
  48.         }
  49.  
  50.     if (listen(sockfd, 10 ) == -1)
  51.         {
  52.                 perror("listen");
  53.                 exit(1);
  54.         }
  55.  
  56.     sa.sa_handler = sigchld_handler;                // retira todos os processos ociosos
  57.     sigemptyset(&sa.sa_mask);
  58.     sa.sa_flags = SA_RESTART;
  59.  
  60.     if (sigaction(SIGCHLD, &sa, NULL) == -1)
  61.         {
  62.                 perror("sigaction");
  63.                 exit(1);
  64.         }
  65.  
  66.     while(1)                                        // loop principal accept()
  67.         {  
  68.                 sin_size = sizeof(struct sockaddr_in);
  69.                 if ((nova_fd = accept(sockfd,(struct sockaddr *)&outro_end, &sin_size))== -1)
  70.                         {
  71.  
  72.                                 perror("accept");
  73.                                 continue;
  74.                         }
  75.  
  76.                 printf("[Servidor]A conexao com o endereco %s foi estabelecida \n",inet_ntoa(outro_end.sin_addr));
  77.                 if (!fork())                        // este é o processo-filho
  78.                         {    
  79.                                 close(sockfd);      // o filho não precisa de escutar conexões
  80.                                 if (send(nova_fd, "Olá , Beleza!\n", 14, 0) == -1)
  81.                                         {
  82.                                                 perror("send");
  83.                                                 close(nova_fd);
  84.                                                 printf("Conexao encerrada")                    
  85.                                                 exit(0);
  86.                                         }
  87.                         }
  88.                 close(nova_fd);                     // o processo principal não precisa disso
  89.         }
  90.  
  91.     return 0;
  92. }