Hello.
This should get you started.
Code:
/* Assuming that the length of your messages fits into a short */
void writer(void)
{
/* Determine message and length (len) of message */
/*
...
*/
/* Encode len into network byte order */
short netLen = htons(len);
/* put indvidual bytes of len into the write buffer */
*pBuffer++ = char(netLen >> 8); // most significant byte will be written first
*pBuffer++ = char(netLen & 0xFF); // least significant second
/* fill up rest of buffer */
/*
...
*/
/* write to socket len + 2 */
}
void reader(void)
{
char buffer[128];
/* read from socket 2 bytes into buffer and do the reverse of what the writer did */
short netLen = short(buffer[0]) << 8; // extract the most significant byte
netLen |= buffer[1]; // add in the least significant byte
short len = ntohs(netLen); // translate from network byte order to local machine order
/* ret from socket len bytes */
}