title: C Linux Sockets( Server Client Synchronization issue )
body: <p>I have written a sample socket program in C on Linux.
  The server is single process server.
  The program is simple where the server is running and the client connects to the server waiting on accept() call.
  When the server accepts the client request it sends some string to the client using write call.</p>
  
  <p>Server Code:</p>
  
  <pre><code>#define MAXHOSTNAME 256
  #define MAX_CONN 10
  
  void single_process_server(unsigned int portNumber)
  {
      int listenSockFd, acceptSockFd, portNo;
      socklen_t clilen;
      char buffer[256] = "Connected";
      struct sockaddr_in srvInfo;
      int n;
  
      char sysHost[MAXHOSTNAME+1];  // Hostname of this computer we are running on
  
      if((listenSockFd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          close(listenSockFd);
          error("ERROR opening socket");
      }
  
      bzero((char *) &amp;srvInfo, sizeof(srvInfo));
      portNo = portNumber;
      srvInfo.sin_family = AF_INET;
  
      srvInfo.sin_addr.s_addr = htonl(INADDR_ANY);
      srvInfo.sin_port = htons(portNo);
  
      if (bind(listenSockFd, (struct sockaddr *) &amp;srvInfo, sizeof(srvInfo)) &lt; 0)
      {
          close(listenSockFd);
          error("ERROR on binding");
      }
  
      listen(listenSockFd,5);
  
      while(1)
      {
          clilen = sizeof(srvInfo);
          if((acceptSockFd = accept(listenSockFd, (struct sockaddr *) &amp;srvInfo, &amp;clilen)) &lt; 0)
          {
              error("ERROR on accept");
          }
  
          if((n = write(acceptSockFd,buffer,255)) &lt; 0)
          {
              error("ERROR writing to socket");
          }
          close(acceptSockFd);
      }
  
      close(listenSockFd);
  }
  
  
  int main(int argc, char* argv[])
  {
      unsigned int portNo= 0;
  
      portNo = 444;
      single_process_server(portNo);
  
      return(0);
  }
  </code></pre>
  
  <p>The client receives the string using read call.</p>
  
  <p>Client Code:</p>
  
  <pre><code>void simple_internet_client(char hostip[], unsigned int portNo)
  {
      int sockFd, portno, n;
      struct sockaddr_in srvInfo;
      struct hostent *server;
  
      char buffer[256];
  
      portno = portNo;
  
      if((sockFd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          error("ERROR opening socket");
      }
  
      if((server = gethostbyname(hostip)) == NULL)
      {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
  
      bzero((char *) &amp;srvInfo, sizeof(srvInfo));
      srvInfo.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr, (char *)&amp;srvInfo.sin_addr.s_addr, server-&gt;h_length);
      srvInfo.sin_port = htons(portno);
  
      if(connect(sockFd,(struct sockaddr *) &amp;srvInfo,sizeof(srvInfo)) &lt; 0)
          error("ERROR connecting");
  
      {
      bzero(buffer,256);
      if((n = read(sockFd,buffer,255)) &lt; 0)
          error("ERROR reading from socket");
  
      printf("Socket read = %s\n", buffer);
      }
  
      close(sockFd);
  }
  
  main(int argc, char* argv[])
  {
      char hostip[50] = {0};
      unsigned int portNo= 444;
  
      printf("B. Provide the host name  or IP:\n");
      scanf("%s",&amp;hostip);
  
      simple_internet_client(hostip, portNo);
  
      return(0);
  }
  </code></pre>
  
  <p>When I run the client(./clientdemo) at some intervals then the client receives whatever string the server sends.</p>
  
  <p>But when I run the client multiple times through some script then the client remains stuck up at read() call and does not receive what the server has sent.</p>
  
  <p>Simply speaking when the client connects to the server at faster rate then the server writes to the socket at same rate.But the client is not able to read the data written on the socket and remains stuck up on the read() call.</p>
  
  <p>What may be the cause of this and why client is not able to read data from socket at same rate as server writes to it?</p>