What is better for instant messaging TCP or UDP?

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

+6
source share
4 answers

TCP

Cause:

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

UDP: "There is no guarantee that sent messages or packets will be available at all."

Find out more: http://www.diffen.com/difference/TCP_vs_UDP

Would you like to lose your chat message?

Change I missed part of the "great chat program." I think that due to the nature of the chat program, it should be a TCP server, I can’t imagine the actual text content sent by users via UDP.

The maximum limit for TCP servers is 65,536 connections at a time. If you really need to go through this number, you can create a dispatch server that will send incoming connections to the appropriate server depending on the current server loads.

+7
source

You can use both. Use TCP to exchange actual messages (so data loss and streaming of large messages (for example, containing jpegs, etc.) are possible. Use UDP only to send "connectNow" short messages to clients for which there are messages in the queue. May have states such as (NotLoggedIn, TCPconnected, TCPdisconnected, LoggedOut) with different timeouts for managing state transitions, as well as regular messaging events. The UDP “connectNow” message should indicate to clients in “TCPdisconnected" to connect, and thus "TCPconnected" where he will be in the middle of a message exchange until some kind of inactivity timer allows the client to disconnect at the moment, which, of course, would be unreliable, and so you can repeat the “connectNow” message every X seconds for N times until the client connects. should in any case try to interrogate every X minutes, just in case ...

+4
source

It depends on whether the user needs to know if messages have been delivered to the server. UDP packets do not have inherent acknowledgment. If the client sends an IM message to the server and it disappears during the transition, neither the client nor the server will be aware of this.

(The short answer is “use TCP” ... but you should think about the design implications for you.)

+1
source

TCP will give you reliability, which is certainly advisable when during instant messaging you do not want messages to be deleted during the conversion.

However, if you plan to use group messages, you can use mulitcast. In such cases, UDP will be the correct chioce, since UDP can handle points for a multipoint connection. Using TCP for multicast applications would be difficult as the sender would now have to track retransmissions / baud rate for multiple receivers. One alternative would be to use TCP for point-to-point chat and use UDP for group messaging.

+1
source

Source: https://habr.com/ru/post/953419/


All Articles