title: Sending a structure over a socket
body: <p>I'm trying to implement a a program to send a structure over a c++ socket from a client to a server.</p>
  
  <p>What I have so far is working fine on my own computer (Both server and client on my own computer) but I suspect I may have trouble when running the server and client on different computers.</p>
  
  <p>What I have so far is:</p>
  
  <pre><code>struct myStruct
  {
      int whatever;
  };
  
  //Sender
  
  myStruct obj;
  obj.whatever = 123;
  char *byteStream = (char *) &amp;obj;
  
  write(socketFD, byteStream, sizeof(myStruct));
  
  //Receiver
  
  char *byteStream = new char[sizeof(myStruct)];
  read(socketFD, byteStream, sizeof(myStruct));
  
  myStruct *received = (myStruct *) byteStream;
  cout&lt;&lt;received-&gt;whatever;
  </code></pre>
  
  <p>The cout statement at ther receiver prints 123.</p>