Sub topic 3
Connectionless versus connection-oriented delivery
This chapter assumes you know the layered network model (application, transport, network, link), what an IP address and a packet are, and basic Python. It explains the two delivery models that TCP and UDP represent. Later chapters cover the headers and mechanisms in detail.
The problem both models solve
IP is best-effort. It moves packets from one host to another, but a packet can be lost, duplicated, delayed, or arrive out of order. IP gives no warning when this happens. The transport layer decides how much of this the application sees.
UDP and TCP make opposite choices. UDP passes IP's behavior through with ports added. TCP adds state and rules on top of IP to hide most of the problems.
Connectionless delivery
In a connectionless model, each message stands alone. The sender addresses it and sends it. No setup happens first, and the receiver does not need to agree in advance.
UDP works this way. Each sendto call produces one datagram. The network treats it independently of every other datagram, even between the same two hosts.
- No handshake, so the first byte goes out immediately.
- No per-peer state kept by the protocol. One socket can talk to many peers.
- No delivery report. A lost datagram is simply gone.
- Message boundaries are kept. One send is one receive.
Connection-oriented delivery
In a connection-oriented model, the two ends first set up shared state. Both sides then know the other exists and track what has been sent and received.
TCP works this way. A handshake establishes the connection. After that, both ends keep counters, buffers, and timers for it. The application sees a single ordered stream of bytes.
- Setup costs a round trip before data flows.
- Each connection holds state in both endpoints.
- Lost data is detected and resent.
- Data arrives in order, without duplicates, or the connection reports an error.
- A connection joins exactly two endpoints.
Comparing the two
| Property | Connectionless (UDP) | Connection-oriented (TCP) |
|---|---|---|
| Setup before data | None | Handshake |
| State in the endpoints | Almost none | Per-connection state |
| Delivery guarantee | None | Retransmission until acknowledged |
| Ordering | Not guaranteed | In order |
| Message boundaries | Preserved | Not preserved (byte stream) |
| Peers per socket | Many | One per connection |
The key point is that a "connection" is not a physical circuit. Routers in between do not know about it. The connection exists only as state in the two end hosts, and packets still travel over best-effort IP.
Seeing the difference in code
The UDP client needs no connection, but it must handle silence itself:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as cli:
cli.settimeout(2)
cli.sendto(b"ping", ("127.0.0.1", 9001))
try:
reply, _ = cli.recvfrom(2048)
print(reply)
except socket.timeout:
print("no reply")If no server listens on port 9001, this prints no reply after two seconds. Nothing fails at send time. A TCP client in the same situation fails immediately, because connect cannot complete the handshake:
import socket
try:
socket.create_connection(("127.0.0.1", 9001), timeout=2)
except ConnectionRefusedError:
print("connection refused")That error is a benefit of connection state. TCP tells you the peer is not there. UDP cannot.
Common errors
- Assuming a UDP send that returns without error means the data arrived. It only means the datagram was handed to the network.
- Assuming one TCP
sendmatches onerecv. TCP has no message boundaries, so the receiver may see the data split or merged. - Using UDP and then rebuilding ordering, retransmission, and duplicate removal by hand, which repeats TCP's work.
Exercise
A client sends one datagram over UDP to a port where nothing is listening, then waits for a reply. A second client tries to open a TCP connection to the same port. Describe what each client observes and why they differ.
Show answer
The UDP client sees nothing at send time and, without a timeout, would wait indefinitely. It cannot tell a lost datagram from a missing server because UDP keeps no connection state and has no delivery report. The TCP client fails right away with a "connection refused" error. Its SYN gets a reset from the host, because the handshake is part of the protocol and the endpoints track the outcome.