0
0
Computer Networksknowledge~10 mins

TCP connection termination (four-way handshake) in Computer Networks - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - TCP connection termination (four-way handshake)
Client sends FIN
Server receives FIN
Server sends ACK
Server sends FIN
Client receives FIN
Client sends ACK
Connection closed
The TCP connection closes in four steps: client sends FIN, server acknowledges and sends its own FIN, client acknowledges, then connection closes.
Execution Sample
Computer Networks
Client -> Server: FIN
Server -> Client: ACK
Server -> Client: FIN
Client -> Server: ACK
Shows the four messages exchanged to close a TCP connection gracefully.
Analysis Table
StepActionSenderReceiverMessageConnection State
1Send FINClientServerFINClient: FIN sent, waiting for ACK
2Receive FINServerServerFIN receivedServer: FIN received, send ACK
3Send ACKServerClientACKServer: ACK sent, send own FIN
4Send FINServerClientFINServer: FIN sent, waiting for ACK
5Receive FINClientClientFIN receivedClient: FIN received, send ACK
6Send ACKClientServerACKClient: ACK sent, connection closing
7Connection closedBothBothNoneConnection fully closed
💡 Connection closes after client sends final ACK and both sides confirm closure.
State Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6Final
Client StateESTABLISHEDFIN_WAIT_1FIN_WAIT_1FIN_WAIT_1TIME_WAITCLOSED
Server StateESTABLISHEDESTABLISHEDCLOSE_WAITLAST_ACKLAST_ACKCLOSED
Key Insights - 3 Insights
Why does TCP use four steps instead of just two to close a connection?
Because each side must close its own sending channel separately, the client first tells the server it finished sending (FIN), the server acknowledges (ACK), then the server tells the client it finished sending (FIN), and finally the client acknowledges (ACK). This is shown in steps 1-6 in the execution_table.
What happens if the final ACK from the client is lost?
The server will retransmit its FIN until it receives the ACK, ensuring reliable connection closure. This is implied by the waiting states after steps 4 and 6 in the execution_table.
Can data still be sent after the client sends the first FIN?
Yes, because the connection is half-closed; the server can still send data until it sends its own FIN. This is shown by the server's state remaining ESTABLISHED after step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Client State after step 1?
AFIN_WAIT_1
BESTABLISHED
CACK_SENT
DCLOSED
💡 Hint
Check the variable_tracker row for Client State after Step 1.
At which step does the Server send its own FIN message?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Message' column in execution_table for when Server sends FIN.
If the client never sends the final ACK, what will the server do according to the process?
AClose connection immediately
BRetransmit FIN until ACK received
CSend another FIN
DIgnore and wait forever
💡 Hint
Refer to key_moments explanation about lost final ACK and server behavior.
Concept Snapshot
TCP connection termination uses a four-way handshake:
1. Client sends FIN to close its sending side.
2. Server acknowledges with ACK.
3. Server sends its own FIN to close its sending side.
4. Client acknowledges with ACK.
Each side closes independently, ensuring reliable shutdown.
Full Transcript
TCP connection termination happens in four steps called the four-way handshake. First, the client sends a FIN message to tell the server it wants to stop sending data. The server receives this FIN and replies with an ACK to confirm. Then the server sends its own FIN to tell the client it also wants to stop sending. Finally, the client sends an ACK back to the server. After these four messages, the connection is fully closed. Each side closes its sending channel separately, allowing any remaining data to be sent before closing. If any message is lost, TCP will retransmit to ensure reliable closure.