TCP: The Trust-First Protocol
The TCP/IP model is a layered networking framework that explains how data is communicated between devices over a network using standardized protocols to ensure reliable and efficient transmission.
In networking, sending data without a predefined set of rules is known as "best-effort" delivery. Without a control protocol, packets can arrive out of order, become corrupted, or vanish entirely due to network congestion, leaving the receiving application with a fragmented and useless data stream.
TCP (Transmission Control Protocol) was designed to solve these exact problems.
Problems TCP Solves:
Packet Loss: Automatically detects and retransmits lost segments.
Out-of-Order Delivery: Reorganizes packets that arrive in the wrong sequence.
Data Integrity: Uses checksums to ensure data hasn't been corrupted during transit.
Flow Control: Prevents a fast sender from overwhelming a slow receiver.
The 3-Way Handshake during Connection
Before data transfer begins, the client and server must confirm they are synchronized and ready to communicate. This is achieved through the 3-Way Handshake.
SYN (Synchronize)
In the first step, the client wants to establish a connection with a server, so it sends a segment with SYN(Synchronize Sequence Number) flag enabled which informs the server that the client is likely to start communication. It communicates it’s Initial Sequence Number (ISN) in this step .
SYN-ACK (Synchronize-Acknowledgment)
The server receives the SYN segment and responds with a segment where both the SYN and ACK flags are enabled. The ACK acknowledges the receipt of the client's SYN (the acknowledgment number is the client’s ISN + 1).The SYN signals the server’s own intent to communicate and includes the server's own Initial Sequence Number.
ACK (Acknowledge)
The client receives the SYN-ACK and sends a final segment with only the ACK flag enabled. They both establish a reliable connection with which they will start the actual data transfer.

This process proves that the path from Client to Server and the path from Server to Client are both functional.
Handling packet loss and retransmission
The Acknowledgment (ACK) Mechanism
The foundation of TCP reliability is the Acknowledgment. When a receiver gets a segment of data, it is required to send back a small packet called an ACK.
This ACK specifies the next sequence number the receiver is expecting. If the sender transmits segments 1, 2, and 3, but only receives an ACK for segment 2, it knows segment 3 may be in trouble.
Retransmission Timeout (RTO)
The primary way TCP handles loss is through a timer.
Every time a sender transmits a segment, it starts a Retransmission Timer.
If an ACK for that segment is received before the timer expires, the timer is cancelled.
If the timer runs out (Timeout) and no ACK has arrived, the sender assumes the packet was lost and immediately retransmits the data.
Flow and Congestion Control
TCP doesn't just resend data; it tries to learn why the loss happened. If loss occurs frequently, TCP assumes the network is congested. It will then:
Reduce the Window Size: It sends fewer packets at once to avoid overwhelming the network.
Slow Start: It gradually increases the transmission speed again only once the network proves it can handle the load.
The 4-Way Handshake during Goodbye
In networking, simply cutting a connection is like hanging up a phone mid-sentence—data could be lost. To prevent this, TCP uses a 4-Way Handshake to terminate a connection.
FIN (Client to Server)
When the client has no more data to send, it sends a segment with the FIN flag set. Think of this as the client saying, "I'm done sending my part, but I'm still listening if you have more to say."
ACK (Server to Client)
The server receives the FIN and immediately sends back an ACK. At this point, the connection is in a "half-close" state. The client can no longer send data, but the server might still be finishing up a data transfer.
FIN (Server to Client)
Once the server finishes sending its remaining data, it sends its own FIN segment to the client, signalling, "Okay, I'm done on my end too."
ACK (Client to Server)
The client receives the server’s FIN and responds with a final ACK. This confirms the server can safely close the connection.
