I'm trying to write a small online tile-based game. I've established a game - server connection with sockets, so I can easily send Strings from client to server and vice versa.
What I don't understand, is in what form should I store the data to be able to transfer it efficiently? Should I use an object of data, serialize and send it through? Should I store data in one long string? Or is there another way?
If it matters what kind of data I will be sending, it will be information about other players, map objects and etc.
I've been browsing for a couple of days now, and still no results (probably it's because I'm not sure how this issue is called ).
Thank you !
The thing that is most important here is this: is it a turn-based game?
For turn-based games, you can just use TCP sockets and transfer the full game-state (or a delta based on the previous gamestate) over the socket. Does not really matter how it is encoded even.
For realtime games, it is important that you try to update the state as often as possible. It is not important if a packet is lost; maybe another packet is still in transit which overrides the previous packet. This is why you should use UDP sockets instead of TCP.
Considering you are talking about tile-based games, think about what information you need to communicate to your clients. Maybe only the players move, while the rest is static?
This is a difficult issue that does not allow a single conclusive answer. There are a lot of design decisions to take into account, touching the design of the main game simulation loop, cheating, network architecture tests, etc. See https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking for a small example of the issues you face.
The most simple solution I can give you is:
ENTITY NUMBER - NEW POSITION
. (i.e. two 32-bit integer). When you need to communicate multiple updates at once, make sure they are contained in the same TCP packet, and that the TCP send buffer is immediately flushed on both ends.400ms
and beyond. If required, you can transform the packet like this: ENTITY NUMBER - NEW POSITION - CURRENT SPEED VECTOR
which will allow the client to estimate the entity position including lag delay.