The process of establishing and terminating TCP connections

Source: Internet
Author: User
Tags ack socket port number

We know that communication between networks is simple from the application layer, which is the transmission and/or reception of information between processes on the same or different hosts. In the process of information transmission, we need to follow certain rules.
As a simple example, if I want to send a message A to you, then according to the specific rules I send a in the process of transmission is actually in the form of a++. But the message you received is still a. This means that my information has been "regularly" returned in the course of transmission. This "rule" is what we usually call the agreement. That is, to follow its style of affairs.
The object TCP protocol discussed in this session is one of these rules. That is, if we follow the TCP protocol when we communicate on the network, we have to follow its tone.
The TCP protocol is a connection-oriented protocol that, in either direction, must establish a connection between the two parties, as described above, "must establish a connection" is part of the TCP style.
The simplest three steps to communicate using the TCP protocol are:

1. Establish a connection between the two parties
2. Information Transmission and Reception
3. Disconnect this connection

Note: This article often mentions the word "communication", where it simply refers to the process of communicating and transmitting information across the network.
Here is a question, whether to establish a good connection can communicate, after the completion of the communication to shut down or disconnect the connection. Yes. Normally it would be so simple and easy to understand the memory plus operation. However, many times, the situation is not normal. So what's the point of not being normal? Let's first put the problem here and look at the most normal situation.

Before analyzing this simple diagram, let's look at some concepts:

SYN: The synchronous sequence number, which is the handshake signal ACK used when TCP establishes a connection
: A transmission class control character that is sent to the sender, indicating that the sent data has been confirmed to receive an error
fin:tcp the wave signal used when terminating.

Below, we use the two nouns "server" and "client" to represent the two ends of the communication we are talking about.

TCP connection,
Server side through socket, bind, listen the three system call function to complete the preparation phase of receiving foreign connection.
When the client needs to send a connection request to the server, a good socket is created.
When both ends are ready, you can complete the TCP connection as shown in Figure 1.

Just like the phone and phone number are required on the phone, a socket pair is required for the TCP connection. A TCP socket pair is a four-tuple that defines the two endpoints of the connection. Local IP address, local TCP port number, foreign IP address, foreign TCP port number. Each socket pair uniquely identifies a TCP connection on a network.

The two values (IP address and port number) that identify each endpoint are usually called a socket.
Figure 1 also called three-time handshake, the process is as follows:
(1) The client initiates an active open by calling the system Call function Connect function, which causes the client TCP to send a SYN to the server side, indicating that I want to connect you.
(2) After the server receives the request, sends an ACK to tell the client that I know it, and sends a SYN to the client saying that you are connected to me and ready to send the data.
(3) The client receives an ACK to the server later to say I know I can send data, not hair to look at the mood.

Details of the connection I did not introduce, I mainly briefly introduce the general process and some of the more special cases.

When connected, both parties can send and receive data to each other, as shown in Figure 3

Figure 2 also called four waves, as shown in the process is as follows:
(1) The client first system call function Close, (of course, it can also be the server first call off, here is the more common situation) to actively close the connection, the client TCP send a fin to the server side said I do not need to connect to our broken.
(2) The server-side TCP confirms the fin of the client and sends an ACK to the client saying I know that you will not send me the data again.
(3) After a while, the server calls close to close the socket that the client connects to this side, and sends a fin to the client that says I turned off the connection above me.
(4) The client receives fin and sends an ACK to the server to indicate that he knows it's okay with the server, and then dies at ease.

Well, the above is the best and simplest way to send data to a TCP connection, as I mentioned above, usually not so simple, because it always happens "accident", there are good and bad, we refer to these "accidents" as "special circumstances."
Let's take a look at these scenarios: # # # # TCP semi-shutdown:

First Picture:

First note: Sending a fin means that I will not send you any more data.
TCP provides the ability for one end of a connection to receive data from the other end after it has been sent (a fin is sent). Until the other person sends over a fin it is completely disconnected no longer (loss) received.

Let's look at the six arrows in Figure 4 to step through the analysis:
(1) The client sends a fin to the server, saying I want to die and won't send you any more data.
(2) The server sends an ACK to the client, indicating that I know you will not send it to me again.
However, the server side of the data to be sent to the client ah, and just the client is now the state is able to receive, so,
(3) The server side sends the data to the client.
(4) The client is very not reserved to receive and send the server an ACK, said I received your data.
When the server is not good to see the hair, you do not want to break, then you break it.
(5) The server broke the client's connection on it and sent it to the client a fin said I had broken you and will not send you again.
(6) The client receives fin and sends an ACK to the server to indicate that he knows it's okay with the server, and then dies at ease.

