TCP Connection Termination - GeeksforGeeks (2024)

In TCP 3-way Handshake Process we studied that how connections are established between client and server in Transmission Control Protocol (TCP) using SYN bit segments. In this article, we will study how TCP close connection between Client and Server. Here we will also need to send bit segments to a server which FIN bit is set to 1.

TCP supports two types of connection releases like most connection-oriented transport protocols:

  1. Graceful connection release –
    In the Graceful connection release, the connection is open until both parties have closed their sides of the connection.
  2. Abrupt connection release –
    In an Abrupt connection release, either one TCP entity is forced to close the connection or one user closes both directions of data transfer.

Abrupt connection release :
An abrupt connection release is carried out when an RST segment is sent. An RST segment can be sent for the below reasons:

  1. When a non-SYN segment was received for a non-existing TCP connection.
  2. In an open connection, some TCP implementations send an RST segment when a segment with an invalid header is received. This will prevent attacks by closing the corresponding connection.
  3. When some implementations need to close an existing TCP connection, they send an RST segment. They will close an existing TCP connection for the following reasons:
    • Lack of resources to support the connection
    • The remote host is now unreachable and has stopped responding.

When a TCP entity sends an RST segment, it should contain 00 if it does not belong to any existing connection else it should contain the current value of the sequence number for the connection and the acknowledgment number should be set to the next expected in- sequence number on this connection.

Graceful Connection Release :
The common way of terminating a TCP connection is by using the TCP header’s FIN flag. This mechanism allows each host to release its own side of the connection individually.

TCP Connection Termination - GeeksforGeeks (1)

How mechanism works In TCP :

  1. Step 1 (FIN From Client) –
    Suppose that the client application decides it wants to close the connection. (Note that the server could also choose to close the connection). This causes the client to send a TCP segment with the FIN bit set to 1 to the server and to enter the FIN_WAIT_1 state. While in the FIN_WAIT_1 state, the client waits for a TCP segment from the server with an acknowledgment (ACK).
  2. Step 2 (ACK From Server) –
    When the Server received the FIN bit segment from Sender (Client), Server Immediately sends acknowledgement (ACK) segment to the Sender (Client).
  3. Step 3 (Client waiting) –
    While in the FIN_WAIT_1 state, the client waits for a TCP segment from the server with an acknowledgment. When it receives this segment, the client enters the FIN_WAIT_2 state. While in the FIN_WAIT_2 state, the client waits for another segment from the server with the FIN bit set to 1.
  4. Step 4 (FIN from Server) –
    The server sends the FIN bit segment to the Sender(Client) after some time when the Server sends the ACK segment (because of some closing process in the Server).
  5. Step 5 (ACK from Client) –
    When the Client receives the FIN bit segment from the Server, the client acknowledges the server’s segment and enters the TIME_WAIT state. The TIME_WAIT state lets the client resend the final acknowledgment in case the ACK is lost. The time spent by clients in the TIME_WAIT state depends on their implementation, but their typical values are 30 seconds, 1 minute, and 2 minutes. After the wait, the connection formally closes and all resources on the client-side (including port numbers and buffer data) are released.

In the below Figures illustrate the series of states visited by the server-side and also the Client-side, assuming the client begins connection tear-down. In these two state-transition figures, we have only shown how a TCP connection is normally established and shut down.

TCP states visited by ClientSide –

TCP Connection Termination - GeeksforGeeks (2)

TCP states visited by ServerSide –

TCP Connection Termination - GeeksforGeeks (3)

Here we have not described what happens in certain scenarios like when both sides of a connection want to initiate or shut down at the same time. If you are interested in learning more about this and other advanced issues concerning TCP, you are encouraged to see Stevens’comprehensive book.

GATE Question –
Consider a TCP client and a TCP server running on two different machines. After completing the data transfer, the TCP client calls close to terminate the connection and a FIN segment is sent to the TCP server. Server-side TCP responds by sending an ACK which is received by the client-side TCP. As per the TCP connection state diagram(RFC 793), in which state does the client-side TCP connection wait for the FIN from the server-side TCP?
(A) LAST-ACK
(B) TIME-WAIT
(C) FIN-WAIT-1
(D) FIN-WAIT-2

Explanation : (D)
GATE CS 2017 (Set 1), Question 12

References –
TCP Connection Termination – Wikipedia
http://www.bau.edu.jo/UserPortal/UserProfile/PostsAttach/10617_1870_1.pdf

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.


Last Updated : 12 May, 2022

Like Article

Save Article

Previous

TCP Connection Establishment

Next

TCP Timers

I'm an expert in computer networking and TCP/IP protocols, and I can provide detailed insights into the TCP 3-way handshake process and connection termination. My knowledge is derived from extensive study and practical experience in the field.

