TCP learning records (from TCP/IP Volume 1)

Source: Internet
Author: User

TCP Learning Record (from TCP/IP Volume 1) tcp's TIME_WAIT status TCP requirements, first issue the FIN end, after both parties completely close the connection, the maximum message segment survival time (MSL) remains in the TIME_WAIT status until two times ). The recommended MSL value is 120 seconds, that is, it takes 4 minutes To Be In The TIME_WAIT status. When the connection is in the TIME_WAIT status, the same connection (that is, the client I p address and port number, and the server I P address and port number are the same) cannot be opened again. The client first issues FIN, which is called active shutdown, so the TIME_WAIT status appears on the client. During the extended state, TCP must retain certain status information for the closed connection, in order to correctly process the packets that arrive after the connection is closed for a period of time in the network. Similarly, if the last ACK is lost, the server re-transmits the FIN so that the client re-transmits the last ACK. Because the customer process proactively closes the connection (that is, the customer process first issues FIN), the customer needs to stay in the TIME_WAIT status for about 240 seconds after receiving the server's FIN. the combination of TIME_WAIT status and 16-bit TCP port number limits the maximum transaction rate between two hosts. For example, if the same customer owner needs to constantly communicate with the same server host, it either starts the next transaction every 240 seconds after the transaction is completed, select another port number for the followed transaction. However, up to 240 ports (64512 minus 65535 well-known ports) can be used every 1023 seconds, so that up to 268 transactions can be processed per second. The RTT value is about 1 ~ A 3 ms LAN may actually exceed this rate. Receiving a FIN only means that there is no data flow above this side. A TCP connection can still send data after receiving a FIN. This is possible for applications that use semi-closed services. The first party that closes the Service (that is, sending the first FIN) will take the initiative to close the service, and the other party (receiving this FIN) disable it passively. Usually one party completes active shutdown, while the other party completes passive shutdown. Half-closing TCP provides the ability of one end of the connection to receive data from the other end after sending the connection. The TIME_WAIT status is also called 2MSL wait status. For each specific t c p implementation, You must select a Maximum message Segment survival time MSL (Maximum Segment Lifetime ). It is the longest time before any packet segment is discarded in the network. We know that this time is limited, because the TCP packet segment is transmitted in the network as an I p datagram, while the I P datagram has a TTL field that limits its survival time. It is normal for the customer to take the initiative to close and enter TIME_WAIT. The server is usually passively shut down and does not enter the TIME_WAIT status. This implies that if we terminate a customer program and immediately restart the customer program, the new customer program will not be able to reuse the same local port. In FIN_WAIT2 status, we have sent a FIN and the other end has confirmed it. Unless we implement semi-close, we will wait for the application layer at the other end to realize that it has received a file Terminator and send a FIN to us to close the connection in the other direction. Only when the process at the other end completes this shutdown will the FIN_WAIT2 status change to the TIME_WAIT status. This means that this end may always be in this state. The other end will also be in the CLOSE_WAIT state and remain in this State until the application layer decides to close it. Detect semi-Open connection if one party has closed or the other party has terminated the connection abnormally but does not know it, we call this TCP connection semi-Open. A host exception at any end may cause this situation. As long as you do not intend to transmit data on a semi-open connection, the connected party will not detect exceptions on the other. Because the TCP of the server has been restarted, it will lose all information about the connection before the reset, so it does not know the connection mentioned in the data packet segment. The principle of TCP processing is that the receiver uses reset as the response. It is possible to open two applications at the same time and execute active opening at the same time, even though the possibility is very small. Each party must send a SYN and the SYN must be transmitted to the other party. This requires each party to use a port familiar to the other party as the local port. This is also known as simultaneous open (simultaneous open) TCP is specially designed to be able to process and open at the same time, it establishes only one connection instead of two connections (other protocol families, the most prominent is the OSI transport layer. In this case, two connections will be established instead of one connection.) an abnormal termination of a connection has two advantages for an application: (1) discard any data to be sent and immediately send the reset packet segment. (2) The recipient of the RST will distinguish whether the other end is shut down abnormally or normally. The API used by the application must provide a means to generate an exception rather than close normally. A common case of a reset is that when the connection request arrives, no process is listening to the target port. When the application layer issues the close command, both ends change from ESTABLISHED to FIN_WAIT_1. This will cause both parties to send one FIN, and the two FIN reach the other end after the network transmission. After receiving the FIN, the status changes from FIN_WAIT_1 to CLOSING and the final a c k is sent. When the last ACK is received, the status changes to TIME_WAIT. TCP server port number TCP uses a 4-tuple consisting of a local address and a remote address: Destination IP address, destination port number, source IP address, and source port number to process incoming connection requests. TCP only uses the destination port number to determine that the process has received a connection request. In addition, only LISTEN processes that use port 23 can receive new connection requests. The process in ESTABLISHED cannot receive SYN packets, but the process in LISTEN cannot receive data packets. A TCP connection is uniquely identified by a 4-tuple: local IP address, local port number, remote IP address, and remote port number. Incoming call Connection Request queue 1) A Concurrent Server calls a new process to process each customer request. Therefore, the server in the passive connection request should always be prepared to process the next incoming call connection request. That is why concurrent servers are used. However, there may still be multiple connection requests when the server creates a new process or when the operating system is busy processing processes with higher priority. When the server is busy, how does TCP handle these incoming connection requests? A fixed-length connection queue is waiting for one end of the connection request. The connections in the queue have been accepted by TCP (that is, three handshakes have been completed), but have not been accepted by the application layer. Note that TCP puts a connection into this queue, while the application layer accepts the connection to remove it from this queue. 2) the application layer specifies the maximum length of the queue. This value is generally referred to as the backlog value (backlog ). The value range is an integer between 0 and 5, including 0 and 5 (this value is set to 5 in most applications). When a connection request (SYN) arrives, TCP uses an algorithm to determine whether to receive the connection based on the number of connections in the current connection queue. We expect the backlog value described by the application layer to be the maximum number of connections allowed by this end point, but the situation is not that simple. Note: the backlog value indicates the maximum number of connections that the TCP listener endpoint has been accepted by TCP and is waiting for the application layer to accept. This backlog does not affect the maximum number of connections allowed by the system or the number of customers that can be processed concurrently by the concurrent server. 3) If there is still space in the connection queue of the TCP listener's endpoint for new connection requests, the TCP module will confirm the SYN and establish the connection. However, the application layer will only know the new connection when the third packet segment in the three-way handshake is received. In addition, when the customer process opens successfully but the application layer of the server does not know the new connection, it may think that the server process is ready to receive data (if this happens, the server's TCP only puts the received data in the Buffer Queue ). 4) if there is no space in the connection queue for new connection requests, TCP will ignore the SYN received. No message segment is sent back (that is, no RST is sent back ). If the application layer cannot accept connections accepted by TCP in a timely manner, these connections may occupy the entire connection queue, and the customer's active opening will eventually time out. The receiving connection queue is expected to be passed to the application layer in the FIFO order. For example, if TCP accepts connections with ports 1090 and 1091, we want the application layer to first accept connections with ports 1090 and then accept connections with ports 1091. However, many Berkeley TCP implementations all follow the forward-first-out transmission order. This error has existed for many years. When the queue is full, TCP ignores the SYN that will be passed in and does not send back the RST as a response, because this is a soft error rather than a hard error. Generally, the queue is full because the application or operating system is busy. This prevents the application from serving incoming connections. This condition can be changed in a short period of time. However, if the TCP of the server uses system reset as a response, the active opening of the client process will be discarded (if the server program is not started, we will encounter it ). Because no SYN is answered, the server program forces the client TCP to re-transmit the SYN to wait for the connection queue to have space to accept new connections. This behavior also means that the TCP server cannot enable the active opening of the customer process. When a new client connects to an application that is passed to the server, the TCP three-way handshake ends, and the customer's active access is successful. If the application on the server sees the client's I P address and port number, and decides whether to serve the customer, what the server can do is to close the connection (send FIN ), or reset the connection (send RST ). In either case, the customer process considers everything to be normal because the active open process has been completed and the request has been sent to the server program.

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.