Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tcp or udp for a game server?

Tags:

tcp

sockets

udp

I know, I know. This question has been asked many times before. But I've spent an hour googling now without finding what I am looking for so I will ask it again and mention my context along with what makes the decision hard for me: I am writing the server for a game where the response time is very important and a packet loss every now and then isn't a problem.

Judging by this and the fact that I as a server mostly have to send the same data to many different clients, the obvious answer would be UDP.

I had already started writing the code when I came across this:

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

In my case the information units I'm sending are <100 bytes, which means each one fits into a single UDP packet (which is quite pleasant for me because I don't have to deal with the fragmentation) and UDP seems much easier to implement for my purpose because I don't have to deal with a huge amount 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 am willing to pick TCP if that's the faster way. Unfortunately I couldn't find more information about the above quoted case, which is why I am asking: Which protocol will be faster in my case?

like image 430
LastExceed Avatar asked Jun 25 '17 13:06

LastExceed


People also ask

Which is better for gaming TCP or UDP?

Most people say UDP is always better for real-time games than TCP. My understanding is that TCP tries to re-send packets over and over til the other side gets them whereas UDP doesn't care. Most of the things I've read is that UDP is a must for any realtime game and TCP is terrible.

Is TCP used for gaming?

Many Massively Multiplayer Online Role-Playing Games (MMORPGs) use TCP flows for communication between the server and the game clients. The utilization of TCP, which was not initially designed for (soft) real-time services, has many implications for the competing traffic flows.

What protocol do game servers use?

The two networking protocols that we'll discuss in the section, and that are also the two most widely used protocols in multiplayer networked games, are the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). Both protocols provide communication services between clients in a network system.

Is TCP fast enough for games?

Games like World of Warcraft use TCP for their communication, because you circumvent many problems by using it. There may be a higher ping as a result, but for many games, this is acceptable.


1 Answers

UDP is still going to be better for your use case.

The main problem with TCP and games is what happens when a packet is dropped. In UDP, that's the end of the story; the packet is dropped and life continues exactly as before with the next packet. With TCP, data transfer across the TCP stream will stop until the dropped packet is successfully retransmitted, which means that not only will the receiver not receive the dropped packet on time, but subsequent packets will be delayed also -- most likely they will all be received in a burst immediately after the resend of the dropped packet is completed.

Another feature of TCP that might work against you is its automatic bandwidth control -- i.e. TCP will interpret dropped packets as an indication of network congestion, and will dial back its transmission rate in response; potentially to the point of dialing it down to near zero, in cases where lots of packets are being lost. That might be useful if the cause really was network congestion, but dropped packets can also happen due to transient network errors (e.g. user pulled out his Ethernet cable for a couple of seconds), and you might not want to handle those problems that way; but with TCP you have no choice.

One downside of UDP is that it often takes special handling to get incoming UDP packets through the user's firewall, as firewalls are often configured to block incoming UDP packets by default. For an action game it's probably worth dealing with that issue, though.

Note that it's not a strict either/or option; you can always write your game to work over both TCP and UDP, and either use them simultaneously, or let the program and/or the user decide which one to use. That way if one method isn't working well, you can simply use the other one, and it only takes twice as much effort to implement. :)

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

If this turns out to be an issue for you, you can obtain the same efficiency gain in your UDP protocol by placing multiple messages together into a single larger UDP packet. i.e. instead of sending 3 100-byte packets, you'd place those 3 100-byte messages together in 1 300-byte packet. (You'd need to make sure the receiving program is able to correctly intepret this larger packet, of course). That's really all that the TCP layer is doing here, anyway; placing as much data into the outgoing packets as it has available and can fit, before sending them out.

like image 183
Jeremy Friesner Avatar answered Oct 07 '22 21:10

Jeremy Friesner