Four Problems about the time_wait status in tcp, tcptime_wait status

Source: Internet
Author: User

Four Problems about the time_wait status in tcp, tcptime_wait status

In the interview, time_wait is a common question. What is most difficult to understand in tcp network programming is its time_wait status. This also shows the importance of the time_wait status in TCP/IP's four waves.
The following describes four problems.

Problem 1. What is the time_wait status? 2. Why is there a time_wait status? 3. Which party has the time_wait status? 4. How to avoid occupying Resources in the time_wait status 1. What is the time_wait status?

Simply put, the time_wait status is the status that the server enters after sending FIN to the client to terminate the connection four times.

Four Waves for tcp

The time_wait status exists when the client receives the server Fin and returns the ack packet.
When it is in the time_wait status, we cannot create a new connection because the port is occupied.

2. Why is there a time_wait status?

There are two reasons for time_wait
1. Reliable termination of TCP connections.
2. Ensure that the delayed TCP packet segment has enough time to be identified and discarded.

1. reliable termination of TCP connections. If a client in time_wait sends a message segment to the server to confirm that the packet segment is lost, the server will resend the FIN packet segment here, therefore, the client must be in a received status that is time_wait rather than close.
2. ensure that a later TCP packet segment has enough time to be identified and discarded. in linux, a TCP port cannot be opened twice or more than twice, when the client is in the time_wait status, we will not be able to use this port to establish a new connection. If the time_wait status does not exist, the new connection may receive data from the old connection. The time_wait duration is 2MSL, which ensures that the old data can be discarded because the data in the network exists at most MSL (maxinum segment lifetime)

3. Which party has the time_wait status?

The time_wait status is generally in the client state. It also occupies the port
It is sometimes generated on the server because the server actively disconnects or encounters exceptions.

4. How to avoid occupying Resources in the time_wait status

If it is a client, we generally do not have to worry about it, because the client usually uses a temporary port, and a new port will be allocated when the connection is created again. Unless the client is specified to use a port, this is generally not required.

If the connection is terminated abnormally after the server closes the connection, the connection time_wait status will cause it to fail to restart because it always uses a well-known server port number, however, we can use the socket option SO_REUSEADDR to force the process to immediately use the port occupied by connections in the time_wait status.
After socksetopt is configured, the socket address bound to sock can be reused immediately even if sock is in the time_wait status.

In addition, you can modify the Kernel Parameter/proc/sys/net/ipv4/tcp_tw/recycle to quickly recycle the closed socket, but the tcp connection does not enter the time_wait status at all, this allows the application to immediately reuse the local socket address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.