   Transfer and Receive a structure over a (char*) buffer
  I am using code like this to send data over a socket:   char transferBuffer[512];   sprintf(buffer, "Testing\r\n");   len = strlen(transferBuffer) + 1;    if (SDLNet_TCP_Send(sd_tcp, (void *)transferBuffer, len) < len)    {    ErrorLog("SDLNet_TCP_Send: %s\n", SDLNet_GetError());    }  Sending strings and receiving it works fine... but... My question is how can I send and retrieve an identifier and a structure?byte id;SomeStructure sInstance;I would like to put that into transferBuffer, and somehow retrieve it out of the same type of transferBuffer on the receiving end.Some tiny examples will be appriciated...:)   
  Easy, just serialize each field in the struct.  You'll need functions to specify how to serialize each type (in a machine-agnostic way).  I always suggest POSH.struct Stream {    Stream(byte *_p) : p(_p) {    };    byte *p;};/* I use a namespace for each type, and put all its * crap in there. */namespace I32 {    typedef posh_i32_t T;    void Write(Stream &, T);    T Read(Stream &);}/* This is the data you need to send */struct Bar {    int someData;};void Foo(const Bar &b) {    byte buffer[512];    Stream s(buffer);    *s.p++ = PACKET_ID;    I32::Write(s, b.someData);    //send buffer.  length is (s.p - buffer)};  dunno if this works, but should be something like this (WARNING, pseudo-code ahead):int len = sizeof(yourStructure);void* pData = (void*)yourStructure;if (SDLNet_TCP_Send(sd_tcp, pData , len) < len){ ErrorLog("SDLNet_TCP_Send: %s\n", SDLNet_GetError());}  (anyway thank you, I stopped writing c/c++ code a year ago when I started working for the company I'm actually in, here we use only .NET.... it's always a pleasure :D )   ;Quote:Original post by b3rs3rkdunno if this works, but should be something like this (WARNING, this won't work across platforms because of endian differences, or across compilers because of struct padding differences, or at all if your structs contain pointers)Fixed that for you :)   ;  Why are you misquoting him?   ;Quote:Original post by MegahertzWhy are you misquoting him?to make important points about other things you have to be careful about-me   ;Quote:or at all if your structs contain pointersIn general, for any non-POD type.   ;  my fault, I always tend to make things simpler than what they should be :)Anyway (to @stianstr), why don't you take a look at some pre-built library? RakNet is very good for such things :)   