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.

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:


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:

ACK / SYNC
TCP needs ACK and SYNC. The steps are:

Packet Header

Source/Destination Port: source and the destination port number (not the ip address)- A
socket = ip + port
- A
Sequence number: 32 bits keep track of the order of the packetAcknowledgement number: 32 bits of the next sequence number should expect- If the
Acknowledgement number != next sequence numberthen the packet is marked as lost
- If the
HLEN (Header Length): 4 bits identify the length of the header. This is necessary because the length ofOptionsis undefined and can be varied.Reserved: Always set to 0Flags: 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)
- The flags are:
window size: 16 bit field for flow control. How many bytes sender is allowed to transmit without receiving an acknowledgementChecksum: 16 bits for errordetectionUrgent pointer: only use whenURGflag is set. This 16 bit is added to the end for urgent dataOptions: 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)