title: recvmsg linux receive wrong data
body: <p>I am using recvmsg and sendmsg to send data over an asynchronous STREAM socket . The amount of data to transfer is rather big, between 15 MB and 30 MB. </p>
  
  <p>I cannot understand why the data arrives corrupted. The sending messages and the reciving function gets interrupted by the errno EINTR adn EAGAIN. </p>
  
  <p>The functions I ma using are : </p>
  
  <pre><code>int receive_result_with_extra(int fd, struct syscall_result * result, int extra_size, char * buf) { 
      struct iovec io[2];
      struct msghdr msg; 
      int transfered=0, temp=0; 
      const int total =  SIZE_RESULT + extra_size; 
  
      CLEAN_MSG(&amp;msg);
      memset(io, 0, sizeof(io)); 
  
      // result header 
      io[1].iov_len=SIZE_RESULT; 
      io[1].iov_base=result;
      // result buffer
      io[0].iov_len = extra_size; 
      io[0].iov_base = buf; 
      // iov struct 
      msg.msg_iov=io;
      msg.msg_iovlen=2;
  
      do { 
        temp = recvmsg(fd,&amp;msg, 0);       
        if ( temp &lt; 0 &amp;&amp; (errno==EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) 
          continue; 
        else if ( temp &lt; 0 )
            die("Error receiveing data recvmsg (receive_result_with_extra)"); 
        transfered += temp; 
      } while(transfered &lt; total); 
  
      if ( transfered &lt; 0) 
          die("recvmsg (receive_result_with_extra)"); 
  
      assert(transfered == total);
      return transfered; 
  }
  int send_result_with_extra(int fd, struct syscall_result * result, int extra_size, char * buf) {
  
      struct iovec io[2];
      struct msghdr msg; 
      int transfered=0, temp=0; 
      const int total =  SIZE_RESULT + extra_size; 
  
      CLEAN_MSG(&amp;msg);
      memset(io, 0, sizeof(io)); 
  
      // result header 
      io[1].iov_len=SIZE_RESULT; 
      io[1].iov_base=result;
      // result buffer
      io[0].iov_len = extra_size; 
      io[0].iov_base = buf; 
      // iov struct 
      msg.msg_iov=io;
      msg.msg_iovlen=2;
  
      do {
        temp=sendmsg(fd,&amp;msg, 0); 
        if ( temp &lt; 0 &amp;&amp; (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
            continue; 
        else if ( temp &lt; 0)
            die("Failed sending data ( send_result_with_extra)");  
        transfered += temp; 
      } while( transfered &lt; total);
  
      if ( transfered &lt; 0) 
          die("recvmsg (fstat handler)"); 
  
      assert(transfered == total);
  
      return transfered; 
  }
  </code></pre>