TCP and the socket API
TCP presents a reliable, ordered, full-duplex byte stream between two endpoints. Flow control protects the receiver; congestion control protects the network. The application uses sockets, while sequence numbers and windows control delivery below that interface.
7.1 Bytes, sequence numbers and wraparound
Past papers · CS 2014–2026 (23 papers): 5 questions, 8 marks
A TCP connection is identified by source IP, source port, destination IP and destination port. Each direction has its own sequence space. Segments can arrive out of order, but TCP exposes data to the application in order; individual application writes do not define message boundaries.
| TCP field | Size or rule |
|---|---|
| Source and destination port | 16 bits each |
| Sequence and acknowledgement number | 32 bits each |
| Data offset | 4 bits, measured in four-byte words |
| Basic header; maximum header | 20 bytes; 60 bytes |
| Unscaled advertised window | 16 bits; maximum 65,535 bytes |
| MSS | Maximum TCP data bytes per segment; excludes TCP/IP headers |
| SACK option | Reports received noncontiguous byte blocks when negotiated |
ACK number is the next byte expected in sequence. A pure ACK consumes no sequence number. SYN and FIN each consume one. The ACK flag normally remains set on established-connection data; a segment can carry outgoing data and acknowledge reverse-direction data at the same time.
7.2 Establishment, close and restart
Past papers · CS 2014–2026 (23 papers): 6 questions, 7 marks
For initial sequence numbers at client and at server, a normal three-way handshake without data is:
| Step | Flags | Sequence | Acknowledgement |
|---|---|---|---|
| Client to server | SYN | Not valid (ACK flag zero) | |
| Server to client | SYN, ACK | ||
| Client to server | ACK |
The initial sequence number is selected independently of zero; exam models treat it as a random 32-bit starting value. With negligible segment transmission, the client's handshake completion takes one RTT; its last ACK can carry the first request. The server receives that ACK after another one-way propagation.
Handshake and ordinary active close; vertical direction represents increasing time.
| Active closer | Event | Next state |
|---|---|---|
| ESTABLISHED | Sends FIN | FIN-WAIT-1 |
| FIN-WAIT-1 | Own FIN acknowledged, peer FIN absent | FIN-WAIT-2 |
| FIN-WAIT-2 | Peer FIN received, ACK sent | TIME-WAIT |
| TIME-WAIT | Twice maximum segment lifetime elapses | CLOSED |
The passive closer goes through CLOSE-WAIT after receiving FIN, then LAST-ACK after its application closes and sends FIN. A FIN closes one direction; the other can still carry data. Either endpoint can close first, and simultaneous close is possible. ACK and FIN may be combined, so graceful close need not always use four separate packets. RST aborts a connection rather than performing graceful FIN exchange. State and restart details follow RFC 9293.
After a server crash/reboot, old connection state can be lost while a new listener binds the same service port. A client waiting without keepalive, application timeout or further traffic may wait indefinitely. A stale segment reaching a host without matching connection state can elicit RST; reboot does not itself send a graceful FIN to the old client.
7.3 Receiver window and sender allowance
Past papers · CS 2014–2026 (23 papers): 2 questions, 3 marks
The advertised receive window is available receive-buffer space. It can change as data arrives and the application consumes it. The congestion window is a separate sender limit. Here all windows and outstanding-data counts are in bytes, not segments.
A receiver's zero window invokes persist probing so a lost window update does not stall forever. Congestion timeout and zero-window probing solve different problems. A shrinking advertised window cannot recall data already sent; the simple allowance is recomputed for future transmissions. Under the usual fixed-window textbook model, outstanding bytes stay within the window.
7.4 Slow start, avoidance and loss response
Past papers · CS 2014–2026 (23 papers): 6 questions, 11 marks
Use the algorithm and initial window specified in the question. The classical GATE round model below starts at one MSS when no other initial value is supplied. This is an examination model, not a claim that every deployed TCP starts at one segment. Assume one new-data ACK per full segment, no delayed ACKs, ample receive window, and a continuously backlogged sender.
Delayed ACKs and partial segments can change per-RTT growth. At a non-power-of-two threshold, do not double past it mechanically; use the stated round model or trace per-ACK updates. Once rwnd limits flight, an unconstrained doubling model no longer predicts actual transmitted segments.
7.5 Retransmission timer
A retransmission timer must reflect both typical RTT and its variation. Keep the old smoothed estimate when computing the new deviation. Distinguish a textbook estimator from a specified implementation's minimum timer.
Karn's rule avoids ambiguous RTT samples from retransmitted segments unless a mechanism such as timestamps disambiguates them. A constant multiplier such as is a stipulated simple model, not the universal TCP timer rule.
7.6 TCP socket calls
Past papers · CS 2014–2026 (23 papers): 2 questions, 2 marks
| Call | Meaning for a TCP application |
|---|---|
socket(AF_INET, SOCK_STREAM, 0) | Create an IPv4 stream endpoint |
bind | Associate a local address and port |
listen | Make the bound stream endpoint passive and allow connection requests to queue |
connect | Initiate a connection to a remote endpoint; a client can receive an implicit local binding |
accept | Obtain a new connected descriptor from the listener's pending queue |
send / recv | Transfer bytes on the connected endpoint |
shutdown | Close a chosen communication direction |
close | Release a descriptor; final socket release participates in closing the connection |
An IPv4 socket address uses sockaddr_in: sin_family is AF_INET, with the IPv4 address and sin_port in network byte order (most significant byte first). Use htons/ntohs for 16-bit port values and htonl/ntohl for 32-bit integers; do not convert an address twice after an address-parsing routine has already returned network order. See IPv4 socket addressing and byte-order conversion.
The conventional server order is socket → bind → listen → accept → recv/send → close. The conventional client order is socket → connect → send/recv → close. Explicit client bind is optional when an automatically chosen local endpoint is acceptable. listen, not bind or accept, makes the endpoint passive. The listening descriptor remains available after accept; the returned descriptor serves one connected peer. See the Linux accept manual.
server:
listener = socket(...)
bind(listener, local_address)
listen(listener, backlog)
connection = accept(listener, ...)
exchange bytes using connection
close(connection)
continue accepting using listener
client:
connection = socket(...)
connect(connection, server_address)
exchange bytes using connection
close(connection)
This is call-order pseudocode, not a complete error-handling program. A send may accept fewer bytes than requested and does not prove remote application delivery. A blocking recv can return fewer bytes than requested; preserve leftovers and continue until the application-defined message is complete. For a positive receive-buffer length, a return of zero on a stream means orderly end-of-stream, not “temporarily no data”. A nonblocking call with no available data reports a would-block condition. See the Linux receive manual.
Last-minute recall
- TCP is a byte stream, not a message stream; both directions can carry data and ACKs.
- SYN and FIN consume sequence space; pure ACK does not.
- ACK is the next expected contiguous byte.
- Compute byte-counter wrap using bytes/s, not bits/s.
- New sending allowance subtracts outstanding bytes from the smaller window.
- Receiver flow control and network congestion control are separate limits.
- Slow start roughly doubles per RTT, not per ACK.
- State the initial window, threshold rule and round-zero convention.
- Timeout resets the classical congestion window; Reno duplicate-ACK recovery differs.
listencreates the passive role;acceptreturns a new connected socket.