Alex
Alex

Reputation: 1

Serializing a class that contains a string, to send it over a network through a socket?

I'm experimenting a little bit with the winsock library, by creating a pretty simple server-client connection. The client should send messages to the server, which should forward them to all the clients. Each client should only print a message if it's not the sender of the message itself.

In order to identify messages and serialize/deserialize them when sending them through the network, I created a Message class, that has among its members a std::string.

However, I'm having trouble with the serializing process.

To serialize, I use a struct SerializedMessage:

typedef struct {
    int         iMessageID;
    std::string strText;
} SerializedMessage;

I'd then like to create a new SerializedMessage* pointer, store data inside it and send it through the socket. While I can easily save the id using the htonl() function, I don't know how to act with the string.

This is my Serialize method so far:

void* Message::Serialize()
{
    SerializedMessage* pSerializedMessage = new SerializedMessage();
    pSerializedMessage->iMessageID = htonl(m_iMessageID);
    //copying the text?
    return (void*)pSerializedMessage;
}

I've tried looking here and it should answer my question, but I'm a beginner and I'm not really understanding what the solution suggests me.

Would really appreciate some help. Thanks!

Upvotes: 0

Views: 387

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57718

The root cause of the problem is that text strings are variable length records.

There are three popular methods for serializing strings: 1) length followed by text, 2) text with terminator character and 3) Fixed length (maybe with padding).

Length Followed by Text

Write the length of the text.
Write the text.

+-------------+  
| Text Length |  
+-------------+  
|             |
|    Text     |  
|             |
+-------------+  

This technique is nice because you can block read the text; you know the length before reading (which also helps in allocating memory dynamically).

Text With Terminator (Sentinel) Character

This is the definition of a C-Style string.

One issue with this method is that you don't know the length which makes allocating memory more tedious and you have to search until the terminator character is found (usually character by character).

Fixed Length

Use a block size that is large enough to accommodate the largest text length, such as 4096. This is popular with a lot of databases. It is a performance / space trade-off. The block is fast to load (read), because it is a fixed size. It may waste space because there may be space not occupied by the text.

Upvotes: 2

Related Questions