🎉 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!

tcp or udp?

Started by
9 comments, last by Saai 23 years, 11 months ago
im planning to make a mmorpg ( i hate calling it that but oh well ) and ive been pondering how im going to do the network stuff. i realize i have two protocols to choose from: udp and tcp. i know what each does, a little bit about them. udp can lose/duplicate/change order of packets and can send to multiple destinations, while tcp doesnt lose/duplicate/change order of packets and requires a socket for each remote destination/connection. (or so i think ) now, what im curious is, which would be the best in a client-server situation in a game? and why? or, could i just get some info on which protocol is best used in what way? thanks, saai
Advertisement
For real time games, UDP gives you fine control over latency and packet retransmission -- ie, you gotta write it yourself. It''s not that hard to whip something up that gives you reliabililty and packet reordering (it''s mostly keeping a queue of incoming packets), but it is much harder to beat an existing protocol like TCP (which was designed by lots of smart people).

For turn based games, TCP gives you very simple and reliable data streams that are optimized for bandwidth. Unfortunately, the bandwidth optimizations add more places where flow control can slow down your data. With a few tweaks (like disabling Nagle''s algorithm), you can turn off some of those optimizations and improve your latency.

Finally, some games (or network game libraries) use both UDP and TCP at the same time, sending data over the right connection to get the best results.

The choice is yours, do you want to spend extra time writing your own or are you willing to suffer the moderate overhead of TCP? Maybe the best approach is to start with TCP, and then decide later whether you need to invest the time and write your own networking layer. Yes, TCP requires a socket for each connection -- but with UDP, you''ll have to dispatch and track the address of every packet you receive.

Personally, I''d recommend sticking with TCP for a truly *massive* MMORPG -- because scalability will be a big concern in everything you do.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
thanks for your reply

i dont want my game to be really massive - im not a professional,
and at the moment i dont have direct access to large connections and servers although i have 2 p3 class systems and may be getting DSL soon.

right now though, i have 56k. i want my game to be playable on 56k. i dont think id mind some extra coding for more performance, network programming looks interesting to me.

when you say, "TCP gives you very simple and reliable data streams that are optimizied for bandwidth" what kind of bandwidth are you talking about? a local lan? 56k? dsl/cable?
or something else?


thanks,

saai
your game might differ, it depends on how dependant you are on fast packets and your coding ability and time.

In most cases, I would use tcp.
I would rather not bother with order or lost packets (UDP), especially since this is a home project and a cleaned up tcp packet can be plenty fast for 56k modems.

TCP is perfect for many occasions and proves still much faster than a layer of DirectPlay.

I''ve only heard of UDP being used for "twitch" games such as quake and tribes where online multiplayers were expected to be in the tens with 56k modems.
___________________________Freeware development:ruinedsoft.com
Just go with TCP, because it is easier to begin with. If you are in a further stadium of your game, and think networking performance could be a bit better, step over to UDP.

To TCP or UDP..

Actually, in my own projects I use both. The more frequent and ''real-time'' data is sent UDP. Initial login & registration, and anything not time-critical is done with a TCP connection.

Mainly, I use UDP to relay player locations and events.

I haven''t tried writing an RPG; I have mainly stuck to, more or less, arcade style systems. You can put one together relatively easily, although there are a lot of details to fill/code in. No one piece is really difficult, but getting it all working together can be tricky.

I''m also a firm believer in designing small and building up. I suggest a similar approach. Build and test pieces and spend the time to design the entire game architecture before you start any real coding.

// CHRIS
// CHRIS [win32mfc]
When I say "TCP is optimized for bandwidth", I mean it was designed for things like FTP and mail protocols -- which exchange lots of data over a variety of network connections. It recognizes when the link can handle more data on the fly, then sends larger packets/more data before pausing for an acknowledgement.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
Using TCP for any real time simulation is the kiss of death.
anonymous:
why is using tcp the kiss of death in a real time game? and, what do you consider ''real time'' ?

fprefect:
the linux kernel had some sort of network support option like that, where on a long distance high latency connection, itd send out another group of packets before waiting on a reply from the other end. Is that very useful on a standard dialup internet connection?


thanks,

saai
Saai, the fact that Anon''s post was exactly 1 sentence long with a broad generalization about an extreme position pretty much eliminates his comment from consideration.

The other posts have presented a pretty good overview of the trade-offs of both protocols. Use the one that seems to work best for your game.


DavidRM
Samu Games

This topic is closed to new replies.

Advertisement