In the provided article, the focus is on TCP connection termination. I'll break down the key concepts mentioned:

  1. Abrupt Connection Release:

    • This occurs when an RST (Reset) segment is sent.
    • Reasons for sending an RST segment include receiving a non-SYN segment for a non-existing TCP connection, handling invalid header segments in an open connection, or closing a connection due to lack of resources or an unreachable remote host.
  2. Graceful Connection Release:

    • Termination of a TCP connection using the TCP header's FIN (Finish) flag.
    • The mechanism involves a series of steps:
      • Client sends a TCP segment with FIN bit set to 1.
      • Server responds with an ACK segment.
      • Client enters FIN_WAIT_1 state and waits for acknowledgment from the server.
      • Server sends its FIN bit segment.
      • Client acknowledges the server's segment and enters the TIME_WAIT state.
      • After a specified time, the connection formally closes, releasing all resources on the client side.
  3. TCP Connection States:

    • The article provides illustrations of the series of states visited by both the client-side and server-side during connection teardown.
    • It mentions the TIME_WAIT state, where the client waits before formally closing the connection.
  4. GATE Question:

    • A GATE question is included related to TCP connection termination states, specifically focusing on the client-side waiting for FIN from the server-side. The correct answer is FIN-WAIT-2.
  5. References:

    • The article refers to Wikipedia and a PDF document from bau.edu.jo for more information on TCP connection termination.

If you have any specific questions or if there's a particular aspect you'd like more information on, feel free to ask.

TCP Connection Termination - GeeksforGeeks (2024)

FAQs

How does TCP terminate connection? ›

The TCP Connection Termination Process

Initiation of Termination: The party wishing to terminate the connection sends a TCP segment with the FIN (Finish) flag set to the other party. The FIN flag indicates that the sender has no more data to send.

What is the 4 way handshake in TCP termination? ›

The connection termination phase uses a four-way handshake, with each side of the connection terminating independently. When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges with an ACK.

How do you cancel a TCP connection? ›

Use the *CANCEL,TCP command to stop the connection to a node or on a socket. All active transmission and reception activity is allowed to complete before this command takes effect.

Do TCP connections close automatically? ›

A good way to test it is connect two computers with 3 legs and two switches in between, disconnecting the middle leg, thus preventing carrier loss but still physically disconnecting the machines. TCP sockets don't automatically close at all. However TCP connections do.

What type of packet terminates a TCP connection? ›

A computer initiates closing the connection by sending a packet with the FIN bit set to 1 (FIN = finish). The other computer replies with an ACK and another FIN. After one more ACK from the initiating computer, the connection is closed.

What is TCP connection termination in Wireshark? ›

To close the TCP connection, the closing side should send a FIN packet which also contains an ACK for the last data this side received, then the other side should reply with an ACK that it received the FIN and notify the application that the other side is closing the connection.

What does connection termination mean? ›

Each connection has a client side and a server side. If either side is terminated separately, the connection is no longer usable, although the other side might not be terminated until some time later.

What is the life cycle of TCP connection? ›

The Life-Cycle of TCP/IP Connections

A connection opens with a pair of SYNchronization messages. It closes with a pair of FINish messages. The messages are in pairs because they go one in each direction. This is the so-called “handshake” between two TCP/IP stacks.

What is TCP 3-way termination? ›

The three-way handshake is also used during connection termination. When a device wants to terminate the connection, it sends a TCP segment with the FIN (finish) flag set. The other device acknowledges the FIN segment, and both devices exchange FIN and ACK segments to close the connection gracefully.

What are the 3 steps in a TCP handshake? ›

TCP uses a three-way handshake to establish a reliable connection. The connection is full duplex, and both sides synchronize (SYN) and acknowledge (ACK) each other. The exchange of these four flags is performed in three steps: SYN, SYN-ACK, ACK, as shown in Figure 5.8.

What is the TCP connection handshake? ›

What is a TCP three-way handshake?
  1. The client sends a segment to the server that includes: ...
  2. The server receives the client's segment. ...
  3. The client receives the server's request for synchronization and responds with a segment that contains an ACK to acknowledge receipt of the server's ISN, and the connection is established.
Oct 2, 2023

How do I release a TCP port? ›

If the process or application using the port is a standalone process, we can simply terminate it to free up the port. To do this, we need to find the PID of the process using the port, and then use the kill command to terminate it.

What is TCP connection timeout? ›

A TCP connection timeout is a situation where a network device or application stops waiting for a response from a remote host after a certain period of time. It can happen for various reasons, such as network congestion, packet loss, firewall rules, or malicious attacks.

How does TCP keep connection alive? ›

The KeepAlive mechanism does this by sending low-level probe messages to see if the other side responds. If it does not respond to a certain number of probes within a certain amount of time, then it assumes the connection is dead and the process using the socket will then detect this through an error indication.

How connection is terminated in TCP using three-way handshaking? ›

The three-way handshake is also used during connection termination. When a device wants to terminate the connection, it sends a TCP segment with the FIN (finish) flag set. The other device acknowledges the FIN segment, and both devices exchange FIN and ACK segments to close the connection gracefully.

How is TCP connection established and terminated using 3 way handshaking? ›

TCP uses a three-way handshake to establish a reliable connection. The connection is full duplex, and both sides synchronize (SYN) and acknowledge (ACK) each other. The exchange of these four flags is performed in three steps: SYN, SYN-ACK, ACK, as shown in Figure 5.8.

Why is TCP end to end? ›

The TCP/IP transport layer ensures that packets arrive in sequence and without error, by swapping acknowledgments of data reception, and retransmitting lost packets. This type of communication is known as end-to-end.

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6611

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.