proper use of UDP

Started by
11 comments, last by wing_majin 20 years, 5 months ago
Use TCP for connections that need all the data, eg. when a client initially connects to a game server and needs to send username/password and receive game information, number of games, players playing etc. Then the client needs to create an account or pick a player type, etc. It can also be used for data that isn''t time-critical, for example if you allow chatting in your game the chat messages can go over the tcp connection.

Use UDP for data that can be lost, eg. if your game informs the client of the NPC''s or other game state around the player 4 times per second, the client doesn''t need to receive that, it can just wait for the next update.

Your client can open both an UDP and TCP connection to the server, and use them both for different types of messages.
Advertisement
The problem with TCP is that it only provides certain type of reliable transfer: Everything must be transferred reliably. But in a game, only *the most recent* info must be delivered reliably. Suppose player''s energy drops to 50. Server tries to send this as a packet to player. The packet gets lost. Then the server sends it again until the player acknowledges that it has been received. But then, take a scenario where the energy first drops to 50, then the packet is sent and lost, and meanwhile the energy has dropped to 30. TCP would still send *both* energies 50 & 30, even though only 30 is meaningful for the client. So while the transfer must be reliable, it doesn''t need to be reliable in the same sense as TCP is.

And if some data is reliable (even in TCP sense) and some is unreliable, sending them all using the same UDP allows you to merge packets better, resulting in less traffic.
http://enet.cubik.org is more or less exactly what you're looking for.

I'm hip because I say "M$".

[edited by - the speed bump on November 17, 2003 11:07:08 AM]
"There is only one everything"

This topic is closed to new replies.

Advertisement