title: Porting an application from little-endian to big-endian architecture
body: <p>I have a TCP server developed on x86 architecture using C under Linux using berkley socker API. The server runs fine without any problems. But now for some reasons I have to run the server on MIPS architecture which has a <code>big-endian</code> architecture. </p>
  
  <p>The server and the clients communicate through a set of predefined protocol. I will give an example of how a server sends a simple message to the clients:</p>
  
  <pre><code>struct echo_req req;
  
    req.header.version = OFP_VERSION;
    req.header.type = OFPT_ECHO_REQUEST;
    req.header.length = htons (sizeof req);
    req.header.xid = htonl(y);
    req.data = htonl (456);
  
  char data[sizeof (req)];
  data[0] = req.header.version;
  data[1] = req.header.type;
  memcpy (data + 2, &amp;req.header.length, 2);
  memcpy (data + 4, &amp;req.header.xid, 4);
  memcpy (data + 8, &amp;req.data, 4);
  
    if ((send (sock_fd, &amp;data, sizeof (data), 0) == -1))
      {
        printf ("Error in sending echo request message\n");
        exit (-1);
      }
  printf("Echo Request sent!\n");
  </code></pre>
  
  <p>As you can see I use <code>htonl</code> and <code>htons</code> for any type longer than a byte to convert it to network byte order. After making up a packet I serialize and pack the data in <code>char</code> array and finally send it over to the netowrk.</p>
  
  <p>Now before I run my server on Big-endian architecture I wanted to clear out a few things. In my perception as I <code>memcpy</code> the data and pack it, if I send it over the network it shouldn't cause any problems on the big-endian architecture as memcpy will perform a byte by byte copy of the data into the array and hence there shouldn't be any problem with the byte ordering when running on Big-endian. Yet I wanted to get the opinion of you people out there which I persume know a lot more than I do as I am still a beginner in network programming :). Please guide me in this whether I am on the right track or not. All help much appreciated.</p>
  
  <p>Thanks</p>