In order to be able to use the semi-shutdown feature of TCP, the programming interface must provide a way for the application to illustrate.
The way is, the client in Figure 4 does not call Close when the fin is issued and calls shutdown, and the second parameter is set to 1, at which point the API supports semi-shutdown.

However, although TCP has this ability but uses less, the general is to send the data after the sending fin want to die is also too late. But people do have pies in use, but the situation is relatively small, the application (example Figure 4 client), generally by calling close to terminate the two-direction connection. RST: Indicates that the other person is required to reestablish the connection

In some cases, one end will be sent to each other, and a RST requires reconnection, and if the two parties are not connected, disconnect the connection if it is already connected.

I read a blog post on the internet to introduce some of the possible situations of RST feel very detailed here to stick a hyperlink bar, interested can see for themselves: Several TCP connections appear in the case of RST. In this article there are some small problems, such as the inside of the request timeout connection, should be "host 27 is not friendly" to "host 89 is not friendly."

Here, I would simply follow the above blog to mention a few types of TCP connections in the case of RST:

(1) The server program port is not open and the client is connected: At this point the host on some operating systems responds to an RST to the client, while some hosts on the operating system are ignored.
(2) The request timed out: For example, the client sends a SYN to the server side, the server does not respond to the client ACK or/and SYN before timing out, then the client sends a SYN to the server to deny further connections.
(3) Early closure: For example, you send me 5 data, I only pick three and two do not answer, and then I close the connection, send you a rst said I do not pick up, we re-connect it.
(4) When the data is received on a closed socket, a RST is sent to the other person. simply put, I will send you an RST if I don't receive you, whether it's not receiving a connection or not receiving the data. No matter if there is any connection before, it is broken, I have to reconnect.

Said so much, the following to consider a situation detection detection: The server host crashes after the restart, will send the data sent to the client sent a RST, in that case.
Yes, it belongs to the case of not receiving data.
Because when the server crashes and restarts, TCP loses all the connection information before the crash, the TCP connection is safe and secure, I don't know you. Of course, you will not receive your data, give you a RST, you re-connect me.

In fact, here I should be on the TCP state transfer diagram, then I will be the last picture. (This is a blog post looking for the map to be attached but not attached, the key is to understand the meaning of the diagram)

Feel me above the state of the general process is said to be almost, combined into the picture is each send or receive a message to change a state. Here I only describe the three states in the diagram above.

(1) Open at the same time: open at the same time both ends of the active open, this possibility is very small but exists. In order to handle the simultaneous opening of TCP also deliberately designed, for this simultaneous open, TCP establishes a connection instead of two (many other protocols set up two). The
sends the SYN at almost the same time and enters the syn_sent state, and when each end receives the SYN, the state becomes the SYN_RCVD state. At the same time they all send SYN (what the server does) and confirm the Received SYN (the third step of the server when the client is doing it). When both sides receive the SYN and the corresponding ACK, the state changes to established. It is important to note that each end of the two ends is both a server and a client.

(2) Established: This state indicates that three times the handshake has been completed to transmit the data.
(3) Time_wait: Also known as waiting for 2MSL status. Each specific TCP implementation must select a message segment Maximum lifetime MSL, which is the longest time in the network before any message segments (data) are discarded.
In fact, I did not understand the beginning, why should have time_wait this state, obviously have already died ah. That is no state to give it time to wait for what, directly to the closed is good ah.
Later, I found two reasons for its existence, outlined as follows:
A. Reliably terminate TCP full-duplex connections: Maintain connections for a certain time in case the last confirmation ask needs to be re-sent. This also explains why the time_wait side always executes the active shut-off end. Because you might have to re-preach the end of that ack.
B. Allow old repeating segments to fade out of the network: for example, we close a connection A, after a period of time to establish a new connection between the same IP address and port number B, because the IP address and the port number of a and B are the same, if the old connection a disconnected time is not msl,b connection can transmit data, if connected a re- Data is likely to be transmitted by connecting B mistakenly as data in its own connection. In order to solve this problem, TCP will not give the TIME_WAIT state of the connection (IP address and port number) to initiate a new connection, and so on after the time_wait this state has been over the past 2MSL time, the data in the connection has long disappeared in the network, New connections that are initiated at this time will not receive the old duplicate data.

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.