Code:
send(mysock, (char*)mystruct, sizeof(mystruct), 0)
You face several issues if you send a structure that way. Compilers can add padding between members of a structure, and for this to work, that padding must be the same on both machines. Also, the "endianness" of the machines must match - if you send a structure from a little endian machine to a big endian one that way, the result will be garbage. Also, any pointers in the structure are meaningless when they reach the other side.
If you want portability, allocate a character array long enough to hold the data, serialize the data to that array, and then send the array with send(). Unfortunately, that word 'serialize' involves 90% of the work in that. By serialize, I mean to write the data in a known and defined way. How you do this is up to you, unless you need to follow some already established protocol. See this URL
I'm assuming you can invent whatever network protocol you want. Example, take this struct:
Code:
struct
{
char some_string[30];
uint32_t some_int;
};
I could send it by just creating a string, and doing:
Code:
char buffer[50];
sprintf(buffer, "%s\t%u\n", some_string, some_int);
send(buffer ...);
...making sure that some_string never contained a '\t', lest my communications become garbled. I now have a sort of text based protocol, whereby I can send a string and an integer. (If you want to send other things, you just have to send enough data so that the other side knows what it is looking at/it can differentiate between different 'messages')
If text is too cumbersome, you can save some space by sending binary integers. I could have sent the byte stream:
Code:
48 65 6C 6C 6F 00 00 00 05 39
Which is the string "Hello", a nul terminator, and the number 1337 encoded as a big endian 32 bit unsigned integer.
To convert to big endian/back to the endianness of your computer, you can use the htonl, htons, ntohl, ntohs() functions.
Overall, I could probably fill a tutorial on serialization alone. Understanding of what endianness is (see above url) and serialization are topics any programmer should be aware of.