Understand TCP, UDP and port numbers in 10 minutes

Understand TCP, UDP and port numbers in 10 minutes

When we start learning network programming, we find that some terms appear very frequently, such as TCP/IP, UDP, OSI seven-layer network model, etc. This is definitely not accidental, because they are extremely important, so they are mentioned repeatedly. Especially during interviews, interviewers like to use them as an important part of examining the basic skills of the interviewee. In order to avoid making the article redundant, we will only talk about TCP, UDP and ports in the network today.

[[265976]]

Protocols, TCP and UDP

Before we understand TCP (Transmission Control Protocol) and UDP (User Datagram Protocol), we must first know a term, namely, protocol. What exactly is it? It is actually a set of conventional rules. Just like when we have a meeting, we reach some consensus, which interface to develop first, when the project will be launched, etc.

It is no exaggeration to say that the construction and development of computer networks are based on various network protocols. They define the way information is communicated and how to send and receive information, such as:

  • The IP protocol is responsible for connecting multiple packet switching networks and managing the protocol of communication IP source addresses and destination addresses.
  • TCP and UDP protocols are located between the application layer and the IP layer and are important protocols for transmitting information between them.

Since it is an agreement, everyone must abide by it. Otherwise, if Zhang San makes an agreement today and Li Si disagrees, he will make another agreement tomorrow, which will cause chaos. Therefore, different large factories and many companies have united to form an organization to standardize these agreements. You must use these prescribed agreements, otherwise you will not be able to communicate. This has realized the dream of the "global village". Today, no matter what device you use, in any corner of the world, and in any language, you can easily use the Internet to communicate.

Characteristics of TCP and UDP

To sum up TCP in a less rigorous way, if a girl meets a guy like TCP, she should marry him. Because the core feature of TCP is reliability. Its features include connection-oriented, reliable, and byte-flow-controlled transmission, which connects the application layer and the IP layer like a pipe.

In contrast, UDP is like a twenty-something prodigal. They are not reliable, but they are full of enthusiasm. They put efficiency first, and they can promise you many things immediately and are willing to put them into practice immediately. If you say that Buddhist beads are romantic, he will immediately take a plane to pick them up for you no matter where he is. If you say that you want to go to Jay Chou's concert, he will immediately book tickets for you. Of course, people's energy and time are limited, so uncontrollable accidents may occur, such as not having enough money to buy tickets. This is similar to UDP, which provides connectionless communication, but does not provide reliability guarantees for the transmitted data packets.

Summary of the characteristics of TCP and UDP

TCP and UDP usage scenarios

Different people have different personalities and do things in different ways. You can't say this is definitely good and that is definitely bad. The same goes for TCP and UDP. They each have their own uses in different scenarios.

For example, when developing a game program, for players to log in to their accounts, we need to ensure that different players can log in to their own accounts, so we need to apply the TCP protocol. For controlling the movement of the characters in the game, we only need to move the characters according to the mouse clicks of the players. If there is a small probability of network problems, the players can just move the characters again. The cost and risk are within a controllable range, so the UDP protocol can be used.

From the above, we can see that if it is a basic service function, the TCP protocol should be used to ensure the reliability of the service. To ensure that each network packet can reach the destination correctly during the process. Compared with UDP, programmers need to further handle packet loss and byte transmission order by themselves.

Don't think that UDP is difficult to use. On the contrary, there are actually a lot of services based on the UDP protocol. For example, DNS resolution service. Every computer that accesses an external web page needs to resolve the domain name through the DNS service and find the corresponding IP address. At this time, if there is a problem with the DNS, we actually only need to perform a DNS address query again. The only side effect may be a slight delay.

[[265977]]

Another application of UDP is IP telephony, which we often call VoIP (Voice over IP). Its principle can be simplified as transferring phone calls from traditional telephone communication to network communication, similar to our current WeChat calls. When you talk to someone, you use UDP. Imagine that what we need at this time is not reliability, but real-time performance. If unfortunately there is a little information loss in the middle, for example, your mother said: "Bring the object back to me during the Chinese New Year, otherwise don't come back." Due to the loss of part of the information, like this - "Bring the object back during the Chinese New Year, otherwise don't come back", in fact, we can understand the meaning of the whole sentence. If we use TCP, we may say a sentence, and others can only hear it 30 seconds later, which is obviously unbearable.

Therefore, we understand that in the daily development process, we should choose the corresponding TCP or UDP as the network transmission protocol according to different application scenarios, and we should not resist it psychologically because we are not familiar with UDP.

Ports and their characteristics

If IP is used to locate a block, then the port is the house number corresponding to each household in the block. In the communication process, after the data finally reaches the device (such as a computer) through various communication protocols, the device here is equivalent to a block, and there are many programs running inside the device computer. After the data comes in, it must be given a corresponding house number (that is, port number) so that the program can perform subsequent operations conveniently.

The port number is part of the transmission protocol, so we can say that data is sent to the specified device through the IP address, and the data is sent to the specified service or program through the port number.

Programs usually not only listen to the specified port number, but also specify the corresponding transmission protocol. Therefore, when we transmit data, we must specify both the corresponding port number and the corresponding communication protocol. Many people will simply say: Program A listens to port 33001, which is incorrect, at least not completely correct. Instead, we should say: Program A uses the TCP protocol and listens to port 33001. Of course, you can also say: Program A uses the UDP protocol and listens to port 33001.

The obvious benefit of specifying the transmission protocol and port is that when we forward ports or build a network firewall, we can easily isolate them by protocol and port to prevent unforeseen accidents. For computers, this method can prevent all kinds of unnecessary data from the external network from entering the local area network.

You may think that if everyone uses the same port number, there will be conflicts. That's right, a special organization is needed to manage them, IANA (Internet Assigned Numbers Authority), which is responsible for managing port registration. Most mainstream programs have a clear registered port, such as the common FTP listening on port 20 and 21, and HTTP service listening on port 80. If a program wants to register a port, IANA will first check whether the port has been registered. If it has been registered, it will reject the application.

Port numbers are divided into three types according to their range

1. Well-Known Ports

These are some well-known port numbers that are fixedly assigned to some services. The HTTP service and FTP service mentioned above belong to this category. The range of well-known port numbers is: 0-1023.

2. Registered Ports

It is a port segment that cannot be adjusted dynamically. These ports do not clearly define which specific objects they serve. Different programs can define them according to their needs. The range of registered port numbers is: 1024-49151.

3. Dynamic, private or ephemeral ports

As the name implies, these port numbers cannot be registered. This section of ports is used for some private or customized services. Of course, it can also be used for dynamic port services. The range of this section is: 49152-65535.

<<:  How do you know if you are suitable for Wi-Fi 6 when it is about to be widely adopted?

>>:  A collection of network failure and cause analysis (with solutions included)

Recommend

IMIDC Hong Kong/Taiwan Server E3 Series $90 off per month starting at $39/month

IMIDC is a local operator in Hong Kong. It has br...

Where will edge computing investments go?

At the beginning of 2020, edge computing seemed t...

The secrets behind the IoT strategy of Internet giants at the end of 2017

2017 is coming to an end. There is no doubt that ...

5G is here: Now how will we make it work?

They say honeymooning in Niagara Falls sets you u...

The three major telecom operators earned 387 million yuan a day in 2020

On March 25, China Mobile released its 2020 perfo...

A large wave of policies is coming, what are you waiting for, IoT people!

On April 23, General Secretary Xi Jinping emphasi...

...

8 essential skills for network engineers in 2017

The current average job responsibilities of a net...