Skip to main content

TCP vs UDP

TCP is always called reliable and UDP unreliable. This note focuses on what that really means.

Everything is same at network layer
  1. TCP and UDP are transport layer protocols.
  2. At network layer, they're just regular IP packets.

Network stack

Every IP packet from the network layer carries one fact. It says if the packet is part of a TCP or a UDP connection.

When the network layer gets a packet, it checks the protocol type. Then it sends the packet to the right transport stack, TCP or UDP.

TCP Connection
acknowledgement packets

In TCP, there is no acknowledgement for each segment. The receiver sends an acknowledgement now and then with a segment number. That means it got all segments up to that number.

Kernel Responsibilities

  1. Entire session establishment, maintenance and teardown is handled by the kernel.
  2. Kernel's network stack maintains a connection table for all active connections.
  3. Kernel handles the full handshake process for TCP connections. Only after the handshake is complete, the application is notified of a new connection.
  4. Kernel handles packet sequence, retransmission, acknowledgements, flow control and congestion control.
  5. Kernel handles packet reordering and duplicate packets.
  6. Kernel handles packet loss and corruption.
  7. Kernel handles fragmentation and reassembly of packets.
  8. Kernel handles buffering of packets.
kernel-states

Application Perspective

For the application, the whole network protocol is hidden. The app just writes a stream of data to a socket. It reads a stream of data from a socket.

The network stack does the rest. It breaks the stream into packets, sends them, receives them, reassembles them, and hands the stream to the app.

Shared buffering responsibility

The kernel buffers packets for the receiver. Still, the app must buffer data at the application layer.

For example, say the app sends a huge file. And the network or the receiver is slow. Then the socket buffer fills up. The app can't write any more data.

In that case, the app uses epoll. It then learns when the socket is ready for writing again.

TCP - Transmission Control Protocol

In this protocol, a session is set up between the real sender and receiver. It spans many devices and networks.

Just regular IP Packets

For the network layer, these are just regular IP packets.

Routers and switches don't care if a packet is TCP, UDP, or a handshake. They just forward it by the destination IP address.

UDP - User Datagram Protocol

UDP is a connectionless protocol. No session is set up between the sender and receiver. Each packet is just sent. The receiver takes and processes the packets as they arrive.

lightweight protocol

UDP is a lightweight protocol as

  • There exists no overhead of connection establishment, maintenance and teardown.
  • IP Packets don't contain the TCP header which has sequence number, acknowledgement number, flags etc.
meaning of Datagram

It comes from the word "telegram." A telegram is a short message sent over a long distance. Each message stands alone, with no link to the others.

NAT

For NAT too, the kernel must process TCP messages.

  1. When there is a SYN packet, the kernel needs to create a new NAT mapping for the connection.
  2. When the connection is closed, the kernel needs to remove the mapping from the entry table.
necessary to delete entries

If NAT doesn't delete entries, the table grows large and uses up memory.