background

The question is in my interview, the technical director asked me a real question, then the answer is not very good, so it out of the summary.

In fact, the interviewer mainly wants me to tell the difference between UDP and TCP in principle, how to add some functions to UDP to implement TCP.

It’s easy to tell a difference or two between TCP and UDP, but what if you can say it in a way your girlfriend can understand?

Girlfriend: I don’t want to listen to what Ben says! I don’t understand

I’m going to answer the above question in plain English.

The characteristics of UDP

UDP reminds me of the time when I was a rookie after graduation.

  • Simple communication

The task that the leader arranges, do directly is finished.

UDP is also, I believe that the network world is always beautiful, THE packet I send is very easy to send, the receiver is also very easy to assemble. The data structure is also very simple and does not require a lot of data structure, processing logic, header fields.

  • Trusting others

I don’t argue with testers about bugs. I always believe the testers are right. What the testers say is what I change.

UDP also does not establish a connection, there is a port number, anyone can listen to this port number to send data to it. You can also pass data to anyone from this port number. I just send it anyway.

  • You can’t haggle

The product manager said yesterday that the phone case needs to change color according to mood, and the tester said that this bug should be fixed together with the two related bugs. Then do what they say!

UDP is also, do not understand the persistence and compromise. That is, congestion control is implemented according to network conditions. No matter how serious the network packet loss, I still send ~

UDP Application Scenarios

As I was a rookie after graduation, the leader arranged three working environments for me to choose.

  • Internal system, simple task, single module, do not need to consider the associated impact of code, even if the failure does not matter.

UDP is also required for less resources, better network conditions on the Intranet, or for packet loss insensitive applications.

  • There is a strong team support, are all senior developers, testers, team members have been dealing with each other for many years, trust each other. If there’s a problem, just yell!

UDP is also an application that does not require one-to-one communication to establish a connection and can broadcast.

  • A new project, need to have passion, for just graduated rookie, is a strong initiative, also will not play clever, hiding in the toilet to play mobile phone, paid pull shi? The instant project is not busy, so I make the best of it. Busy project, or the same dry!

UDP is also used to send packets, which is mainly used in cases where the processing speed is fast, the delay is low, and a few packets can be lost. Even if the network condition is not good, sending packets is ~

For the above three scenarios, UDP is commonly used in real-time competitive games, IoT, and mobile communications.

What are TCP’s features?

  • connection-oriented

TCP and UDP are two important protocols in the transport layer. Most interviews ask the difference between the two. TCP is connection-first, UDP is connectionless.

So what is connection-oriented?

The TCP three-way handshake is something we all repeat and recite. After three successful handshakes, the connection is established.

What is facing?

We also hear a lot about object-oriented programming, aspect programming, and service-oriented programming. So what exactly is orientation?

In my opinion, orientation is a set of things that follow certain protocols, specifications, data structures, etc.

Connection-oriented, for example, is to maintain the connection between the client and server, and establish certain data structures to maintain the state of interaction between the two sides, using such data to ensure the so-called connection-oriented characteristics.

Knowing that TCP uses a three-way handshake to establish a connection, can we have UDP send three packets to simulate TCP? It can be, but it doesn’t really mean much if you just build, not connection-oriented.

So what does TCP do for connections?

TCP provides reliable delivery. Data transmitted over a TCP connection can arrive error-free, without loss, without duplication, and in sequence. UDP inherits the features of IP packets and does not guarantee that they are not lost or arrive in sequence.

  • Word oriented stream

TCP is a byte stream. A byte stream is a stream with no head or tail. TCP maintains the flow state by itself.

UDP is based on IP datagrams, sent and received one by one.

  • Congestion control

TCP has congestion control. If a packet is discarded or the network environment is bad, TCP automatically controls its behavior according to the network condition to determine whether to send packets faster or slower.

UDP is not so intelligent, you let me send, I will send bai, anyway is you let me send, the other all ignore ~

  • Stateful service

TCP is a stateful service. Stateful means: I keep track of what was sent, what wasn’t sent, what was received, what wasn’t received, what should have been received, with no error. TCP does a lot of things!

UDP is not a stateful service. I just send it and leave the rest to the receiver. It’s a bit capricious, isn’t it?

How to make UDP implement TCP function?

Establishing a connection as mentioned above, three-way and four-way handshakes, can also be simulated using UDP.

Here are a few more questions:

  • Order problem
  • Packet loss problem
  • Flow control
  • Congestion control

The TCP data structure looks like this:

In fact, if you can explain these structures clearly, you already understand the core functions of TCP.

Let me explain the above four questions in plain English.

Sequence problems and packet loss problems can take advantage of the mechanism of acknowledgement and retransmission.

If the packet is received, you can make an acknowledgement and send an ACK to the sender to tell him that I received it. If any packages arrive early, they are cached. If a packet is missing, you can try again in timeout. The timeout retry time must be longer than RTT; otherwise, unnecessary retransmission may occur. It should not be too long. If the timeout is too long, the access will slow down. So how do you determine this time? You can take the time of sampling RTT and do a weighted average. It also needs to change dynamically according to the network condition. You can see the adaptive retransmission algorithm.

Traffic control is to adjust the rate of sending packets according to network conditions. Using a sliding window. During packet confirmation, a colleague will carry a window size. As long as the window size is well used, the packet sending rate can be well adjusted. As long as the packet segment does not exceed the window size, it is OK.

TCP’sCongestion controlIt is used to avoid packet loss and timeout retransmission. If these two phenomena occur, it indicates that the packet rate is too fast. So how do you know the sending rate in the first place? In fact, only one segment of data is sent initially, and if an acknowledgement is received, the segments are multiplied, and so on. When timeout retransmission is found, only one packet segment is sent again. This is slow start, which is not appropriate. In fact, there is a fast retransmission algorithm, in simple terms, the congestion window is halved, the subsequent linear growth. I won’t go into the details of how the algorithm is implemented.

So far, I explained the difference between UDP and TCP in plain English, as well as what UDP lacks, how to make up to achieve the function of TCP. I believe that this way of thinking can let the interviewer feel that there is something.

Shoulders of giants:

Interesting Talk Network Protocol

Computer Network