When developing TCP high-concurrency servers, some rules seem to be customary. Many friends will follow these rules, suchThe client is preferred for active shutdown on highly concurrent TCP servers.,It is best to enable the SO_REUSEADDR option for the server program.But many people do not know why. Why should we do this?
First
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/115R55Y8-0.jpg "title =" close.jpg "alt =" 203639589.jpg"/>
We can see the status of each phase of the active and passive shutdown ends. Today, we focus on the TIME_WAIT status. We can see that the TIME_WAIT status is generated at the end of the active shutdown.
There are two reasons for the TIME_WAIT status to exist.
Reliable termination of TCP full-duplex connections;
Allows old duplicate segments to disappear in the network;
For the first reason, refer. Assume that the final ACK sent by the active shutdown end is lost. The peer will re-Send the FIN, and the active shutdown end can re-send the final ACK only when maintaining the status information. If this status information is not maintained, the active closing end will respond to an RST, and the peer end will mark this response as an error, so it cannot be properly closed.
The second reason is that we have established a tcp connection between host A: Port B and host C: Port D. We close this connection and establish another connection between the same IP address and port for a while. Because their IP addresses and port numbers are the same, if the old duplicate Group of the last connection appears, the new connection will be affected. To do this, TCP will not initiate this new connection for a connection in the TIME_WAIT status. If the duration is longer than the maximum time for MSLIP datagram to survive on the Internet)
To meet the preceding requirements, the TIME_WAIT status must have a certain duration. Therefore, TIME_WAIT is also called the 2MSL waiting status, which generally lasts between 1 minute and 4 minutes.
The client is preferred for active shutdown on highly concurrent TCP servers.: For highly concurrent servers, file descriptor resources are very important resources. If every connection requires a TIME_WAIT 2MSL duration, resources cannot be reused immediately. Although the client's TIME_WAIT status will occupy port and handle resources, the client generally has few concurrent resource restrictions, so it is more appropriate for the client to actively close the execution.
It is best to enable the server programSO_REUSEADDROption: We want to do this. If the server program in the production environment is closed due to some wrong operation, we must immediately restart the service program, however, TIME_WAIT still occupies these address and port resources to make your service unavailable, so you are not in a hurry. SOThe REUSEADDR option allows repeated binding of address ports.
Reference books:
UNIX Network Programming Volume 1, TCP/IP details Volume 1: Protocol
This article from "forever friends" blog, please be sure to keep this source http://yaocoder.blog.51cto.com/2668309/1338567