# Network protocol for chat — UDP or TCP

I'm trying to develop a fast-paced multiplayer game.

Now I'm using UDP as a transport-layer protocol to communicate between clients and server. But what if I want to implement a chat? Should I send chat messages via UDP as well (with re-sending and other TCP-like stuff) or should I use another TCP socket for such purposes?

Which one is the best practice?

When it comes to chat systems, reliability is far more important than latency and bandwidth. That would usually make it a typical TCP/IP use-case. However, using both TCP and UDP in parallel through the same network can cause more UDP packet loss and adds additional complexity to your application. So it might be smarter to add some optional reliability features to your UDP protocol which mimic certain reliability features of TCP:

1. Every chat message gets a sequence number. When messages arrive out of order, the chat UI automatically reorders them.
2. The receiver must send a confirmation for every text message it receives. When the confirmation doesn't arrive within n ms, the message is resent (with the same sequence number) up to m times.

There might also be other features of your netcode where these reliability features might be handy, but others where you shouldn't bother with either. So decide on a case-by-case basis which message types need sequence numbers and which also need confirmations.

• "using both TCP/IP and UDP in parallel over the same connection can be detrimental for the UDP performance" -- could you provide more info about it, please? – FrozenHeart Aug 2 '16 at 14:51
• @FrozenHeart I added a source to the answer. – Philipp Aug 2 '16 at 14:59
• By "over the same connection" you mean the same (client_ip_addr, server_ip_adder) pair? – FrozenHeart Aug 2 '16 at 15:08
• But not the port, just IP addresses of client and server? – FrozenHeart Aug 2 '16 at 15:12
• @FrozenHeart Yes, that's why Steam by default does not download any updates for other games while a game is running: when it's a multiplayer game it would cause network lags. But this whole side-discussion would really be more of a topic for networkengineering.stackexchange.com – Philipp Aug 2 '16 at 15:31

Your question sounds as if you are a beginner, as me, so my first advice is to use SFML as I did (I used the TCP option it provides). You will see that even if your problem of having to decide between TCP vs UDP is solved, the problem of managing your network will still be considerable, so just take a library which works, no matter if UDP or TCP, because as you gain experience, you will have to change all your code anyway.

Try using Enet if you insist with mixing UDP and TCP. Its actually UDP based, but with one button you can turn reliability on and off as if you were using TCP.

Ps: TCP will guarantee that your chat mesages arrive. UDP doesnt give you this guarantee by default, unless you tweak it to do so. Instead of tweaking it, you can ask Enet to do it automatically for you.

• SFML is not based on TCP, SFML has a network module with UDP, TCP, HTTP, FTP classes that allow cross platform access, that's entirely tangential to the question which you don't make an attempt at answering here. – user5665 Aug 2 '16 at 20:22
• @MattJensJensen You are not able to tell what I attempt or not, even though you might claim my attempt didnt work. – user5193682 Aug 2 '16 at 21:55
• I had only meant that before your edit, you had not attempted to provide a justification for using TCP, now your post-script provides an attempt at that. – user5665 Aug 2 '16 at 22:06