16

I've seen while searching that it is possible to use two different programs on the same computer communicating over the network using the same port and same network interface provided one use UDP and the other TCP. However I didn't get a good explanation, how does it actually work and why this is possible?

Is it also possible for multiple programs to use the same UDP port since UDP does not establish a real connection between the peers, but just sends the packets to an address? I understand it's not possible with TCP as it creates a synchronized connection between the server and the client, but what about UDP?

Please explain in details if possible, or link a good article on the topic.

Joel Cunningham
  • 641
  • 6
  • 17
Xsmael
  • 3,624
  • 7
  • 44
  • 60
  • 1
    This is equivalent to asking why it's possible to have 5 apples and 5 oranges at the same time. – Oliver Charlesworth Apr 11 '15 at 10:00
  • 1
    okay, how about my 2nd question ? – Xsmael Apr 11 '15 at 10:25
  • 1
    @OliverCharlesworth, not at all. It's all about what differentiates a "socket" endpoint. His question is simply what factors are part of that differentiation. Yes, TCP is an "apple" and UDP is an "orange" but it could just have easily been both are only "fruit" to a socket. – Brian White Apr 11 '15 at 13:34
  • @Brian from the point of view of sockets, I guess that's fair. My comment was referring to the fact that TCP and UDP have independent notions of ports (each implements ports in its own packet header format). – Oliver Charlesworth Apr 11 '15 at 13:38
  • A *port* is not the same as a *port number*. Many different ports can share the same port number. – David Schwartz May 16 '19 at 21:25
  • @DavidSchwartz Would you care to elaborate ? – Xsmael May 17 '19 at 19:07
  • 2
    @Xsmael A "port" is a logical concept -- it's a named way to accept or originate connections or data. As it happens, TCP ports have port numbers. Also, UDP ports have port numbers. But there is no such thing as "the same port on TCP and UDP" because a TCP port is never the same port as a UDP port because one accepts/originates TCP connections and the other accepts/originates UDP data. That makes them different ports. A computer can even have two distinct UDP port 80s, for example, one bound to 127.0.0.1 and the other bound to 127.0.0.2 -- they are distinct ports. – David Schwartz May 17 '19 at 20:44
  • @DavidSchwartz, thanks for the extra clarity :) .So, are 127.0.0.1 and 127.0.0.2 two different interfaces ? – Xsmael May 17 '19 at 23:29
  • 3
    @Xsmael They're typically two different IP addresses bound to the loopback interface. But if you have two sockets, one bound to UDP port 80 with IP 127.0.0.1 and one bound to UDP port 80 with IP 127.0.0.2, they are bound to different ports. Incoming traffic that is addressed to one port cannot possibly be addressed to the other. Think of it as docking port 2 north and docking port 2 south. They have the same port number but are different ports. – David Schwartz May 18 '19 at 01:09

4 Answers4

23

The other answers are correct but somewhat incomplete.

An IP (aka "INET") socket "connection" (i.e. communication between two processes, possibly on different machines) is defined by a 5-tuple: protocol, source address, source port, destination address, destination port. You can see that this is not limited to a stateful connection such as TCP.

This means that you can bind different processes to any unique instance of that 5-tuple. Because the "protocol" (e.g. TCP and UDP) is part of the differentiating factor, each can have a different process.

Theoretically, you could bind different services to the same TCP port if they bind to different interfaces (network cards, loopback, etc.) though I've never tried it.

It is standard practice, however, to always use the same service on the same port number. If both UDP and TCP are supported, they're just different ways of communicating with that same service. DNS, for example, uses UDP on port 53 for lookup because they are small requests and it's faster than creating a TCP connection but DNS also uses TCP on port 53 for "transfers" which are infrequent and can have large amounts of data.

Lastly, in complete accuracy, it isn't necessarily a 5-tuple. IP uses the "protocol" to pass to the next layer such as TCP and UDP though there are others. TCP and UDP each seperately differentiate connections based on the remaining 4 items. It's possible to create other protocols on top of IP that use completely different (perhaps port-less) differentiation mechanisms.

And then there are different socket "domains", such as the "unix" socket domain, which is completely distinct from "inet" and uses the filesystem for addressing.

Brian White
  • 8,332
  • 2
  • 43
  • 67
  • _Theoretically, you could bind different services to the same TCP port if they bind to different interfaces (network cards, etc.) though I've never tried it._ Actually that's right, i know it, since you can connect the network cards to different network, the communications are independent. – Xsmael Apr 11 '15 at 13:39
  • _This means that you can bind different processes to any unique instance of that 5-tuple._ Does it mean i can listen on same port and same protocol (let's say) TCP but different destination ip ? – Xsmael Apr 11 '15 at 13:41
  • Your two comments are the same. An IP address is associated with a network interface. You _should_ be able to bind different processes to the same TCP port number on different IP addresses... but I've never tried it. Seems a bad idea. – Brian White Apr 11 '15 at 13:45
  • Actually my second comment is different. As i understood (if i'm wrong tell me) **source ip** is the ip of the local computer on one interface, and **destination ip** is the ip of the remote computer hence does not rely on local computer interfaces. if i use different **source ip** it means different network interface then it will work but how about different **destination ip** only ? according to your 5-tuple approach – Xsmael Apr 11 '15 at 13:55
  • 1
    Binding to a specific IP/interface for outgoing socket is not any different that binding to a specific IP/interface for a listening socket. For outgoing sockets, you typically use port #0 to have the system pick an unused one while listening sockets usually want to use a well-defined port number. But conceptually it's the same. So yes, _theoretically_ it's possible to have completely different services on the same machine and on the same TCP port number but bound to different IP addresses. It's uncommon (to say the least) so other limitations (or outright OS bugs) could interfere. – Brian White Apr 11 '15 at 14:06
  • More accurate description is needed. In the case of UDP, a socket is not *uniquely identified* by 4 values but only two values (dest ip, dest port). And in TCP world, even if it is possible *theoretically* that one socket can bind to same local ip and same local port if dest ip or dest port is different, in real world it is NOT possible a socket bind to same local ip and same local port unless it is not server socket. – obanadingyo Apr 22 '23 at 01:55
  • UDP definitely has all 4 values. It isn't reported via `recvfrom` but can be retrieved using other methods such as IP_PKTINFO (linux) or IP_RECVDSTADDR (FreeBSD). In general, though, nobody ever bothers. And because UDP is connectionless, there's not state associated with any of the 4 values; they're just made available to the application. Also, I didn't say that one socket can bind multiple times; I said that you could bind multiple sockets to the same port number so long as the local IP address of each is different. – Brian White Apr 22 '23 at 02:11
5

The destination isn't identified by IP Addr:Port alone. There is another thing - IP header has a field called Protocol which differentiates the TCP and UDP endpoint. As such it becomes possible for two process to bind to same IP:Port as long as communication protocol is different.

Prabhu
  • 3,443
  • 15
  • 26
1

The endpoint of a connection is for UDP and TCP defined by IP, protocol (TCP or UDP) and port. This means as long as you use a different protocol the endpoint of the communication is different too.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
0

Because they are not the only component of the means of address. It's the same as why you can have two houses with the same number on different streets, or why you know John Whorfin is not the same Red Lectroid as John Bigbooté.

Each IP packet contains a field that says which transport-layer protocol is to be used, and within the domain of that protocol is a set of ports that can be the same as in any other protocol because they are actually a completely separate set.

As for the second question, there are answers elsewhere.

Blair Houghton
  • 467
  • 3
  • 10