title: socket TCP: Why do I lose messages with scanf
body: <p>I created a server and a client with c in Mac OS.<br>
  What they do is simple: the server sends messages and the client receives them.<br>
  However I don't know why but I always lose some of messages when I type more than one message with the help of <code>scanf</code>.<br>
  Here is the main codes:<br></p>
  
  <pre><code>// client
  char msg[200];
  while(1)
  {
      if(read(sockclient, msg, sizeof(msg)) &lt;= 0)
      {
          break;
      }
      printf("Message recu: %s\n", msg);
      bzero(msg, 200);
  }
  
  
  // server
  while(1)
  {
      lg=sizeof(coord_client);
      newsockfd=accept(sockserveur,(struct sockaddr*)&amp;coord_client,(socklen_t *)&amp;lg);
      char msg2[100];
      while(1)
      {
          scanf("%s",msg2);
          write(newsockfd,msg2,sizeof(msg2));
          printf("message envoye\n");
          bzero(msg2, sizeof(msg2));
      }
  }
  </code></pre>
  
  <p><br>
  The problem is: if I type messages one by one, for example, <code>a&lt;Enter&gt;</code>, <code>b&lt;Enter&gt;</code>, everything is fine. But if I type more than one message at one time, I will always lose some of them. For example, if I type <code>a b c d&lt;Enter&gt;</code>, meaning that I want to send four messages: <code>a</code>, <code>b</code>, <code>c</code> and <code>d</code>, the client can always get three of them, <code>c</code> is miss. But the server does send four times because I get four <code>message envoye</code>.<br></p>
  
  <p><strong>EDIT</strong><br>
  I checked the return value of the function <code>read</code> and I get something that I don't understand.
  <a href="https://i.stack.imgur.com/xtwlJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xtwlJ.png" alt="enter image description here"></a></p>
  
  <p>Please attention: when <code>read</code> returns <code>200</code> instead of <code>100</code>, the client will lose the following message.</p>