Archived

This topic is now archived and is closed to further replies.

GenmaC

UDP vs TCP

Recommended Posts

Why would you use UDP in a game if there is a possiblity of the packet being lost? TCP seems more secure to me. How much faster does UDP get? I haven''t read up on these, using TCP for now, so I figured I''d just ask and see if anyone has any input on this issue.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It can be considerably faster - and there are ways to combat the possible losses you can experience including self-imposed authentication of packets etc. It will most likely be more difficult for you to establish a flexible and powerfull system with UDP but it has the potential to be a faster system. In the end, TCP could be exactly what you want - depending on the type of application/game it could work perfectly for you, however UDP has some great benefits and should also be researched/looked into.

Good Luck, later

Share this post


Link to post
Share on other sites
In ideal conditions UDP is much faster. UDP is however very unreliable and if not implemented right could end up being slower than TCP. It''s also a lot more difficult to implement as you have to do all the error checking yourself to verify packet integrity and that they''re being recieved.

In many cases using UDP just ends up with you reinventing the TCP wheel.

Personally I''d start with TCP to get things working and then switch to UDP if TCP isn''t fast enough for you. As long as you keep all things Winsock related seperate from the rest of your code, making the switch wouldn''t require modifying any of your actual game and message handling code.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Dot Com ]

Share this post


Link to post
Share on other sites
Because you don''t care that the packet gets lost... probably 90% of game network traffic can be sent unreliably. TCP is not more secure than UDP. If you implement TCP networking improperly, you open yourself up to severe security issues (e.g. Syn attacks and others).

UDP is much faster than TCP. Go to google and do a search on "the Nagle algorithm" and "delayed ack" for information why TCP is too slow for typical real time games. TCP can cause jerky receipt of data even with a single user.

As stated by another poster, I would recommend you start with TCP, then migrate to UDP as you begin to understand the operation of the underlying network. UDP is a little bit more work and requires a magnitude of understanding above that required to use TCP. But once you write the code for it, you will always have it available for future projects. And honestly, if you don''t want to write a reliable UDP layer, you can always use TCP for your reliable data (at a smaller performance penalty than if using TCP for all data traffic).

Share this post


Link to post
Share on other sites
quote:
Original post by KalvinB
It''s also a lot more difficult to implement as you have to do all the error checking yourself to verify packet integrity and that they''re being recieved.



Actually, you don''t have to do that. UDP gaurantees that if you recieve a packet, that it''ll be exactly the same as the one that was sent (if it''s not, it''ll just pretend that the whole packet was dropped)

Though you do often end up writing your own gauranteed protocol for "important" messages. However, a lot of data that is sent for real-time games isn''t that important - if you drop a position update, you can just get the next one anyway.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
"UDP gaurantees that if you recieve a packet, that it''ll be exactly the same as the one that was sent"

Did not know that. That would make sense as it''s trivial to set up your own checksum to verify integrity and ignore corrupt packets. Hardware is just doing what I assumed would need to be done in software.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Dot Com ]

Share this post


Link to post
Share on other sites