I have a threaded server, usually one thread per client, so whatever packets I receive will be from the same source.
I am designing a protocol based on
struct Packet
{
int Data;
char Data2 [size];
} Packet;
and any other permutations I may need.
The only way I can distinguish between packets so far is based on their size. Since both the server and the client have a the same struct declarations, sizeof(Packet) on the server will be the same as sizeof(Packet) (assuming identical hardware) on the client, and when I call
int bytesReceived = recv(...);
switch (bytesReceived) { (...) }
I can pass on the buffer to a packet-specific function to handle it.
This is imperfect at best, because
- Datatype sizes may differ per platform --> a mismatch can occur between server and client
- I may have two different packets of identical size.
What is a good workaround this problem? How can I design a protocol in a better way?