TCP vs UDP is a choice between dependable delivery and low overhead. TCP makes a connection, checks that data arrives and keeps it in order. UDP sends each message without those promises. Use TCP when missing data would break the result. Use UDP when a late message is of no use, or when the application will handle loss itself.
TCP vs UDP: the verdict table
| Point | TCP | UDP |
|---|---|---|
| Full form | Transmission Control Protocol | User Datagram Protocol |
| Connection | Set up before data | Not needed |
| Delivery | Resends lost data | No promise |
| Order | Keeps byte order | May arrive in any order |
| Data view | Stream of bytes | Separate messages |
| Header | 20 to 60 bytes | 8 bytes |
| Flow control | Yes | No |
| Congestion control | Yes | No; the app should handle it |
| One-to-many sending | No | Broadcast or multicast possible |
| Examples | HTTP/1.1 and HTTP/2, email, SSH | DNS, DHCP, live calls, games |
TCP vs UDP full form and place in computer networks
The TCP vs UDP full form is easy to remember: Transmission Control Protocol and User Datagram Protocol. The word control tells you TCP manages the sending. The word datagram means a separate message.
In computer networks, both work in the transport layer. They sit above IP. IP moves packets from one machine to another. TCP and UDP use port numbers to give the data to the right program on that machine.
Application HTTP, DNS, SMTP
Transport TCP or UDP
Internet IP
Link Ethernet, Wi-Fi
A simple picture: IP finds the apartment building. The port number finds the flat.
How TCP works
TCP starts with a three-way handshake:
Client -> Server : SYN "Can we talk?"
Server -> Client : SYN-ACK "Yes, I heard you."
Client -> Server : ACK "I heard you too."
After this, each byte has a sequence number. The receiver sends acknowledgements. If data is not acknowledged in time, the sender sends it again. If parts come in the wrong order, TCP arranges them before giving them to the program.
TCP also protects both sides:
- Flow control stops a fast sender from filling a slow receiver.
- Congestion control slows sending when the network seems crowded.
Closing is usually done with FIN and ACK messages from both sides.
Common mistake: Thinking one
sendequals onereceive. TCP is a stream of bytes. Two sends may arrive in one read, and one send may need several reads. Your program must mark message ends, for example with a new line or a length.
How UDP works
UDP adds a source port, destination port, length and checksum. That makes 8 bytes of header. Then it sends the message.
There is no handshake and no automatic resend. A message may be lost, repeated or late. But message borders are kept: if one datagram arrives, you do not get it mixed with another. If your buffer is too small, the end of the message can be cut.
This sounds careless, but it is useful. In a live call, a sound packet that arrives two seconds late is worthless. It is better to skip it and play the next one.
A small UDP example in Java
This program sends a message to itself on the same machine.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
public class UdpDemo {
public static void main(String[] args) throws Exception {
InetAddress local = InetAddress.getLoopbackAddress();
try (DatagramSocket receiver = new DatagramSocket(0, local);
DatagramSocket sender = new DatagramSocket()) {
receiver.setSoTimeout(2000);
byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
sender.send(new DatagramPacket(
data, data.length, local, receiver.getLocalPort()));
DatagramPacket box = new DatagramPacket(new byte[100], 100);
receiver.receive(box);
System.out.println("UDP got: " + new String(
box.getData(), 0, box.getLength(),
StandardCharsets.UTF_8));
}
}
}
Output:
UDP got: Hello
Notice that no connection was accepted. The receiver also has a timeout, because a UDP message may never come.
A small TCP example in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpDemo {
public static void main(String[] args) throws Exception {
InetAddress local = InetAddress.getLoopbackAddress();
try (ServerSocket server = new ServerSocket(0, 1, local);
Socket client = new Socket(local, server.getLocalPort());
Socket link = server.accept()) {
PrintWriter out =
new PrintWriter(client.getOutputStream(), true);
out.println("Hello");
BufferedReader in = new BufferedReader(
new InputStreamReader(link.getInputStream()));
System.out.println("TCP got: " + in.readLine());
}
}
}
Output:
TCP got: Hello
Here a connection exists before the data. readLine uses the new line to find the end of the message. A real server would handle many clients, often with threads. See process vs thread for that idea.
TCP vs UDP examples
| Use | Usual protocol | Reason |
|---|---|---|
| Loading a site over HTTP/1.1 or HTTP/2 | TCP | All bytes must arrive in order |
| HTTP/3 | QUIC over UDP | QUIC adds reliability itself |
| Sending email with SMTP | TCP | Message must be complete |
| SSH remote login | TCP | Every key and reply matters |
| Database connection | Mostly TCP | Data must be correct |
| Normal DNS lookup | Often UDP, also TCP | Small question and answer |
| Getting an IP address with DHCP | UDP | Device may not have an address yet |
| Live voice and video | Often UDP | Late sound is not useful |
| Fast online games | Often UDP | Newest position matters most |
Streaming a recorded film is different from a live call. Film services commonly use HTTP, and the player keeps a few seconds in reserve. So do not say "all video uses UDP."
Pro tip: DNS is a good exam example because it uses both. UDP serves many small lookups. TCP is used for zone transfers and when an answer is too large for the UDP reply.
Is UDP always the faster protocol?
UDP has less to do, so it can start sooner and keep delay low. But speed for the user depends on the whole system. A file sent with plain UDP may arrive with missing parts. If you add numbers, resends and speed control, you have rebuilt much of TCP.
QUIC takes this route in a careful way. It uses UDP to carry packets and adds reliability, encryption and separate streams. A lost packet in one stream need not block the data of another stream.
Choose TCP if / choose UDP if
Choose TCP if
- every byte matters
- order matters
- you want delivery handled for you
Choose UDP if
- fresh data matters more than old data
- messages are small and can be repeated
- you need multicast or broadcast
- you are ready to handle loss in the app
Application developers often start with HTTP or a library and never open a raw socket. Learn the difference to debug timeouts and make better choices. The REST API guide and GET vs POST cover the layer you will use more often.
What the errors tell you
The protocol also explains what you see when something fails.
| What you see | Likely meaning |
|---|---|
| TCP "connection refused" | The machine answered, but no program is listening there, or something rejected it |
| TCP connection timeout | No answer came; a firewall or wrong address is possible |
| TCP "connection reset" | The other side ended the connection suddenly |
| UDP receive timeout | The message or reply was lost, blocked or never sent |
With UDP, silence can have many causes, because the protocol itself does not report that a message arrived. Check the port, the firewall and the server log in that order.
Freshers: TCP vs UDP difference in an interview
30-second answer: "Both are transport layer protocols. TCP is connection-based and gives reliable, ordered byte delivery with flow and congestion control. UDP sends separate datagrams without those promises and has an 8-byte header. Email and SSH use TCP. DNS lookups and live calls often use UDP."
Then be ready to draw the handshake, name two examples for each protocol and explain HTTP/3. To practise writing the Java side, see the Java full stack course. For more server-side topics, try the DevOps questions.
Interview note: A frequent trap is "Is UDP unreliable, so why use it?" Answer that the protocol does not promise delivery, but an application can add the checks it needs. Some data is also useless after a delay.
Frequently Asked Questions
Is UDP always faster than TCP?
Does HTTP use TCP or UDP?
Can TCP and UDP use the same port number?
Does TCP encrypt data?
Does a video call use TCP or UDP?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

