What is better for instant messenger TCP or UDP?

Wizit picture Wizit · Sep 8, 2013 · Viewed 11.2k times · Source

I need to implement client/server instant messenger using pure sockets in Java lang.
The server should serve large number of clients and I need to decide which sockets should I use - TCP or UDP.
Thanks, Costa.

Answer

ug_ picture ug_ · Sep 8, 2013

TCP

Reason:

TCP: "There is absolute guarantee that the data transferred remains intact and arrives in the same order in which it was sent."

UDP: "There is no guarantee that the messages or packets sent would reach at all."

Learn more at: http://www.diffen.com/difference/TCP_vs_UDP

Would you want your chat message possibly lost?

Edit: I missed the part about "large chat program". I think because of the nature of the chat program it needs to be a TCP server, I cannot imagine the actual text content sent by users over a UDP protocol.

The max limit for TCP servers is 65536 connections at the same time. If you really need to go past that number you could create a dispatcher server that would send incoming connects to the appropriate server depending on current server loads.