Why can I use the same port at the same time in TCP and UDP?

I saw during the search that you can use two different programs on the same computer, exchanging over the network using the same port and the same network interface, provided that one uses UDP and the other TCP. However, I did not get a good explanation of how this works and why this is possible?

It is also possible that several programs use the same UDP port, because UDP does not establish a real connection between peers, but simply sends packets to the address? I understand that this is not possible with TCP, as it creates a synchronized connection between the server and the client, but what about UDP?

Please explain in detail, if possible, or a link to a good article on this topic.

+9
source share
4 answers

Other answers are correct, but somewhat incomplete.

The “connection” of an IP socket (in other words, “INET”) (that is, the connection between two processes, possibly on different machines) is determined by the 5th tuple: protocol, source address, source port, destination address, destination port. You can see that this is not limited to connecting to a state such as TCP.

This means that you can bind different processes to any unique instance of this 5th tuple. Since the “protocol” (for example, TCP and UDP) is part of the differentiating factor, each can have its own process.

Theoretically, you can associate different services with the same TCP port if they communicate with different interfaces (network cards, cable, etc.), although I have never tried.

However, it is standard practice to always use the same service on the same port number. If both UDP and TCP are supported, these are simply different ways of communicating with the same service. DNS, for example, uses UDP for port 53 to search because it is small queries, and it is faster than creating a TCP connection, but DNS also uses TCP for port 53 for "transfers", which are rare and can contain large amounts of data.

Finally, with complete accuracy, this is not necessarily a 5-tuple. IP uses a “protocol” to go to the next level, such as TCP and UDP, although there are others. TCP and UDP separately distinguish between connections based on the remaining 4 elements. You can create other protocols over IP that use completely different (possibly without ports) differentiation mechanisms.

In addition, there are different "domain" sockets, such as the domain of socket "unix", which is completely different from the "inet" and uses the file system for addressing.

+12
source

Destination is not identified by IP address: port one. Another thing is that the IP header has a field called Protocol that distinguishes between the TCP and UDP endpoints. Thus, it becomes possible for two processes to communicate with the same IP port, because the communication protocol is different.

+3
source

The connection endpoint is for UDP and TCP defined by IP, protocol (TCP or UDP) and port. This means that if you use a different protocol, the endpoint of the connection is also different.

+1
source

Because they are not the only component of addressing tools. This is the same as why you can have two houses with the same number on different streets, or why you know that John Warfin is not the same Red Lectroid as John Bigbot.

Each IP packet contains a field that indicates which transport layer protocol should be used, and in the domain of this protocol there is a set of ports that can be the same as in any other protocol, since they are actually a completely separate set.

Regarding the second question, there are answers elsewhere.

0
source

Source: https://habr.com/ru/post/984983/


All Articles