TCP From Scratch

Concept of Sequence Number & Acknowledgement


📑 On this page
  1. Sequence Number
  2. TCP Length Field
  3. Acknowledgements
  4. TCP Header
  5. Two Data Streams
📚 TCP Connection EstablishmentPart 7 of 7
  1. 1. Why TCP?
  2. 2. How People and Computers communicate
  3. 3. TCP Terminology
  4. 4. How Port Numbers work
  5. 5. Problems a missing Packet cause
  6. 6. Purpose of Connection Establishment
  7. 7. Concept of Sequence Number & Acknowledgement

Sequence Number

  1. If Alice wants to communicate with David, a TCP connection should be established. She can use any sequence number but lets start off with 1.  The transport Layer receives information from Application layer and segments these into TCP Segments which are encapsulated into IP packets.
  2. Then David is expecting packet 2,3,4. Lets say he has only received 1,3 and 4. Transport Layer on his NIC card is intelligent to understand that he is missing a packet. The reason it knows this is when it receives the IP packet, it de-encapsulates it, it sees Packet 1 sequence 1, all good, sees packet 2 sequence 3, packet 3 sequence 4. But it knows that it is missing a sequence 2.

From Application to Transport layer, this is a continuous stream of 1's and 0's, which transport layer makes into smaller chunks of TCP segments. TCP Segment includes port numbers and sequence numbers. However, every single BYTE of data is marked with a number.

So for example if we are receiving 10 bytes of data from the Application Layer, 1 Byte = 8 bits, so 8x10 = 80 bits. THOSE BYTES are what are actually marked with a number. The below diagram shows this:

TCP Length Field

Within the TCP Header there is a field called the Length Field. This field says how much BYTES of data in this segment from the Application Layer. So if this field is 10, it means it has received 10 bytes of data from the Application Layer and put into the TCP Segment at the Transport Layer.

After the Length Field, is the Sequence Field. Here is where the actual sequencing happens. This TCP Segment will have a sequence number of the first sequence number it used to mark a particular byte of data. In this example it was 1. So this packet sequence will be 1.

For the second TCP Segment, if you know the previous packet's sequence number and length field, you can calculate what the second TCP segment sequence and length field will be.

This Length Field is the maximum. So for example say Byte 11, 12, 13 etc comes from the Application Layer, a new TCP Segment must be created. Byte 11 will be marked with 11, Byte 12 with marked 12, 13 byte marked with 13. This is the only continuous data we have received and the Transport Layer puts 3 as the Maximum in its Length Field in the TCP Segment Field. The sequence number of this TCP Segment then becomes 11 as that is the first byte of this new stream of data.

Tip

Sequence Number + Length = Sequence number of the NEXT packet.

Every single Byte received from Application Layer is Marked with a number.

Acknowledgements

  1. Here we can see Alice has sent a packet that contains a TCP Segment with a sequence of 1 and length of 100. As we know the next sequence number will be 1+100 =101.
  2. Then we can see David is sending back another packet that has a TCP Segment that says Acknowledgement 101. This is basically saying David has received information up to 100. Sequence 100 is the 100th byte in the first TCP packet. This Ack is basically David saying he is then expecting a next packet starting with Sequence 101.
  3. Then when Alice receives the Ack, it will then send another packet that has a sequence of 101 and length of 200 of data.
  4. David receives this and says it has receives 300 bytes of Data and is expecting the next packet with a TCP Segment sequence number of 301.

TCP Header

So far we have the below in the TCP Header:

Two Data Streams

Here we have two TCP Data steams. One from Alice to David and One from David to Alice at the same time. Imagine this is a phone call,

  1. Alice is asking if David is there, and David is acknowledging he is there. This is the First Data Stream
  2. Then there's another data stream but David is the one asking Alice if she is there, and Alice acknowledging that she is there.

These are two completely separate data streams. Blue stream is number 1, and yellow stream is number 2. Completely separate. This is the Client to Server (Stream 1) and Server to Client (Stream 2)

Comments