17

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.

9
  • 1
    This is equivalent to asking why it's possible to have 5 apples and 5 oranges at the same time. Apr 11, 2015 at 10:00
  • 1
    okay, how about my 2nd question ?
    – Xsmael
    Apr 11, 2015 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. Apr 11, 2015 at 13:34
  • 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. May 17, 2019 at 20:44
  • 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. May 18, 2019 at 1:09

4 Answers 4

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.

7
  • 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, 2015 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, 2015 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. Apr 11, 2015 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, 2015 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. Apr 11, 2015 at 14:06
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.

3
  • so i can listen on port 85 for TCP and on the same port 85 for HTTP simultaneously ?
    – Xsmael
    May 17, 2019 at 19:14
  • No. HTTP is a application level protocol that uses TCP.
    – Prabhu
    May 18, 2019 at 4:07
  • so, transport layer protocol, are the basis ? we have to check only at that level right?
    – Xsmael
    May 18, 2019 at 15:52
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.