title: Multi client socket server client disconnects after first request to the server. (C programming)
body: <p>I am trying to write a multi-client socket server and a client to go with that server, which would send server simple requests, like, current time, date etc. My problem seems to be, that I can't get the client stay connected, because after first request it seems to disconnect and request doesn't reach the server. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;time.h&gt;
  
  void error(char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  void doprocessing (int sock)
  {
     int n;
     char buffer[5];
     bzero(buffer,5);
     n = read(sock,buffer,4);
     time_t rawtime;
     char tim[22];
     char dat[20];
     char day[16];
     char yer[18];
  
     if (n &lt; 0) error("ERROR reading from socket");
  
     printf("Got %.3s command.\n",buffer);
  
     if (strcasecmp(buffer, "tim\n") == 0)
     {
        rawtime = time(NULL);
        strncpy(tim, "Current time: ", 14);
        strncpy(tim+14, (ctime(&amp;rawtime))+11, 8);
        n = write(sock, tim, 22);
     }
     else if (strcasecmp(buffer, "dat\n") == 0)
     {
        rawtime = time(NULL);
        strncpy(dat, "Current date: ", 14);
        strncpy(dat+14, (ctime(&amp;rawtime))+4, 6);
        n = write(sock, dat, 20);
     }
     else if (strcasecmp(buffer, "day\n") == 0)
     {
        rawtime = time(NULL);
        strncpy(day, "Current day: ", 13);
        strncpy(day+13, (ctime(&amp;rawtime)), 3);
        n = write(sock, day, 16);
     }
     else if (strcasecmp(buffer, "yer\n") == 0)
     {
        rawtime = time(NULL);
        strncpy(yer, "Current year: ", 14);
        strncpy(yer+14, (ctime(&amp;rawtime))+20, 4);
        n = write(sock, yer, 18);
     }
     else if (strcasecmp(buffer, "com\n") == 0)
     {
        n = write(sock, "TIM - current time\nDAT - Month and day\nDAY - current weekday\nYER - Year", 71);
     }
     else  n = write(sock, "Wrong command enter COM to see commands.",40);
  
     if (n &lt; 0) error("ERROR writing to socket");
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, newsockfd, portno, clilen, n;
      char buffer[256];
      struct sockaddr_in serv_addr, cli_addr;
      int pid;
  
      if (argc &lt; 2)
      {
          fprintf(stderr, "ERROR, no port provided");
          exit(1);
      }
  
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
  
      if (sockfd &lt; 0) error("ERROR opening socket");
  
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      portno = atoi(argv[1]);
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(portno);
      serv_addr.sin_addr.s_addr = INADDR_ANY;
  
      if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) error("ERROR on binding");
  
      listen(sockfd, 5);
      clilen = sizeof(cli_addr);
  
      while(1)
      {
           newsockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen);
           if (newsockfd &lt; 0) error("ERROR on accept");
  
           pid = fork();
  
           if (pid &lt; 0) error("ERROR on fork");
  
           if (pid == 0)
           {
               close(sockfd);
               doprocessing(newsockfd);
               exit(0);
           }
           else close(newsockfd);
      }
  }
  </code></pre>
  
  <p>Thats the server, and the client:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt; 
  
  void error(const char *msg)
  {
      perror(msg);
      exit(0);
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, portno, n;
      struct sockaddr_in serv_addr;
      struct hostent *server;
  
      char rbuffer[256];
      char sbuffer[256];
  
      if (argc &lt; 3)
      {
         fprintf(stderr,"usage %s hostname port\n", argv[0]);
         exit(0);
      }
  
      portno = atoi(argv[2]);
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
  
      if (sockfd &lt; 0) error("ERROR opening socket");
  
      server = gethostbyname(argv[1]);
  
      if (server == NULL)
      {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
  
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr, (char *)&amp;serv_addr.sin_addr.s_addr, server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
  
      if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) error("ERROR connecting");
  
      for(;;)
      {
         printf("Enter a command: ");
         bzero(sbuffer,sizeof(sbuffer));
         fgets(sbuffer,255,stdin);
         n = write(sockfd,sbuffer,strlen(sbuffer));
         if (n &lt; 0) error("ERROR writing to socket");
  
         bzero(rbuffer,sizeof(rbuffer));
         n = read(sockfd,rbuffer,255);
         if (n &lt; 0) error("ERROR reading from socket");
  
         printf("%s\n",rbuffer);
      }
      close(sockfd);
      return 0;
  }
  </code></pre>