You have a couple of options, there isn't a single trivial solution for this. You will have to design your own protocol that the server and client can agree on with regards to serializing and deserializing to and from the socket stream.
One characteristic of your Message
that makes this a little tricky is that, when flattened to a character array, it results in an array which is a variable length since the Message
's strings are variable length. So there will be some amount of interpretation on the client side since it cannot assume bytes 0-2 correspond to X and so forth.
One approach you might take is to write all of your data members to a string stream with each data member separated by a delimiter, spaces maybe:
std::ostringstream oss;
oss << s_last_id << " " << user_name << " " << content << " " << id;
And then when writing to the socket:
std::string messageString = oss.str();
std::vector<char> writable(begin(messageString), end(messageString));
char* c = &writable[0];
send(socket, c, writable.size(), 0);
On the client side, you could then parse the incoming character array into the corresponding data members:
char recvBuf[bufMax];
recv(socket, recvBuf, bufMax, 0);
std::istringstream iss(std::string(recv));
iss >> s_last_id >> user_name >> content >> id;
It may not be the most performant solution, but it's pretty simple and may work fine for your case.