Establish and disconnect TCP connections

Source: Internet
Author: User
Tags connection reset

Start three handshakes:

If you still don't have simple TCP socket programming, I suggest you study it first. This is like you don't know the basic C ++ syntax, so don't study vtable.
And so on.

The three-way handshake starts when the client tries to connect to the server. When you call functions such as connect, three handshakes are started normally.
Figure showing three handshakes on the Internet:

As described above, a three-way handshake generates three data packets. The client actively connects to send the SYN Packet (note the serial number and
Confirmation number, because this does not contain user data, so the sequence number and confirmation number are plus or minus one ). When the server receives the message
Normally, the SYN and ACK packets are sent for confirmation, and the client is told: I want to open the connection (Duplex) on my side ). Customer
The server then confirms the SYN, and then sends an ACK message. Then the connection is established. For a blocking socket, you
May return success to you.

The main part is the four handshakes:

In fact, from my perspective, the client and server are not differentiated. Either party can call close (or closesocket) or the like.
To terminate a connection. Now let's talk about the normal situation. When you call the close function to disconnect a connection
One Party sends the fin (finish packet to the other party. With your previous experience, I think you should understand what I mean when I talk about FIN packets. That is
A message segment with the FIN flag configured. The fin message may also contain user data. If there is still data to be sent
It is completely normal to add this FIN packet. Later, you will see that there are many additional packets, such as ACK packets. We need to grasp
The principle is that TCP will certainly achieve the maximum efficiency, so the optimization method you can think of, I think TCP will think.

When a passively closed party receives a fin message, it sends an ACK confirmation message (you should be familiar with ACK ). Here is
Note that TCP is duplex, that is, you can imagine that a pair of TCP connections have two data paths. When sending FIN packets
It means that one end of the fin cannot send data, that is, one of the data paths is disabled. Send a message at one end that is passively disabled
After ACK, the application layer usually detects that the connection is about to be disconnected, and then the application layer that is passively disconnected calls close to close the connection.

I can tell you that once you call close (or closesocket), this end will send FIN packets. That is to say, now passive
The closing end also sends fin to the active closing end. Sometimes, the passive shutdown end will combine the ACK and FIN packets to send them together. Active
The shut-down end also sends ack after receiving the fin, and then closes the entire connection (in fact, it is not completely closed, but it only closes the packet sending to be exchanged.
), And the four handshakes are completed. As you can see, because the passive shutdown end may combine ack and fin for sending, this is not enough.
Strict four handshakes-four packet segments.

In the previous article, I did not mention TCP status conversion. Here I am still wondering if I should take out the commonly used figure,
However, here I only show the status transition diagram when the connection is disconnected, from <the TCP/IP guide>:

 

Time_wait:

1) Reliable termination of TCP full-duplex connection
When the four-way handshake protocol is closed, the last Ack is sent by the active closing end. If the final Ack is lost, the server resends the final fin, therefore, the client must maintain the status information to allow it to resend the final ack. If this status information is not maintained, the client will respond to the RST segmentation, and the server will interpret this segmentation as an error (socketexception of Connection reset will be thrown in Java ). Therefore, to terminate a TCP full-duplex connection, you must handle the loss of any of the four segments in the termination sequence. The client that closes the connection must maintain the status information in the time_wait state.

2) allow old duplicate segments to disappear in the Network
The TCP section may be "lost" due to a router exception. During the trip, the TCP sending end May resend this section due to confirmation timeout, after the vro is repaired, the lost section is also sent to the final destination. The original lost section is called the lost duplicate. After a TCP connection is closed, a TCP connection between the same IP address and port is established again. The latter connection is called the embodiment of the previous connection (incarnation ), in this case, the lost and repeated groups of the previous connection may appear after the previous connection is terminated, Which is misunderstood as being subordinate to the new embodiment. To avoid this situation, TCP does not allow connections in the time_wait status to start a new embodiment. Because the time_wait status lasts for 2msl, it can be ensured that when a TCP connection is successfully established, duplicate groups from the previous embodiment have disappeared in the network.

Advantages and disadvantages of time_wait:

A short request may cause many time_wait instances on the server. Therefore, the port is exhausted and the application cannot be accessed.

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.