title: Losing data when sending by UDP
body: <p>I've written a UDP send/receive function to send a struct and listen for another struct back. The bytes have to be sent in a particular order, but this is working OK as I'm using <code>#pragma pack(1)</code>. The only problem that I'm having now is that if any <code>Null</code> values (<code>0x00</code>) appear in the struct, the rest of the data after the <code>Null</code> disappears.</p>
  
  <p>I guess there's something fairly simple that I'm doing wrong, but here is my code:</p>
  
  <pre><code>typedef u_int8_t NN;
  typedef u_int8_t X;
  typedef int32_t S;
  typedef u_int32_t U;
  typedef char C;
  
  typedef struct{
      X test;
      NN test2[2];
      C test3[4];
      S test4;
  } Test;
  
  int main(int argc, char** argv)
  {
      Test t;
      memset( &amp;t, 0, sizeof(t));
      t.test = 0xde;
      t.test2[0]=0xad; t.test2[1]=0x00;
      t.test3[0]=0xbe; t.test3[1]=0xef; t.test3[2]=0xde; t.test3[3]=0xca;
      t.test4=0xde; 
  
      LogOnResponse response;
  
      udp_send_receive(&amp;t, &amp;response);
  
      return 0;
  }
  </code></pre>
  
  <p>And here is my send/receive function:</p>
  
  <pre><code>int send_and_receive(void* message, void* reply, int do_send, int expect_reply)
  {
      struct sockaddr_in serv_addr;
      int sockfd, i, slen=sizeof(serv_addr);
      int buflen = BUFLEN;
      void* buf = NULL;
      struct timeval tv;
      int n_timeouts=1;
      int recv_retval;
  
  //  printf("Message Size: %d\n", strlen(message));
  
      if ( (strlen(message)) &gt;= BUFLEN)
          err("Message too big");
  
      buf = malloc(buflen);
  
      if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
          err("socket");
  
      tv.tv_sec = timeout_seconds;
      tv.tv_usec = timeout_microseconds;
      if( setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,&amp;tv,sizeof(tv)) &lt; 0 ){
          err("Setting Timout"); 
      }
  
      bzero(&amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(PORT);
      if (inet_aton(IP_ADDRESS, &amp;serv_addr.sin_addr)==0)
          err("inet_aton() failed\n");
  
      //---Timeout Send/Receive loop
  
      do{
      if(do_send == TRUE){
          strcpy(buf, message);
          if (sendto(sockfd, buf, buflen, 0, (struct sockaddr*)&amp;serv_addr, slen)==-1)
              err("sendto()");
      }
  
      if (expect_reply == TRUE){
          if( (recv_retval = recvfrom(sockfd, buf, buflen, 0, (struct sockaddr*)&amp;serv_addr, &amp;slen)) == -1){
              itercount++;    
          }
      }
  
      }while ((itercount &lt; itermax) &amp;&amp; (recv_retval == -1));
  
      if ( itercount != itermax ){
          memcpy(reply, buf, BUFLEN);
      }
      else{
          reply=NULL;
      }
  
      close(sockfd);
      free(buf);
      return 0;
  }
  
  void udp_send_receive(void* message, void* reply)
  {
      send_and_receive(message, reply, TRUE, TRUE);
  }
  </code></pre>
  
  <p>Running the above code and capturing the packets with <code>WireShark</code> shows:</p>
  
  <p><code>Data: DEAD000000000000000000....</code></p>
  
  <p>I'd like it to show:</p>
  
  <p><code>Data: DEAD00BEEFDECADE</code></p>
  
  <p>I'd really appreciate some pointers on this.</p>