The TCP three-handshake and four-wave question is one of the most common interview questions. Many readers will know three and four times, but if you ask them more, they will often fail to give an accurate answer.

This article attempts to use animation to illustrate this point, in the hope that it will make it easier for readers to understand the nature of TCP interactions.

TCP/IP stands for Transmission Control Protocol/Internet Protocol and refers to a series of associations.

It can be divided into four levels:

  • 1. Data link layer, network layer, transport layer and application layer.

  • 2. At the network layer, there are IP, ICMP, ARP, RARP and BOOTP protocols.

  • 3, in the transport layer: TCP protocol and UDP protocol.

  • 4. At the application layer, there are FTP, HTTP, TELNET, SMTP, DNS and other protocols.

TCP and UDP use the IP protocol to send packets from one network to another. Think of IP as a kind of highway that allows other protocols to drive over it and find exits to other computers. TCP and UDP are highway “trucks” that carry the goods of protocols like HTTP, FTP, etc.

TCP and UDP are transport layer protocols used by the likes of FTP, HTTP, and SMTP. While BOTH TCP and UDP are used to transmit other protocols, they have one significant difference: TCP provides guaranteed data transmission, while UDP does not. This means that TCP has a special mechanism to ensure that data is passed safely and error-free from one endpoint to another, whereas UDP does not provide any such guarantee.

TCP Three-way handshake

TCP’s three-time handshake is like two people who see each other 50 meters apart on the street, but cannot be 100% sure due to haze and other reasons, so they need to make sure whether the other person knows them by waving.

640

Hug each other

We see that there are four movements in this process, wave — nod and smile — wave — nod and smile. Two gestures are performed in succession: a nod and a smile (reply), and a wave (for confirmation). In fact, the two can be combined, with a nod and a smile (SYN + ACK). So the four actions are reduced to three, wave — nod and smile and wave — nod and smile. That’s the essence of the three-way handshake, where the one action in the middle is a combination of the two.

We see that there are two intermediate states, syn_sent and syn_RCvd, which are called “half open” states, waving at each other, but not yet seeing the other person nod or smile. Syn_sent is the “half open” state of the active open party, and syn_RCVD is the “half open” state of the passive open party. The client is the active open party, and the server is the passive open party.

  • syn_sent: syn package has been sent

  • syn_rcvd: syn package has been received

TCP Data transmission

TCP data transmission is a conversation between two people who are a little bit apart, so they need to be repeated to confirm that they have heard their words.

6402

The client calls out a message (data), and the receiver hears it and responds to it (ACK).

If you shout, for a long time did not hear the reply, you think your words were blown away by the wind, did not hear, so you need to shout again, this is TCP retransmission.

It is also possible that the Server heard the Client, but the reply from the Server to the Client was blown away by the wind, so that the Client did not hear the reply from the Server. The Client does not know whether its own words were blown away by the wind or whether the Server’s reply was blown away by the wind.

6403

The Client can communicate with the Server, and the Server can communicate with the Client. Because TCP links are duplex, both parties can initiate data transmission. However, no matter which side of the propaganda, they need to receive the other side’s confirmation to think that the other side received their propaganda.

The Client may be an anti-aircraft gun and say eight sentences in a row. At this time, the Server can reply to the other party without replying one sentence at a time. Instead, after listening to the eight sentences in a row, the Server can reply to the other party together and say THAT I heard all the previous eight sentences you said, which is batch ACK. However, the Client should not talk too much at once. The Server’s brain may not be able to digest too much in a short period of time. The two parties need to negotiate the appropriate sending and receiving rate, which is called the “TCP window size”.

Summary of three TCP connections

(1**) First handshake: ** When the connection is established, client A sends A SYN packet (SYN=j) to server B. Client A enters the SYN_SEND state and waits for server B to confirm the connection.

**(2) Second handshake: ** Server B receives A SYN packet. Server B must acknowledge client A’s SYN(ACK=j+1) and send A SYN packet (ACK= k). Server B enters the SYN_RECV state.

**(3) Third handshake: ** Client A receives the SYN+ACK packet from server B. Client A sends an ACK packet (ACK=k+1) to server B. After the ACK packet is sent, client A and server B enter the ESTABLISHED state and complete the three-way handshake.

After the three-way handshake is complete, the client and server begin to transfer data.

TCP waves four times

The TCP disconnect process is similar to the link establishment process, except that the middle two don’t always mix into one step, so it is divided into four actions: Client wave (FIN) — Server sad smile (ACK) — Server wave (FIN) — Client sad smile (ACK).

The middle two actions are not combined because TCP is in a “half closed” state, that is, one-way closed. The Client has waved his hand, but the person has not gone, but no longer speak, but the ears can continue to listen, the Server continues to shout. Waiting for Server tired, also no longer talk, super Client waved, Client sad smile, completely over.

6405

A special state, time_WAIT, is a long state that the active party enters after responding to the wave of the other party. This state lasts for four minutes before it enters the closed state and releases socket resources. However, in the specific implementation of this time can be adjusted.

