title: Simple TCP-Server
body: <p>This is a basic TCP-Server implementation for teaching purposes. Are there any error or improvements to do. Any suggest is welcome!</p>
  
  <p>I only have a doubt:        </p>
  
  <pre><code>signal(SIGCHLD, SIG_IGN);
  </code></pre>
  
  <p>Is that call used to prevent zoombie-child processes?</p>
  
  <pre><code> #include &lt;netinet/in.h&gt;
   #include &lt;sys/socket.h&gt;
   #include &lt;netdb.h&gt;
  
   #include &lt;sys/signal.h&gt;
  
   #include &lt;unistd.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;stdio.h&gt;
   #include &lt;string.h&gt;
  
   #define BACKLOG 5
   #define MAXSIZE 1024 //max-bytes for read-buffer
  
  void main(){
  
  int sock_ds, ret, length;
  int acc_ds; //Accept socket descriptor
  
  struct sockaddr_in addr; //this addres
  struct sockaddr rem_addr; //remote address (generic)
  
  char buff[MAXSIZE];
  
  sock_ds = socket(AF_INET, SOCK_STREAM, 0); // =&gt; TCP
  
  bzero((char *)&amp;addr, sizeof(addr)); //reset struct
  addr.sin_family = AF_INET;
  addr.sin_port = htons(25000);
  addr.sin_addr.s_addr = INADDR_ANY;
  ret = bind(sock_ds, &amp;addr, sizeof(addr));
  if(ret == -1){
      perror("Binding error");
      exit(1);
  }
  
  ret = listen(sock_ds, BACKLOG); // backlog queue
      if(ret == -1){
      perror("Listen error");
      exit(1);
  }
  
  length = sizeof(rem_addr);
  signal(SIGCHLD, SIG_IGN); //zombie children management
  
  /*Busy-waiting (server) and concurrency */
  while(1){
  
      /*Repeat until success*/
      while(acc_ds = accept(sock_ds, &amp;rem_addr, &amp;length) == -1){
  
          if(fork() == 0){ //child-process
  
              close(sock_ds); //unused from child
              do{
                  read(acc_ds, buff, MAXSIZE);
                  printf("Message from remote host:&amp;s\n", buff);
  
              }while(strcmp(buff, "quit") == 0);
              /*Transimission completed: server response  */
              write(acc_ds, "Reading Done", 10);
              close(acc_ds); //socket closed
              exit(0); //exiting from child
          }
          else{
              close(acc_ds); //unused from parent
          }
      }
  }
  </code></pre>
  
  <p>}</p>