8 Difference between TCP and UDP Protocol in Java? Networking Question

What is the difference between TCP and UDP is a popular networking question from Java interviews? Though TCP or UDP is Java independent concept and very likely to be asked in other programming language interviews as well, many programmers do not really understand them clearly. They sure have heard them because TCP and UDP are two of the most important transport protocol of the internet, but when it comes to listing down the difference between them, they fail to mention key differences in terms of ordering, guaranteed delivery, speed, and usage. 

The biggest benefit of TCP/IP protocol is that it provides guaranteed delivery of messages and in the order client sent them, which is very important when dealing with important messages like order, trade, and book messages. 

You cannot afford to lose them, neither you can process then out-of-sequence. The UDP protocol, on the other hand, provides the much-needed speed and can be used to implement a multicast network. In this article, I'll tell you the difference between TCP and UDP protocol from a Java interview perspective.



Difference between TCP and UDP Protocol?

Here are some important differences between TCP and UDP protocol from the Java application developer's perspective.

1. Reliable

TCP is a reliable stream-oriented protocol as opposed to UDP which is not reliable and based upon datagram. This is actually the most significant difference between TCP and UDP as you cannot use the UDP for sending important messages which you can't afford to lose. 

Though there are some reliable protocols built over UDP like TIBCO certified messaging which implement additional checks whether the message is delivered or not and then facilitate re-transmission.


2. Speed

One more important difference between TCP and UDP comes from speed. Since TCP is reliable and connection-oriented it has lots of overhead as compared to UDP, which means TCP is slower than UDP and should not be used for transferring messages where speed is critical like live telecast, video or audio streaming. This is the reason UDP is popularly used in the media transmission world.
 

3. Data Boundaries

Another structural difference between TCP and UDP is that data boundaries are preserved in the case of UDP but not in the case of TCP because data is sent as it is as one message in the case of UDP but TCP protocol can break and reassemble the data at sending and receiving end.


4. Connection-Oriented

Another key difference between TCP and UDP protocol comes from the fact that TCP is a connection-oriented protocol but UDP is a connectionless protocol. What this means is, before sending a message a connection is established between sender and receiver in TCP but no connection exists between the sender and receiver in UDP protocol.

TCP vs UDP differences



5. Ordering

One more difference between UDP and TCP protocol comes from how they work with ordering. TCP provides you order guarantee but UDP doesn't provide any ordering guarantee. For example, if Sender sends 3 messages then the receiver will receive those three messages in the same order, Sender, has sent, even if they are received at different orders at the receiver end TCP will ensure they are delivered to a client in the order they are sent by the sender. UDP doesn't provide this feature, which means it's possible for the last message to be received first and vice-versa.


6. Overhead

TCP header size is larger than UDP header size due to excessive metadata information sent by TCP protocol. Those are required to ensure the guarantee provided by TCP protocol e.g. guaranteed ordered delivery.  You can also read The TCP/IP Guide: A Comprehensive, Illustrated Internet Protocols Reference 1st Edition, it's easy to read, interesting, and improve your fundamentals on TCP/IP and UDP protocol along with how to write reliable networking programs in Java.


7. Full form

TCP stands for Transmission Control Protocol while UDP stands for User datagram protocol.


8. Multicasting

Multicast can only be used with UDP, it's not possible with TCP because it's a connection-oriented protocol.

Difference between TCP and  UDP in Java Multicasting



In short,
  • TCP is reliable and guaranteed but UDP is not guaranteed. You are at risk of losing messages in UDP.
  • TCP provides ordering guaranteed which means the message will be received in the same order they are sent, UDP protocol doesn't provide such guarantee.
  • TCP is a connection-oriented protocol as compared to UDP which is connectionless.
  • UDP is much faster than TCP protocol.
  • UDP protocol can be used for multicasting like TIBCO RV
  • UDP also preserves data boundary because all data is sent in the same packet, but TCP doesn't as one message may be divided into several packages before sending.

That's all about the difference between TCP and UDP protocol in Java. As I told it's not really a Java-specific question but commonly asked to check a candidate's knowledge of essential networking protocols and computer networks.


Other Java TCP/IP articles you may like
  • Top 15 Java NIO and Socket Programming Interview Questions (list)
  • How to create an HTTP Server in Java using ServerSocket? (solution)
  • How to fix java.net.SocketException: Too many files open java.io.IOException? (solution)
  • How to solve java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer (solution)
  • How to find the IP address of the localhost in Java Program? (solution)
  • What is TIBCO Rendezvous or TIBCO RV messaging? (answer)

P. S. - If you are interviewing for a server-side Java development job then knowledge of TCP/IP, UDP, and Multicasting becomes very important because these the technologies which are used to create high-performance servers in Java.

3 comments:

  1. Hey, I think your posts are great. The language is straightforward and you get to the point without going into too much details. Maybe you can write more articles on how to design applications - what you do before starting to code it. Thanks!

    ReplyDelete
  2. Useful article. god works

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.