UDP vs TCP

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Post Reply
iceshaft07
Posts: 33
Joined: 28 Jan 2011, 20:10

UDP vs TCP

Post by iceshaft07 »

Hello;

With Unity, UDP can be initialized. I was wondering if there is a way to send some extension calls via TCP and some via UDP--

So, I would update positions via UDP, but send player kills via TCP. Is there any way to do this?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Sure - the extension request constructor takes a boolean for if you want to send this particular request as udp or tcp
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Post by jpardoe »

Stupid question here: What are the benefits of using UDP over TCP?
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Post by tchen »

jpardoe wrote:Stupid question here: What are the benefits of using UDP over TCP?
Traffic flow.

Assuming you time code your packets, we're going to ignore out of order packets.

So it comes down to whether you want the stream to suddenly halt/wait if one of the packets takes a longer route. UDP just keeps going and going.
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Post by jpardoe »

So if we put that into context...

In a real-time game, say a player fires an arrow.

In TCP if the packet takes a long route, the game will pause (lag)?

In UDP the packet would be dropped and so other players would not see the arrow being fired.

Correct?
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Post by tchen »

jpardoe wrote:So if we put that into context...

In a real-time game, say a player fires an arrow.

In TCP if the packet takes a long route, the game will pause (lag)?

In UDP the packet would be dropped and so other players would not see the arrow being fired.

Correct?
Yes, and that's why iceshaft wants to put transient data like position on UDP and guaranteed data on TCP.

You could always rebuild the TCP protocol on UDP and just keep one socket, but that's another fun-filled post I'm too tired to get into.
Last edited by tchen on 24 Feb 2011, 06:13, edited 1 time in total.
jpardoe
Posts: 132
Joined: 31 Aug 2009, 20:54

Post by jpardoe »

Ah hah!

You learn something new every day!

Thanks :)
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

And there is an even longer discussion about UDP not always being the best choice either. There are some overheads too + it takes a larger toll on network equipment having to inspect each and every UDP packet. Etc.etc.

So dont use UDP as the wonder weapon to kill em all.

But in rough terms:
- if you have throwaway data that you send often, then UDP is nice. Transforms for example
- if you have one shot messages that _have_ to get through, TCP is your choice. Login, chat, payment - those kinds of things

For things in between - its up to you :-)

/T
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
atarax
Posts: 6
Joined: 25 Feb 2011, 13:45

UDP

Post by atarax »

For a good example of how some tcp high level features are used on top of UDP check out ENET,

its in c++ but you can download the code and look through it if your really interested. More low level than smartfoxserver though.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

If you want reliable UDP, someone already invented that. Its called TCP

/me ducks the religious peons who throw things at me for the provoking statement
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Post Reply