title: Passing a structure through Sockets in C
body: <p>I am trying to pass whole structure from client to server or vice-versa. Let us assume my structure as follows</p>
  
  <pre><code>struct temp {
    int a;
    char b;
  }
  </code></pre>
  
  <p>I am using <strong>sendto</strong> and sending the address of the structure variable and receiving it on the other side using the <strong>recvfrom</strong> function. But I am not able to get the original data sent on the receiving end. In sendto function I am saving the received data into variable of type struct temp.</p>
  
  <pre><code>n = sendto(sock, &amp;pkt, sizeof(struct temp), 0, &amp;server, length);
  n = recvfrom(sock, &amp;pkt, sizeof(struct temp), 0, (struct sockaddr *)&amp;from,&amp;fromlen);
  </code></pre>
  
  <p>Where pkt is the variable of type struct temp. </p>
  
  <p>Eventhough I am receiving 8bytes of data but if I try to print it is simply showing garbage values. Any help for a fix on it ?</p>
  
  <p><strong>NOTE:</strong> <strong>No third party Libraries have to be used.</strong></p>
  
  <p><strong><em>EDIT1:</strong> I am really new to this serialization concept .. But without doing serialization cant I send a structure via sockets ?</em></p>
  
  <p><strong><em>EDIT2:</strong> When I try to send a string or an integer variable using the <strong>sendto</strong> and <strong>recvfrom</strong> functions I am receiving the data properly at receiver end. Why not in the case of a structure?  If I don't have to use serializing function then should I send each and every member of the structure individually? This really is not a suitable solution since if there are 'n' number of members then there are 'n' number of lines of code added just to send or receive data.</em></p>