TCP three or four handshakes

Source: Internet
Author: User

In layman's terms:

Three-way handshake:

A: Send a connection request.

B: After receiving the request, B knows that its receiving end is okay. B returns the response to a's request and asks a if a has received its own response.

A: receive B's response. A knows that both the sender and receiver are good. Then, send a response to respond to B's inquiry.

When B receives the response from a, B knows that B has received the inquiry sent by the second handshake. This proves that B's sender is also good, in this way, a stable connection is established (so far ).

 

Four Waves:

First, let's clarify several issues.

Ticket: the sender and acceptor are fixed and their roles are unchangeable. That is, a can only be a --> B, and cannot be reversed. Once set, a will always be the sender.

Duplex: the sender and acceptor have variable roles. A --> B, or B -->. The half-duplex and full-duplex are also divided.

Half Duplex: At the same time, information can only be transmitted in one way. That is, at the same time, a --> B, or B --> A can only occur separately, but cannot be reversed at the same time. For example, walkie talkie.

Full Duplex: At the same time, one end can send and receive information simultaneously. At the same time, information can be transmitted in two directions. A --> B and B --> A can occur simultaneously.

In the network, both the client and server adopt full duplex. That is, when the client sends data to the server, it also receives data from the server. Similarly, server.

On this basis, there is a concept of semi-closing: the client closes the sending data, but retains the received data. Similarly, server.

Therefore, the client and server are closed independently of each other. If both the "No data sending" and "No data receiving" conditions are met, disable it. (Regardless of network latency)

C (client): no data needs to be sent, or add an end flag at the end of the sent data to the S (server), telling s that the sending is complete. C is still receiving data.

S (server): keeps receiving data until the end information of C is received (or the end request of C, or the data that contains the end flag ), s knows that C data transmission is complete. S sends a response to C, saying that I know you have finished sending the response. In this case, s keeps receiving and sending data. S may also be sending data to C. (Why do we need this handshake? If s does not return a response, it means S is still receiving data from C .) C receives the response from S, and determines that s receives the End message sent by itself (for the moment, it is understood as end ). C. Enable the semi-closed status. That is, disable data sending to keep data receiving.

S: The End of sending data, tell C.

C: receives the notification of the end of data sending from S and sends it back to S. S receives the C response. c knows that s ends data transmission and s closes. (Close all)

C does not immediately disable data reception. Wait for a while (2msl) and make sure s receives the response sent in the previous step. If s does not receive a C Response Due to network reasons, s will not shut down without permission, but will send the end information to C continuously, c. Keep the half-off status for a period of time. By default, the half-off status is enough to receive the second sending end message within this period of time.

Therefore, to understand the client and server as two independent shutdowns, it is better to understand the four waves. Each end must send a data sending end request and a response to the end request of the other side (used to close the other side ).

Some of your own language descriptions, specific TCP/socket technical implementation needs further learning.

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.