In this example, we will discuss how to process the following packet from the client to the server:
struct packet
{
uint64_t len;
char buf[MAX_SIZE];
uint64_t data1;
uint64_t data2;
};
The packet consists of some fixed-width integer data and a string (fields in a network must always be fixed width, as you might not have control of the type of computer your application is running on and non-fixed width types, such as int and long, might change depending on the computer).
This type of packet is common among many programs, but as will be demonstrated, this type of packet has challenges with respect to safely parsing.
The server is identical to the previous TCP examples, minus the recv_packet() function (and the recv() function processes packets instead of std::arrays):
class myserver
{
...
void recv_packet()
{
if...