What happens when the TCP channel shuts down? __tcp

Source: Internet
Author: User
Tags ack

Some time ago, colleagues found that a thrift theadpoolserver as a TCP service appeared a large number of close_wait state of the socket. When you first encounter this problem, you may have the following questions: What is close_wait? Why would it happen? There are other similar states I don't know.

In fact, what you really want to ask is what happens when the TCP channel shuts down. What am I going to do.

A TCP channel is a connection in which both sides of the connection can write data to the channel or read data from the channel, and both ends of the connection can initiate a shutdown operation. The shutdown process for the entire TCP channel is as follows:

A (socketfd:10) <--–tcp connction ———->b (socketfd:20) closes a, then a sends a fin to B; b After receiving the FIN, return an ACK indicating that the shutdown notification was received, and when B returned the ACK, the status was changed to close_wait(. ), what is it waiting for? Wait for the program to explicitly (manually) close socket B. If the program that opens B does not do any processing after issuing an ACK, it is always in the close_wait. If the program explicitly shuts down B, then B sends a fin to a, then B is in the Last_ack state, and a The last ACK is issued after receiving the fin of B, at which point A is in the well-known time_wait state. Time_wait time is usually longer.

How to avoid close_wait? Too much close_wait usually means there is a serious problem in your program: do not properly handle the shutdown of the socket, to see the code.

how to avoid time_wait? Usually, this is a question that should not be asked, should ask oneself first, time_wait really bring you trouble? Time_wait is the guarantee mechanism of reliability, there is no need to avoid it. But maybe, you can try using RFC1323. If there is too much time_wait at one end of the problem, I suggest the strategy is: try to avoid time_wait too much of the end of the active closed socket socketpool, avoid frequent create/close socket

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.