/ Foundations / Core Concepts / 3 Way Handshake.md
3 Way Handshake.md
 1  ## Description
 2  
 3  The **TCP three-way handshake** is a critical process used to establish a reliable connection between a client and a server before data transmission begins. It involves three main steps: **SYN**, **SYN-ACK**, and **ACK**. Additionally, I'll explain the **RETransmissions (RET)** that may occur during this process. Here’s a detailed overview:
 4  
 5  ## Steps of the TCP Three-Way Handshake
 6  
 7  ### Step 1: Initial Connection Request (SYN)
 8  - The **client** initiates the connection by sending a [[TCP]] segment with the **SYN** (synchronize) flag set to the **server**.
 9  - This segment includes:
10    - A randomly chosen **initial sequence number (ISN)**, which will be used to track the order of packets during the session.
11    - Other optional TCP parameters, such as the maximum segment size.
12  
13  ### Step 2: Server Response (SYN-ACK)
14  - Upon receiving the SYN segment, the **server** acknowledges the request and allocates resources for the connection.
15  - The server responds by sending a [[TCP]] segment with both the **SYN** and **ACK** (acknowledge) flags set. This segment includes:
16    - The server's own ISN.
17    - An acknowledgment number that is one greater than the client's ISN, indicating that it has received the client's request.
18  
19  ### Step 3: Client Acknowledgment (ACK)
20  - After receiving the SYN-ACK segment from the server, the **client** sends an **ACK** segment back to the server.
21  - This ACK segment acknowledges receipt of the server's SYN by including:
22    - An acknowledgment number that is one greater than the server's ISN.
23  - At this point, both parties have successfully exchanged their initial sequence numbers.
24  
25  ## Connection Establishment
26  With these three steps completed, a reliable connection is established between the client and server. They can now begin transmitting data using synchronized sequence numbers, ensuring that packets are sent and received in order without loss or duplication.
27  
28  ## Importance of Each Step
29  
30  1. **SYN (Step 1)**: 
31     - Initiates the connection and informs the server that a client wants to communicate.
32     - The initial sequence number is crucial for tracking data segments.
33  
34  2. **SYN-ACK (Step 2)**:
35     - Confirms receipt of the client's SYN and provides its own sequence number.
36     - Indicates that the server is ready to accept connections.
37  
38  3. **ACK (Step 3)**:
39     - Acknowledges receipt of the server's SYN, completing the handshake process.
40     - Signals readiness for data transmission from both parties.
41  
42  ## Retransmissions (RET)
43  
44  ### What is RET?
45  Retransmissions occur when a packet is not acknowledged within a certain timeframe. In TCP, if either party does not receive an expected packet (such as a SYN or ACK), it will retransmit that packet after a timeout period. This mechanism ensures reliability in data transmission.
46  
47  ### When RET Happens in Three-Way Handshake
48  1. **SYN Retransmission**: If the client sends a SYN but does not receive a SYN-ACK from the server within a specified timeout period, it will retransmit its SYN packet.
49    
50  2. **SYN-ACK Retransmission**: If the server sends a SYN-ACK but does not receive an ACK from the client within its timeout period, it will retransmit its SYN-ACK packet.
51  
52  3. **ACK Retransmission**: If either party does not receive an expected acknowledgment for any packet sent during this process, it will retransmit that packet to ensure that communication can proceed.
53  
54  ### Importance of RET
55  - **Reliability**: Retransmissions ensure that lost packets do not disrupt communication, allowing connections to be established even in unreliable network conditions.
56  - **Error Recovery**: By retransmitting packets, [[TCP]] can recover from lost or corrupted segments, maintaining data integrity throughout communication.
57  
58  ## Conclusion
59  The TCP three-way handshake is essential for establishing secure and efficient communication over networks. By ensuring that both ends of a connection are synchronized and prepared for data transfer, it forms the foundation of reliable data transmission in TCP/IP protocols. Understanding this process, including potential retransmissions, is crucial for network administrators and developers working with networked applications. This knowledge helps diagnose connectivity issues and optimize network performance while ensuring robust communication protocols are in place.