The network layer delivers packets to a host. The transport layer delivers data to the right application process on that host, and may add reliability.
Role of the Transport Layer
The transport layer provides process-to-process (end-to-end) communication. Port numbers (16 bits, 0 to 65,535) identify processes: well-known ports 0–1023 (HTTP 80, HTTPS 443, DNS 53, SMTP 25), registered ports 1024–49151 and dynamic (ephemeral) ports 49152–65535 used by clients. A connection is identified by a socket pair: source IP, source port, destination IP and destination port. The transport layer also performs multiplexing (gathering data from many processes) and demultiplexing (delivering each segment to the right process), segmentation and reassembly, and optionally error, flow and congestion control.
Definition: TCP. Transmission Control Protocol, a connection-oriented, reliable, byte-stream transport protocol that provides ordered delivery, retransmission, flow control and congestion control.
TCP
TCP is connection-oriented: a connection is set up with a handshake before data flows and closed afterwards. It is reliable: every byte has a sequence number, the receiver sends acknowledgements, lost segments are retransmitted after a timeout, and data is delivered in order without duplicates. It treats data as a byte stream, provides flow control (the receiver's window) and congestion control, and is full-duplex. The TCP header is 20 to 60 bytes, with fields for source and destination ports, sequence number, acknowledgement number, header length, flags (SYN, ACK, FIN, RST, PSH, URG), window size, checksum and urgent pointer. TCP is used by the web (HTTP/HTTPS), email (SMTP, IMAP, POP3), file transfer (FTP, SFTP) and remote login (SSH).
UDP
UDP (User Datagram Protocol) is connectionless and unreliable: it sends independent datagrams with no handshake, no acknowledgements, no retransmission and no ordering. Its header is only 8 bytes (source port, destination port, length and checksum). It is fast and has low overhead, and it supports broadcast and multicast. It suits applications where speed matters more than perfect delivery, or that handle reliability themselves: DNS queries, DHCP, VoIP and video streaming, online games, SNMP and QUIC (the basis of HTTP/3, which builds reliability on top of UDP).
Table 3.4: TCP and UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Acknowledged, retransmitted | No guarantee |
| Ordering | In order | Not ordered |
| Header | 20–60 bytes | 8 bytes |
| Flow and congestion control | Yes | No |
| Uses | Web, email, file transfer | DNS, VoIP, streaming, games |
Example 3.4: Choosing a transport protocol
Problem: Choose TCP or UDP for: (a) downloading a PDF, (b) a live video call, (c) a DNS lookup, (d) sending an email.
Solution:
- (a) Every byte of the file must arrive correctly, so TCP.
- (b) Late packets are useless, and small losses are acceptable, so UDP.
- (c) A single small query and reply; resending is cheap, so UDP (port 53).
- (d) The message must arrive completely, so TCP (SMTP, port 25 or 587).
Result: TCP for (a) and (d); UDP for (b) and (c).
import socket, threading
srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
srv.bind(("127.0.0.1", 0))
port = srv.getsockname()[1]
def echo():
data, addr = srv.recvfrom(1024)
srv.sendto(data.upper(), addr)
threading.Thread(target=echo).start()
cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cli.sendto(b"hello over udp", ("127.0.0.1", port))
reply, _ = cli.recvfrom(1024)
print("sent: hello over udp | reply:", reply.decode())
print("TCP header min 20 B, UDP header 8 B")
Output
sent: hello over udp | reply: HELLO OVER UDP
TCP header min 20 B, UDP header 8 B
Common mistakes
- Thinking UDP is useless because it is unreliable. Many real-time services prefer it.
- Confusing IP addresses (which host) with port numbers (which process).
- Believing DNS always uses TCP. Ordinary queries use UDP.
Key takeaways
- The transport layer gives process-to-process delivery using port numbers.
- TCP: connection-oriented, reliable, ordered, flow and congestion control.
- UDP: connectionless, 8-byte header, fast, no guarantees.
- A socket pair (two IPs, two ports) identifies a connection.
Practice questions
- How long is the UDP header?
- Name two applications that use UDP.
- What range are well-known ports?
Answers to the practice questions
- 8 bytes.
- DNS and VoIP (also streaming, games, DHCP).
- 0 to 1023.
Sources
- Official BCA syllabus Computer Networks, course code 26BCA404DS02, Unit III
