TCP (Transmission Control Protocol)

TCP and UDP (User Diagram Protocol) lives in layer 4 in OSI Layers.

TCP splits the data into small managable segment.

Pasted image 20230901093114.png

Each segment is marked as a sequence number. When the other device (Device B) receives these segments of data. It will sort these segments based on the sequence number:

Pasted image 20230901093200.png

Pasted image 20230901093217.png

TCP also provides error checking to make sure that the data is not corrupted.

Note:

TCP does not preseve packet boundaries. Which means by default, it automatically cut the packet into chunks no matters how big it is. For example:

Pasted image 20240912223547.png

ACK / SYNC

TCP needs ACK and SYNC. The steps are:

Pasted image 20240427125146.png

Packet Header

Pasted image 20240919203005.png

  • Source/Destination Port: source and the destination port number (not the ip address)
    • A socket = ip + port
  • Sequence number: 32 bits keep track of the order of the packet
  • Acknowledgement number: 32 bits of the next sequence number should expect
    • If the Acknowledgement number != next sequence number then the packet is marked as lost
  • HLEN (Header Length): 4 bits identify the length of the header. This is necessary because the length of Options is undefined and can be varied.
  • Reserved: Always set to 0
  • Flags: 8 bit flags are used for data flow and connection control
    • The flags are:
      • Congestion Window Reduced (CWR)
      • ECN (Echo ECE)
      • Urgent (URG)
      • Acknowledgement (ACK)
      • Push (PSH)
      • Reset (RST)
      • Synchronize (SYN)
      • Final (FIN)
  • window size: 16 bit field for flow control. How many bytes sender is allowed to transmit without receiving an acknowledgement
  • Checksum: 16 bits for errordetection
  • Urgent pointer: only use when URG flag is set. This 16 bit is added to the end for urgent data
  • Options: options required by the senders process. Normally use to specify Maximum Segment Size — informs the receiver of the largest segment the sender is willing to accept

TCP Cycle example

Note that for each connection here we send a packet

sequenceDiagram
    participant Client
    participant Switch
    participant Server

    Note over Client, Server: PHASE 1: THE THREE-WAY HANDSHAKE (No Data)

    Client->>Server: [Eth][IP][TCP: SYN]
    Note right of Client: Seq=0, Ack=0, Flags=SYN
    
    Server->>Client: [Eth][IP][TCP: SYN-ACK]
    Note left of Server: Seq=0, Ack=1, Flags=SYN,ACK

    Client->>Server: [Eth][IP][TCP: ACK]
    Note right of Client: Seq=1, Ack=1, Flags=ACK

    Note over Client, Server: PHASE 2: DATA TRANSFER (The "Pipe" is Open)

    Client->>Server: [Eth][IP][TCP: ACK, PSH][DATA]
    Note right of Client: Seq=1, Ack=1, Flags=ACK,PSH

    Note over Switch: CONGESTION! Switch marks IP Header (ECN=11)

    Server->>Client: [Eth][IP: ECN=11][TCP: ACK, PSH][DATA]
    Note left of Server: Seq=1, Ack=101, Flags=ACK,PSH

    Client->>Server: [Eth][IP][TCP: ACK, ECE]
    Note right of Client: Seq=101, Ack=201, Flags=ACK,ECE <br/>(Client echoes: "I saw congestion!")

    Server->>Client: [Eth][IP][TCP: ACK, CWR, PSH][DATA]
    Note left of Server: Flags=ACK,CWR,PSH <br/>(Server: "I've slowed down, stop ECE")

    Client->>Server: [Eth][IP][TCP: ACK, PSH][DATA]
    Note right of Client: Flags=ACK,PSH <br/>(Back to normal transfer)

    Note over Client, Server: PHASE 3: CLOSING THE CONNECTION

    Client->>Server: [Eth][IP][TCP: FIN, ACK]
    Note right of Client: Flags=FIN,ACK

    Server->>Client: [Eth][IP][TCP: ACK]
    Note left of Server: Flags=ACK (Acknowledging FIN)

    Server->>Client: [Eth][IP][TCP: FIN, ACK]
    Note left of Server: Flags=FIN,ACK (Server closing too)

    Client->>Server: [Eth][IP][TCP: ACK]
    Note right of Client: Flags=ACK (Connection Closed)