Sobes.tech
Junior

How does the TCP protocol work?

sobes.tech AI

Answer from AI

TCP (Transmission Control Protocol) is a transport layer protocol that provides reliable, connection-oriented data delivery.

Key aspects of operation:

  1. Three-way Handshake:

    • The client sends a SYN (Synchronize) packet with an initial sequence number (ISN).
    • The server receives the SYN, responds with a SYN-ACK (Synchronize-Acknowledge), containing its ISN and an acknowledgment (ACK) of the client's ISN (acknowledgement number = client ISN + 1).
    • The client receives the SYN-ACK, sends an ACK confirming the server's ISN (acknowledgement number = server ISN + 1).
    • The connection is established, and data transfer can begin.
  2. Stream Data Transfer: Data is transmitted as a continuous stream of bytes, not as separate packets. TCP segments the data stream into segments for transmission.

  3. Sequence Numbering: Each byte in the data stream has a unique number. Segments contain the sequence number of the first byte in them and an acknowledgment number indicating the next byte the sender expects to receive.

  4. Acknowledgements (ACKs): The receiver sends ACKs to inform the sender which data has been successfully received. If the sender does not receive an ACK within a certain timeout, it retransmits the segment.

  5. Error Control: Checksums are used to verify data integrity. If a segment is corrupted, it is discarded, and the sender waits for a timeout or duplicate ACK to retransmit.

  6. Flow Control: A mechanism to prevent receiver buffer overflow. The receiver informs the sender of its receive window size in ACK packets. The sender can only send as much data as the window allows.

  7. Congestion Control: A mechanism to determine available network capacity and dynamically adjust transmission speed to avoid network congestion. It includes algorithms like slow start, congestion avoidance, fast retransmit, and fast recovery.

  8. Connection Termination: Usually uses a four-way handshake:

    • One side sends a FIN (Finish) packet.
    • The other side responds with an ACK for the FIN.
    • The other side sends its own FIN after finishing its data transmission.
    • The first side responds with an ACK for this FIN.
    • The connection is closed.

TCP Segment Structure:

Field Size (bytes) Description
Source Port 2 Sender's port
Destination Port 2 Receiver's port
Sequence Number 4 Number of the first byte in the segment
Acknowledgement Number 4 Number of the next expected byte
Data Offset/Header Length 4 bits TCP header length
Reserved 6 bits Reserved
Flags (Control Bits) 6 bits URG, ACK, PSH, RST, SYN, FIN
Window Size 2 Receiver's window size
Checksum 2 Header and data checksum
Urgent Pointer 2 Points to urgent data if URG is set
Options Variable Additional options
Padding Variable For header alignment
Data Variable Transmitted data
// Control Bits Flags:
// URG: Urgent Pointer field significant
// ACK: Acknowledgement field significant
// PSH: Push function
// RST: Reset the connection
// SYN: Synchronize sequence numbers
// FIN: No more data from sender