参考回答
Before getting into the detailed answer, first remember these 3 words: SYN, SYN-ACK, ACK.
Talking about TCP. Before any data is sent ove to TCP, the client and server are expected to make sure that the connection is reliable.
In this case, TCP uses a three-way handshake method where it establishes a connection where both sides are ready to send and receive data.
Here's how it goes:
1. A client, say your browser wants to connect to a server. It starts by sending a SYN packet. Along with this, it includes an initial sequence number basically saying, “I want to start a connection, and here's where my data numbering begins.”
2. The server receives this and responds with a SYN-ACK. Two things take place here, it acknowledges the client's sequence number and also sends its own sequence number back.
3. Now the client sends a final ACK, confirming that it received the server's sequence number.
4. At this point, the connection is established, and data transfer can begin.
You might wonder why 3 steps are required here instead of 2
And it is because both sides need to confirm two things, and that is if they can send and receive.
With only two steps, the server wouldn't know if the client actually received its response.
Once communication is done, the connection is closed using a four-step process, i.e, FIN - ACK - FIN - ACK, which is slightly more involved.
Here's what you might get asked during the interviews for a follow-up:
Q. What happens if the SYN-ACK is lost?
Your Ans: The client waits for a timeout and then retransmits the SYN packet.
Q. What is a SYN flood attack?
Your Ans: It's when an attacker sends a large number of SYN requests but never completes the handshake. This leaves connections half-open and can exhaust server resources.