server

By Round Tern, 2 Weeks ago, written in c.
  1. /*
  2. **server.c-astreamsocketserverdemo
  3. */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<unistd.h>
  7. #include<errno.h>
  8. #include<string.h>
  9. #include<sys/types.h>
  10. #include<sys/socket.h>
  11. #include<netinet/in.h>
  12. #include<arpa/inet.h>
  13. #include<sys/wait.h>
  14. #include<signal.h>
  15.  
  16. #define MYPORT 3490                     //theportuserswillbeconnectingto
  17. #define BACKLOG 10                      //howmanypendingconnectionsqueuewillhold
  18.  
  19. void sigchld_handler(int s)
  20. {
  21.         while(wait(NULL) > 0);
  22. }
  23.  
  24. int main(void)
  25. {
  26.         int sockfd, new_fd;             //listenonsock_fd,newconnectiononnew_fd
  27.         struct sockaddr_in my_addr;     //myaddressinformation
  28.         struct sockaddr_in their_addr;  //connector’saddressinformation
  29.         int sin_size;
  30.         struct sigaction sa;
  31.         int yes = 1;
  32.  
  33. if(( sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  34. {
  35.         perror("socket");
  36.         exit(1);
  37. }
  38.  
  39. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  40. {
  41.         perror("setsockopt");
  42.         exit(1);
  43. }
  44.  
  45. my_addr.sin_family = AF_INET;           //hostbyteorder
  46. my_addr.sin_port = htons(MYPORT);       //short,networkbyteorder
  47. my_addr.sin_addr.s_addr = INADDR_ANY;   //automaticallyfillwithmyIP
  48. memset(&(my_addr.sin_zero),’\0’,8); //zerotherestofthestruct
  49.  
  50. if( bind(sockfd, (structsockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  51. {
  52.         perror("bind");
  53.         exit(1);
  54. }
  55.  
  56. if( listen(sockfd, BACKLOG) == -1)
  57. {
  58.         perror("listen");
  59.         exit(1);
  60. }
  61.  
  62. sa.sa_handler = sigchld_handler;                        //reapalldeadprocesses
  63. sigemptyset(&sa.sa_mask);
  64. sa.sa_flags = SA_RESTART;
  65.  
  66. if( sigaction( SIGCHLD, &sa, NULL) == -1)
  67. {
  68.         perror("sigaction");
  69.         exit(1);
  70. }
  71.  
  72. while(1)                                                //mainaccept()loop
  73. {
  74.         sin_size = sizeof( structsockaddr_in );
  75.         if(( new_fd = accept( sockfd,( structsockaddr* )&their_addr, &sin_size)) == -1)
  76.         {
  77.                 perror("accept");
  78.                 continue;
  79.         }
  80.         printf("server:gotconnectionfrom%s\n", inet_ntoa( their_addr.sin_addr ));
  81.  
  82.         if( !fork() )                                   //thisisthechildprocess
  83.         {
  84.                 close( sockfd );                        //childdoesn’tneedthelistener
  85.                 if( send( new_fd,"Hello,world!\n", 14, 0) == -1)
  86.                 {
  87.                         perror( "send" );
  88.                         close( new_fd );
  89.                         exit( 0 );
  90.                 }
  91.         }
  92. close( new_fd );                                        //parentdoesn’tneedthis
  93. }
  94. return0;
  95. }