Sub topic 5
Opening and Closing a TCP Connection
Prerequisites
Assume that you know the layered network model, IP addresses, ports, bytes, and round-trip time (RTT). You should also know that TCP provides a reliable, ordered byte stream between two application processes.
A TCP connection is identified by a four-tuple:
| Field | Meaning |
|---|---|
| Source IP and source port | The client endpoint |
| Destination IP and destination port | The server endpoint |
A server usually binds to a well-known port and listens. A client chooses a temporary port and connects to the server. The operating system performs the TCP handshake before the application sends normal data.
The three-way handshake
Each TCP endpoint chooses an initial sequence number (ISN). Sequence numbers label bytes in the stream. The acknowledgement number means “the next sequence number I expect.”
The steps are:
- The client sends a segment with the SYN flag set and sequence number
x. - The server sends SYN and ACK. Its own sequence number is
y, andack = x + 1confirms the client’s SYN. - The client sends an ACK with
ack = y + 1.
SYN consumes one sequence number even though it carries no application data. After this exchange, both sides know that the other endpoint is reachable and know the starting sequence space.
A typical socket call sequence is:
- Server:
socket → bind → listen → accept - Client:
socket → connect
accept returns a new connected socket. The listening socket remains available for later clients.
>callout note > TCP establishes a byte stream, not a message stream. If an application sends two messages, one recv call may return part of one message, one complete message, or bytes from both. The application must define its own message boundaries.
Sequence and acknowledgement example
Suppose the client sends 500 bytes beginning at sequence number 1001. The server acknowledges with:
ACK = 1501This means that bytes through sequence number 1500 arrived correctly, and byte 1501 is the next expected byte. If the server receives a segment out of order, it may repeat an acknowledgement for the missing byte. Such duplicate ACKs can help the sender detect loss.
Closing a connection
TCP normally closes each direction independently. Therefore, termination commonly uses four segments:
- The client sends
FIN, indicating that it will send no more bytes. - The server sends
ACK. - After finishing its remaining data, the server sends
FIN. - The client sends the final
ACK.
FIN also consumes one sequence number. The endpoint that sends the final ACK usually enters a waiting state called TIME_WAIT. This prevents delayed duplicate segments from being confused with a later connection using the same four-tuple.
A connection can be half-closed. For example, a client may send FIN but continue receiving data from the server. An abrupt failure or an invalid segment may instead cause a reset, marked by the RST flag.
Exercise
A client sends SYN with sequence number 4000. The server replies with SYN-ACK using sequence number 9000. What acknowledgement numbers should appear in the server's SYN-ACK and the client's final ACK?
Show answer
The server's SYN-ACK has ACK = 4001. The client's final ACK has ACK = 9001 because SYN consumes one sequence number at each endpoint.