1 socket connection establishment timeout
Establish a socket connection is a TCP-based connection. The TCP connection needs to be completed through three handshakes. When establishing a TCP connection, you need to send a synchronous Syn packet, wait for the confirmation message SYN + ACK, and finally send the confirmation message ack. The TCP connection is closed by four waves. One party that closes the TCP connection actively sends the fin message, waiting for the confirmation message from the other party. The other party that closes the TCP connection also sends the fin message, however, the message is waiting for confirmation.
A fixed-length connection queue is waiting for one end of the TCP connection request. The connections in the queue have been accepted by TCP (that is, the three-way handshake has been completed), but have not been accepted by the application layer. TCP accepts a connection and puts it in the connection queue, while the application layer accepts the connection to remove it from the queue. The application layer can specify the maximum length of the connection queue by setting the backlog variable, that is, the maximum number of connections that have been accepted by TCP and are waiting for the application layer to accept.
When a connection request SYN arrives, TCP determines whether to accept the connection. If there is space in the queue, the TCP module confirms the SYN and establishes the connection. However, the application layer will only know the new connection after the third packet in the three-way handshake is received. If the queue has no space, TCP will ignore the SYN received.
If the application layer cannot accept connections accepted by TCP in a timely manner, these connections may occupy the entire connection queue, and new connection requests may time out without being responded. If a connection request SYN is not received after a certain period of time, TCP retransmits the connection request SYN twice, and the interval of each retransmission is doubled, if no SYN + ACK is received within the specified time, TCP will discard the connection request and the connection establishment will time out.
The establishment timeout of a Java Socket Connection is the same as that of TCP. If the three-way handshake times out when a TCP connection is established, the establishment of the socket connection times out. You can set the timeout value for socket connection establishment-
Connect (socketaddress endpoint, int timeout)
If the connection is not established successfully within timeout, A timeoutexception exception is thrown. If the timeout value is less than the three-way handshake time, the socket connection will never be established.
Different application layers have different connection establishment processes. Socket Connection establishment is the same as TCP connection establishment-only three handshakes are required to complete the connection, but some applications need to interact a lot of information before the connection can be established successfully, for example, in the Telnet protocol, after the TCP three-way handshake is complete, the telnet connection is established after option negotiation.
2 socket read timeout
If there is no data in the recvq of the input buffer queue, the read operation will be blocked and the thread will be suspended until new data arrives or an exception occurs. You can call setsotimeout (INT timeout) to set the timeout time. If no data exists after the timeout time, read throws a sockettimeoutexception, and the program needs to capture this exception, however, the current socket connection is still valid.
If the peer process crashes, the peer machine suddenly restarts, and the network is disconnected, the local read will continue to be blocked. It is very important to set the timeout time. Otherwise, the thread that calls the read will remain suspended.
The TCP module puts the received data into recvq until the application layer calls the read method of the input stream to read the data. If the recvq queue is filled up, TCP will notify the peer not to send data based on the sliding window mechanism, and the local end will stop receiving data from the peer end, it makes room until the recipient's application calls the read method of the input stream.
3 socket write timeout
Socket write timeout is TCP-based timeout retransmission. Timeout retransmission is an important mechanism for TCP to ensure data reliability transmission. The principle is to enable a timer after a data packet is sent. If Ack is not confirmed by the sent packet within a certain period of time, then resend the message. If no confirmation message is sent after multiple resends, a reset message RST is sent and the TCP connection is closed. The time difference between the first datagram message sending and the reset message transmission is about 9 minutes. That is to say, if the message is not confirmed within 9 minutes, the connection is closed. However, this value varies depending on the TCP stack implementation.
If the sender calls write to write data continuously until the sendq queue is filled up. If the write method is called when the sendq queue is full, the write will be blocked until sendq has a new free space, that is, until some bytes are transmitted to the recvq of the receiver socket. If the recvq queue is also filled, all operations will be stopped until the receiving end calls the read method to transmit some bytes to the application.
When the socket write sends data, if the network cable is disconnected, the peer process crashes, or the peer machine restarts, the TCP module re-transmits the data and closes the connection upon timeout. If you call write again next time, it will cause an exception and exit.
Socket write timeout is a timeout retransmission mechanism based on the TCP protocol stack. Generally, you do not need to set the write timeout. This method is not provided.