Tcp or udp for game server?

I know, I know. This question has been asked many times. But I wasted an hour googling now without finding what I was looking for, so I will ask this again and talk about my context and also what makes me decide: I am writing a server for a game where response time is very important and a waste packages from time to time is not a problem .

Based on this and the fact that I, as a server, basically have to send the same data to many clients, the obvious answer is UDP.

I was already starting to write code when I ran into this:

In some applications, TCP is faster (higher throughput) than UDP. This is the case when there are many small writes in relation to the MTU size. For example, I read an experiment in which a stream of 300 byte packets was sent over Ethernet (1500 bytes MTU) and TCP was 50% faster than UDP.

In my case, the units of information I am sending are <100 bytes, which means that each one fits into one UDP packet (which is pretty nice for me because I don't have to deal with fragmentation) and UDP seems to be much easier to implement for my goal because I don't have to deal with a huge number of single connections, but my top priority is to minimize the time between

client sends something to server

      

and

client receives response from server

      

So, I'm ready to opt for TCP if it's faster. Unfortunately, I could not find more information on the above case, so I ask: Which protocol would be faster in my case?

+3


source to share


1 answer


UDP will still be better for your use case.

The main problem with TCP and games is what happens when a packet is dropped. In UDP, this is the end of the story; the packet is discarded and life continues as before with the subsequent packet. With TCP, data transmission over the TCP stream will stop until the dropped packet is successfully retransmitted, which means that the receiver not only does not receive the dropped packet in time, but subsequent packets will be delayed as well - most likely, all of them will be received in the package immediately after the re-sending of the dropped package is completed.

Another TCP feature that can work against you is automatic bandwidth management - i.e. TCP will interpret the dropped packets as an indication of network congestion and dial back the baud rate in response; potentially to the point of dialing it down to zero in cases where a lot of packets are lost. This can be useful if the cause is indeed network congestion, but dropped packets can also occur due to short-term network errors (for example, the user pulled their Ethernet cable out for a couple of seconds) and you might not want to deal with these problems this way. ; but with TCP, you have no choice.

One disadvantage of UDP is that special handling is often required to receive incoming UDP packets through the user's firewall, as firewalls are often configured to block incoming UDP packets by default. For an action game, however, it is probably worth touching that gives out.



Please note that this is not a strict option / or; you can always write your game to work with both TCP and UDP, and either use them at the same time, or let the program and / or user decide which one to use. This way, if one of the methods doesn't work well, you can simply use the other, and it takes twice as much effort to implement. :)

In some applications, TCP is faster (higher throughput) than UDP. This is the case when there are many small writes in relation to the MTU size. For example, I read an experiment in which a stream of 300 byte packets were sent over Ethernet (1500 bytes MTU) and TCP was 50% faster than UDP.

If this proves to be a problem for you, you can get the same efficiency gain in your UDP protocol by bundling multiple messages into one larger UDP packet. those. instead of sending 3 100-byte packets, you put these 3 100-byte messages together in a 1,300-byte packet. (You need to make sure the recipient program can instruct this larger package correctly, of course). If anything, that's really all the TCP layer uses. placing as much data as possible in outgoing packets as it is available and can fit before sending it.

+5


source







All Articles