title: Linux socket: How to make send() wait for recv()
body: <p>I am making a simple client-server application using TCP protocal.</p>
  
  <p>I Know that by default. <code>recv()</code> will block until the other side call a <code>send()</code> to this socket.
  But is it possible that <code>send()</code> block itself until the other side has <code>recv()</code>ed the msg instead of keeping <code>send()</code>ing to outgoing queue and later to find the other side <code>recv()</code> got a whole bunch of msg sent by multiple <code>send()</code>s</p>
  
  <p>In other words. Is it possible to let every <code>send()</code> wait for the other side's <code>recv()</code> before it can call another <code>send()</code>?</p>
  
  <p>To ilustate my question. I'll post a simple code here:</p>
  
  <p>client.c</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;poll.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;arpa/inet.h&gt; 
  
  int main(int argc, char *argv[])
  {
      int sockfd = 0;
      char sendBuff[1024];
      struct sockaddr_in serv_addr;
      int i;
  
      if(argc != 2)
      {
          printf("\n Usage: %s &lt;ip of server&gt; \n",argv[0]);
          return 1;
      } 
  
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          printf("\n Error : Could not create socket \n");
          return 1;
      } 
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr)); 
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(5000); 
  
      if(inet_pton(AF_INET, argv[1], &amp;serv_addr.sin_addr)&lt;=0)
      {
          printf("\n inet_pton error occured\n");
          return 1;
      } 
  
      if( connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
          printf("\n Error : Connect Failed \n");
          return 1;
      }
  
      do{
          memset(sendBuff, '\0', sizeof(sendBuff));
          sprintf(sendBuff, "This is line %d", i);
          send(sockfd, sendBuff, strlen(sendBuff), 0);
          //sleep(1);
      }while(++i&lt;100);
  
      return 0;
  }
  </code></pre>
  
  <p>server.c</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;time.h&gt; 
  
  int main(int argc, char *argv[])
  {
      int listenfd = 0, connfd = 0;
      struct sockaddr_in serv_addr; 
  
      char sendBuff[1025];
      char recvBuff[100];
  
      int i = 0;
  
      listenfd = socket(AF_INET, SOCK_STREAM, 0);
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
      memset(sendBuff, '0', sizeof(sendBuff)); 
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      serv_addr.sin_port = htons(5000); 
  
      bind(listenfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)); 
  
      listen(listenfd, 10); 
  
      connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
  
      do{  
          memset(recvBuff, '\0', sizeof(recvBuff));
          recv(connfd, recvBuff, sizeof(recvBuff),0);
          printf( "%s\n", recvBuff);
      }while(++i&lt;100); 
  
      return 0;
  } 
  </code></pre>
  
  <p>What I expect on the server side outcome is to print:</p>
  
  <pre><code>This is line 0
  This is line 1
  This is line 2
  This is line 3
  ...
  </code></pre>
  
  <p>However, the actual outcome is like this:</p>
  
  <pre><code>This is line 0
  This is line 1This is line 2This is line3This is line 4
  This is line 5This is line 6This is line 7This is line 8This is line 9This is line 10
  This is line 11This is line 12...
  </code></pre>
  
  <p>However this is easy to explain: when the client side issued a <code>send()</code>, it didn't wait for the server side's <code>recv()</code> to finish, and for some reason, the server side <code>recv()</code> loop is slower than client side's <code>send()</code>. Thus several <code>send()</code>s on client side may get piled together and received by server as a whole. (is my explanation right?)</p>
  
  <p>Actually there seems to be a very silly and loose solution. just add a <code>sleep(1)</code>(as I commented out) after each <code>send()</code> in the loop. I know this will make the code very inefficient and if the <code>recv()</code> loop will take longer time to implement some other complicated actions( This is obviously unpredictable when the program gets large) which will take longer than 1 sec. This solution fails!</p>
  
  <p>So is there a better solid way to let the two sides communicate with each other to ensure the msg sent by one single <code>send()</code> received by a single <code>recv()</code>? </p>