This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

TIP 👉 The setting sun is infinitely good, just near dusk

preface

TIP 👉 The computer network is large and sophisticated, for the front end of the interview, high frequency test point

TCP protocol

TCP is a complex protocol because it has to solve many problems. The full name of TCP protocol is Transmission Control Protocol, which is a connection-oriented, reliable and byte stream-based transport layer communication protocol. It is defined by RFC 793 of IETF. TCP is a connection-oriented, reliable streaming protocol. A flow is an uninterrupted data structure, and you can think of it as a flow of water in a drainpipe.

First, let’s look at the network OSI seven-layer model

We need to know that TCP is at the fourth layer of the network OSI’s seven-layer model, the Transport layer

Three-way handshake during a TCP connection

Why is a THREE-way handshake during a TCP connection?

There are only two steps to connecting people:

  • A extended an invitation to B
  • B accepts

Computers and people are different, there is a “network state” between computers, it is likely to affect the communication between two computers. Through the three-way handshake, the client has experienced a request and a response, and the server has also experienced a request and a response. At this time, on the one hand, it confirms that the current network status is good, on the other hand, it confirms that the communication object can request and respond, and there is nothing wrong with it. Only in this secure and stable condition can TCP connections be established between two computers.

TCP breaks the link with four waves

Why is the TCP connection disconnected with four waves?

A TCP connection is a full-duplex protocol, meaning that both parties can send or receive data to an object at the same time.

When a client wants to disconnect, it can only confirm that it has no data to send to the server, but not whether the server still has data to send.

Breaking up is a matter of two people. Instead of terminating the relationship because the client has run out of words, the client will wait for the server to finish. Therefore, even if the client throws a break request, the server can still transmit data

The first two waves are confirmation of the breakup, but not an immediate confirmation of the breakup.

Before the third wave, the server will finish what it wants to say and notify the client again. At this point, both parties are truly ready to part.

On the fourth wave, the client received the breakup request from the server and responded with a “accept” signal, finally ending the relationship.

This section describes the characteristics of TCP

  • Connection-oriented, a connection must be established at both ends before data can be sent
  • Bidirectional transmission (full duplex)
  • Byte stream
  • Traffic cache: Resolve speed mismatch (sliding window)
  • Reliable transmission service (guarantee reachability, and increase delay by retransmission in case of packet loss)
  • Congestion control

Under TCP, a three-way handshake is required to establish a connection to ensure that a stable transmission channel is established between the two parties. Once again, TCP is also known as “reliable connection-oriented transport”.

UDP, on the other hand, is a world of no handshakes, no informed consent, and a very casual protocol.

UDP protocol.

Under UDP protocol, data can be sent at any time. It doesn’t care whether the other party receives its data or not, nor does it engage in congestion control-even on 2G and 3G networks today, it doesn’t slow itself down to make sure it doesn’t lose packets.

For this reason, UDP is also known as “connectionless unreliable transport.”

Application scenarios of UDP

At first glance, UDP seems to be so unreliable that it doesn’t make sense for everyone to use TCP as a protocol

In fact, the existence is reasonable, although UDP is not safe, but it also has its advantages, such as:

  • It can serve multiple parties: UDP not only supports one-to-one transport, but also supports one-to-many, many-to-many, many-to-one,
  • Header overhead is small: THE UDP header is only 8 bytes large, while TCP requires 20 bytes. For the same packet content, UDP is more efficient than TCP.
  • Randomness is also an advantage: sometimes our connections need to be established in real time, and we don’t have the resources to give you three handshakes and four waves over and over again. In such situations, UDP “just send” is more flexible.

Combined with the advantages of UDP, it is not difficult for you to imagine its application scenarios: in some occasions requiring strong real-time, such as voip, video conferencing, and live streaming, UDP is more suitable than TCP. And such as file transfer to the reliability and stability of high requirements, but also its own connection certainty is strong demand, using TCP to solve will be more reliable.