It’s like the responsibility of the person who initiated the breakup. It’s you who initiated the breakup and you have to pay the price. The result is a 4-minute time_WAIT state in which socket resources cannot be released, which acts as a widow period during which socket resources cannot be recycled.

It is used to retransmit the last ACK packet to ensure that the other party can receive it. If the socket does not receive an ACK, the socket retransmits a FIN packet. A socket in TIME_WAIT state immediately resends an ACK packet to the socket.

During this period of time, the residual packets generated on the Internet route of the link during the conversation (the path is too rugged, and the data packets take a long time to travel. Therefore, all retransmitted packets are received, and the original packets are still on the road) are immediately discarded. Four minutes is enough time for the residual messages to disappear completely. Otherwise, when the new port is reused, the residual packets may interfere with the new link.

4 minutes is 2 MSLS, each MSL is 2 minutes. MSL is maximium segment lifetime. This time is set by the official RFC protocol. I haven’t seen a very satisfactory explanation as to why it is 2 MSLS instead of 1 MSL.

Four waves are not always four waves. Sometimes the two actions can be combined to make three waves, and the initiator will go directly from fin_wait_1 state to time_WAIT state, skipping fin_wait_2 state.

TCP four breakups summary

Because TCP connections are full-duplex, each direction must be closed separately. The principle is that a party can send a FIN to terminate the connection in this direction when it has completed its data sending task. Receiving a FIN only means that there is no data flowing in that direction. A TCP connection can still send data after receiving a FIN. The first party to close will perform an active close, while the other party performs a passive close.

  1. ** Client A sends A FIN, ** to close the data transfer between client A and server B (segment 4).

  2. When server B receives the FIN, it sends back an ACK confirming the received NUMBER plus 1(segment 5). Like SYN, a FIN takes a serial number.

  3. Server B closes the connection with client A and sends A FIN packet to client A(segment 6).

  4. Client A sends an ACK packet and sets the acknowledgement number to the receiving NUMBER plus 1(segment 7).

conclusion

TCP state transition is a very complex process. This article only explains some simple basic knowledge points by analogy. To learn more about TCP, readers need to search for technical articles to further their study. If the reader has a solid grasp of the basics of TCP, it will not be too difficult to understand the advanced knowledge.

supplement

Recently as an interview manager in the interview, I found that many people framework is very strong, Java foundation is some weak, many children for HTTP, TCP, UDP and SOCKET concepts are not very clear, silly not clear, here we also briefly mentioned

HTTP is a protocol in itself, the transport protocol for transporting hypertext from a Web server to a local browser.

HTTP(Hypertext Transfer Protocol) is a protocol that uses TCP to transfer information between two computers, usually a Web server and a client. The client uses a Web browser to initiate AN HTTP request to the Web server, and the Web server sends the requested information to the client.

The HTTP protocol is based on the request/response model. First, the client establishes a TCP link to the server and sends a request to the server. The request contains the request method, URL, protocol version, and associated MIME style message. The server responds with a status line containing the protocol version of the message, a success and failure code, and the associated MIME style message.

HTTP/1.0 establishes a new TCP link for each HTTP request/response, so a page containing HTML content and images will need to establish multiple short-term TCP links. The establishment of a TCP link will require three handshakes.

In addition, in order to obtain the appropriate transmission speed, TCP takes an additional loop link time (RTT). This constant overhead is required every time a link is established, with no actual useful data, but only to ensure that the link is reliable, so HTTP/1.1 proposes the implementation of sustainable linking. HTTP/1.1 will establish a TCP link only once and use it repeatedly to transmit a series of request/response messages,

Therefore, the number of link establishment and recurrent link overhead are reduced.

Although HTTP itself is a protocol, it is ultimately based on TCP.

SOCKET: INDICATES the API of TCP/IP networks.

Socket is an intermediate software abstraction layer for communication between application layer and TCP/IP protocol family. It is a group of interfaces. In design mode, Socket is actually a facade mode, it hides the complex TCP/IP protocol family behind the Socket interface, to the user, a simple set of interfaces is all, let the Socket to organize data to conform to the specified protocol.

Socket interface is the API of TCP/IP network. Socket interface defines many functions or routines for developing applications on TCP/IP network.

This is to achieve the above communication process and establish a communication pipeline, its real representative is a client and server side of a communication process, both processes through socket communication, and communication rules using the specified protocol. A socket is just a connection mode, not a protocol. TCP and UDP are simply (although not exactly) the two most basic protocols. Many other protocols are based on them. Sockets can be used to create connections of any protocol because all other protocols are based on them.

** To sum up: ** requires IP protocol to connect to the network; TCP is a mechanism that allows us to transfer data securely. HTTP, which uses TCP to transfer data, is a special protocol used by Web servers and clients. HTTP is based on the TCP protocol, but can use sockets to establish a TCP connection.

More reference

Android plug-in package skin change (high imitation netease Cloud Music skin change)

Application is a singleton in Android, right?

Android App Bundle — Android App Bundle

About immutability in Java

Finally, if you are interested in technology, please follow my wechat official account: Terminal DEVELOPMENT Department, ID: codeGooger, and advance technology together