Abstract


TCP is bidirectional

Both peers can send and receive data, which means each peer has a Sequence Number to track the bytes sent and an Acknowledgement Number to track the bytes received.

Get your hands dirty

Create a TCP Server on port 8080

Create a TCP Client

  • telnet 127.0.0.1 8080 (intall on mac brew install telnet) connects to localhost:8080 over TCP. A new socat process should be created

Create TCP Server in NodeJS

Sequence Number

  • Tracks what is sent
  • A measure of bytes

Initial Sequence Numbers (ISN)

The initial sequence numbers are randomly chosen by the sender. This information is shared between the peers during the TCP Connection.

TCP Retransmission Timeout

  • Sender caches a packet when he sends out it. If a TCP ACK isn’t received in a specific timeframe, the packet is resent
  • The sender caches a packet when it is sent. If an TCP ACK isn’t received within a specific timeframe, the packet is resent

What if the receiver receives the packet, but the ACK is lost?

A TCP retransmission timeout will be triggered, and the same packet with the same old sequence number will be resent. The receiver will recognise it as a duplicate, understand that the ACK didn’t reach the sender, and will simply resend the ACK.

Thus, this mechanism handles packet drops in either direction.

TCP Window Size

  • Also bytes in flight
  • Limits how much unacknowledged data can be sent before the sender needs to stop and wait for TCP ACK

Enables receiver to receive data at the optimal rate

The window size is sent with each segment, meaning it can be dynamically updated during the connection. This is used for flow control, where the sender can instruct the receiver to stop sending data by setting the window size to 0 in the TCP ACK. Later, the sender can send another TCP ACK, with a window size of 600, for example, to indicate that it is ready to receive up to 600 bytes of data.

TCP ACK


Acknowledgement Number

  • Tracks what is received
  • A measure of bytes

Important

The acknowledgment number (ACK) that the receiver sends back to the sender is the sequence number the sender will use in the next packet.

The ACK number indicates that all bytes before that number have been received. In other words, the receiver is ready to receive the next byte, and this byte should begin at the ACK number sent to the sender.

Phantom byte

The ACK number is used to inform the sender that the previous packet has been received. Even if the previous packet doesn’t contain any data, just the packet itself, the ACK number that the receiver sends will still increase by one.

This phantom byte is added for SYN and FIN, but not for TCP ACK.

The phantom byte is critical for sender to differentiate between resending an ACK and acknowledging a new SYN or FIN from the receiver.

TCP Delayed Acknowledgements

Important

The acknowledgements are cumulative. The ACK number in the TCP ACK is the sum of the base sequence number and the total bytes received in the two packets.

What if the sender only transmits one packet?

A TCP ACK will be sent after 500 ms if the receiver never receives the second packet.

TCP Slow Start


  • Only a small amount of data is transferred to test the water at the start of the communication. If data is delivered smoothly, TCP will gradually increase the amount of data

Avoid network congestion

If a particular network path is congested and we dump a lot of data to the data. The congested network path will result in a lot of packet loss, and this incurs extra time to resend the network packets

Initial lag

A slower startup speed means accessing new resources like a new web page will appear laggy

References