Page 1 of 1

UDP vs TCP

Posted: 23 Feb 2011, 14:42
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?

Posted: 23 Feb 2011, 15:03
by ThomasLund
Sure - the extension request constructor takes a boolean for if you want to send this particular request as udp or tcp

Posted: 24 Feb 2011, 04:40
by jpardoe
Stupid question here: What are the benefits of using UDP over TCP?

Posted: 24 Feb 2011, 05:46
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.

Posted: 24 Feb 2011, 05:54
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?

Posted: 24 Feb 2011, 06:12
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.

Posted: 24 Feb 2011, 06:13
by jpardoe
Ah hah!

You learn something new every day!

Thanks :)

Posted: 24 Feb 2011, 06:34
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

UDP

Posted: 01 Mar 2011, 13:17
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.

Posted: 01 Mar 2011, 13:19
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