On Monday, September 15, 2014 9:21 PM, Andrew Haley <aph@xxxxxxxxxx> wrote: On 09/15/2014 12:57 PM, Hei Chan wrote: > // on a big-endian 64-bit machine > > struct Message { > int32_t a; > int16_t b; > char c; > char padding; > }; > > // send over a socket > Message msg = {12345, 678, 'x', 0}; I'd do this: Message msg = {htonl(12345), htons(678), 'x', 0}; What if I have no control of the sender? > send(fd, &msg, sizeof Message, 0); > > > // another machine: a little-endian 64-bit machine > char buffer[1024]; > if (recv(fd, buffer, sizeof buffer, 0)) { > Message msg; > // or we can use the union trick Why not read into the Message? I think it goes back to the same issue as Andy (the original poster) -- the server/sender will send multiple kinds of message types. For instances, enum class MsgType : int32_t { Heartbeat, Logon, Logout }; struct Header { int32_t MsgType; int32_t padding; } struct LogonBody { int32_t a; int16_t b; char c; char padding; } I can read into an instance of Header first (1 system call), then I know the message type so I can read into an instance of LogonBody (another system call). But my goal is to avoid latency. I certainly would prefer 1 system call instead of 2 if possible. Hope now it makes sense to you. > memcpy(&msg, buffer, sizeof Message); SomeFunctioToConvertFromBigEndianToSmallEndian(msg.a); I don't know why you'd want to byte-reverse in place if you actually care about zero-copy network programming. That makes no sense to me. Andrew.