TCP connections and byte streams
Why TCP is a stream
TCP lets two endpoints exchange an ordered stream of bytes in both directions. Imagine sending a short document to a server: the receiving application needs the bytes in the original order, even if the network delays or loses a packet. TCP handles sequencing and recovery while the connection remains viable. It does not promise that a connection will survive every failure, or that the receiving application has saved the document.
Opening a connection
A normal client-initiated handshake synchronizes the starting sequence numbers. In this fictional trace A sends SYN at 1200, B returns SYN and ACK with its own start at 7000, and A acknowledges B. SYN occupies one sequence position; a plain ACK without data does not. The first data from A therefore starts at 1201. Real initial numbers vary; this is a paper trace, not captured traffic.
A -> B SYN seq=1200B -> A SYN,ACK seq=7000 ack=1201A -> B ACK seq=1201 ack=7001Sequence numbers and recovering gaps
A sequence number identifies a position in one direction of the stream. An acknowledgment (ACK) gives the next contiguous position expected, not a packet count. Suppose A sends four bytes beginning at 1201, then two at 1205. If the first part is missing, B cannot advance its cumulative acknowledgment beyond 1201. Assume B buffers the later bytes. Once the missing four arrive again, B can acknowledge 1207. TCP can retransmit after a timer or other loss signals; duplicate data is not delivered twice to the application. This simplified example omits wraparound, delayed ACKs and selective acknowledgments.
1201..1204 missing1205..1206 bufferedACK 12011201..1204 retransmittedACK 1207Receiver capacity and network capacity
The advertised receive window limits how much additional sequence space the receiver can accept. A separate congestion window at the sender helps avoid overloading the path. In a simplified case with no outstanding data, a receive window of 3000 bytes and congestion window of 2000 bytes allow at most 2000 bytes in flight initially, before other constraints. Neither number is the link speed. TCP normally has a header of at least 20 bytes; options increase this size.
Message boundaries and closing
Two application writes containing AB and CD may be read as ABCD, A then BCD, or other chunks that preserve the same byte order. The application must define its own message format (for example a length followed by that many bytes). FIN signals the end of one sending direction and uses one sequence position; the other direction can still carry data. An acceptable RST can abort a connection. A successful handshake or ACK is not proof of application success. Practice: calculate the next ACK after a three-byte segment starting at 900 and a following FIN; the values are 903 and 904.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TCP connections and byte streams — knowledge check
Interpret fictional transport observations and calculate expected results.
A sends SYN with seq=300. What ACK should B send for this SYN?
Six contiguous bytes start at seq=901. What cumulative ACK follows?
A program writes AB and then CD on TCP. Which read result can preserve the stream?
No data is outstanding. The receive window is 3000 B and congestion window is 1200 B. What is the simplified upper bound for initial in-flight bytes?
A sends a FIN after its last data. What can still occur during orderly shutdown?