I want to build a server/client engine for a game that I will eventually be making.
While planning, I find that I’m stuck on one aspect: Sending objects across a network.
My goals for the client/server are:
- The client/server that I am describing is a ‘world’ server, and thus will use TCP to communicate.
- The server and/or client must be able to send any serializable object to eachother, and the receiver must be able to deserialize it on their end.
- The serialization method preferred would be binary.
However, I don’t know how to send serialized data over a network socket. Sending a string is easy… You can simply append a terminator string to the end and wait for that to be received. I don’t know how you can do something equivalent with objects; so any ideas/help/tutorials will be appreciated.
Thank you.
I’m biased (as the author), but protobuf-net would be an excellent fix here:
In particular, the
SerializeWithLengthPrefixandDeserializeWithLengthPrefix(etc) are well-suited to socket use, as this handles the segmentation of messages for you. If you want, you can also use a variant here (and the non-generic API) to send any of a range of messages and have it recognise the message type based on a pre-defined number you think of.This way, the library does all the worrying about getting the object onto the wire. The only sticking point is your definition of “any serializable object” – as long as the client/server agree on the types of messages involved, it’ll be fine – but “any” is a bit trickier.
If you have an example model (it doesn’t have to be your “real” model), feel free to add it and I’ll show how it can be used in this way.