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.
For instance, if you want to stream content effortlessly, you will find UDP to be the best. But if you need a more stable connection, without packets losses of any kind, then you will find TCP to be your best bet. Really, there is no simple answer to which protocol is the best.
Both TCP and UDP work with most VPNs, and usually, VPNs lay their protocols over them. Today, the most common VPN protocols laid over TCP and UDP are OpenVPN and WireGuard. Some VPNs also develop a custom protocol, such as ExpressVPN’s Lightway.
UDP is used by routing update protocols like Routing Information Protocol. It is typically used for real-time applications that cannot manage delays while receiving messages. The TCP protocol is extremely reliable, which is why it can be used to connect to remote computers over a network.
Whether it is streaming videos, games, live broadcasts, and any other kind of streaming for that matter, you will find UDP to be right up your alley. Apart from being the perfect candidate for streaming, UDP requires minimum resources and is a lot faster than TCP.
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.
You could use both. Use TCP for exchanging the actual messages, (so no data lost and streaming large messages, (eg. containing jpegs etc), is possible. Use UDP only for sending short 'connectNow' messages to clients for which there are messages queued. The clients could have states like (NotLoggedIn, TCPconnected, TCPdisconnected, LoggedOut) with various timeouts to control the state transitions as well as the normal message-exchange events. The UDP 'connectNow' message would instruct clients in 'TCPdisconnected' to connect and so move to 'TCPconnected', where they would stay, exchanging messages, until some inactivity timer instructs the client to disconnect for now. This would, of course, be unreliable and so you may wish to repeat the 'connectNow' message every X seconds for N times until the client connects. The client should, in any case, attempt a poll every X minutes, just in case...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With