🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Udp Vs Tcp

Started by
1 comment, last by Drekenzin 22 years, 4 months ago
Hello, I was wondering which is better, udp or tcp, specifically in a mass multiplayer situation. Servers would be supporting a few hundred people per, is there a major difference between udp and tcp? I''ve recieved mixed messages in this area, so which is actually better? Thanks. -Drekenzin /* Crash */
/* Crash */
Advertisement
To be honest... they both have their advantages and disadvantages

UDP:
Advantages:
Unreliable - some messages you can tolerate the packet getting lost.. for example, in a MMORPG a position update getting sent every second could stand losing a packet (though you need to check for out of order packets).. the game would go a little longer without an update, but that''s what client-side prediction is for
Small headers - UDP has a smaller header than TCP which means the overhead (header size/total packet size) is lower... Overhead gets to be a problem if your header is a significant percentage of the data size... eg, sending 8 bytes of data with a 16 byte header only yields 33 percent theoretical efficiency (actually is lower).. this is bad, so the smaller the header the better...
Easy on server - UDP is connectionless so the operating system has less to keep track of for a UDP ''connection''
Disadvantages:
Unreliable - UDP does not guarantee that the packets sent arrive in order or even arrive at all. Certain data needs to arrive in order (position updates, chat messages) and certain data needs to have guaranteed delivery (chat messages, etc)
Program-overhead - UDP requires some extra overhead in that you need to store an IP address for each client connected due to UDP needing an address for each sendto().. This generally isn''t a big deal really, just a little extra work

TCP:
Advantages:
Reliable - for messages that need to arrive and arrive in order TCP is the way to go since it guarantees it
Fairly easy - A good deal of the work is done for you with TCP which can be good for small games with only a few connections, but with a few hundred people this ease comes at a cost
Disadvantages:
Reliable - TCP will automatically resend a packet if the receiver doesn''t acknowledge the packet was received. This may not always be what you want and leads to extra data being sent
Overhead - TCP has a larger header than UDP which decreases efficiency (which only matters for small packet sizes really). It also requires extra data to be stored by the client/server about the connection... though this is generally negligable

Anyway... the generally accepted answer to this question is use both... I dont have a URL but there''s a paper talking about the network protocol Unreal uses (or at least used, not sure about now) which is a pretty good argument for using a UDP channel and TCP channel for different types of communication. I can definitely say it''s a bad idea to use TCP exclusively for massively multiplayer situations (it really shocked me when I found out Anarchy Online was using TCP exclusively.. though it explained a lot). If packets start dropping your effective bandwidth drops due to resent packets (that may or may not get there either - as bandwidth drops more packets time out, get resent, and so on.. aka a packet storm).. movement is harder to get right and you may get some rubber-banding (where you, the client, think you''re walking forward then get yanked back 30 feet by the server because some packet you receive is from 4 seconds ago). The main problem with UDP is building code around it to support receiving data in-order or reliably... but in my opinion it''s easier to add functionality as you need it than it is to work around something that you can''t change

MMORPGs are hard to get right - good luck =)
yes massive multiplayer is definitly hard to get right. The answer to your question will depend largely on the communication needs you have. If there is any sort of faster action, UDP will be your better choice. If you don''t need to communicate often but the game state has to be perfect in sync you''ll be better off with TCP. Try to analyze what has to be sent over the network and why.

This topic is closed to new replies.

Advertisement