Can there ever be more than one program running on a single port? In other words, can two applications on the same server have the same port number?
Asked
Active
Viewed 79 times
1
-
Try running `new ServerSocket(10000);new ServerSocket(10000);` – Sotirios Delimanolis Mar 02 '14 at 21:02
-
possible duplicate of [Socket options SO\_REUSEADDR and SO\_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?](http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t) – Jason C Mar 02 '14 at 21:18
2 Answers
3
If they're bound to different IPs, yes. Binding to a port binds to both a IP address and a port number. So if application A is bound to 192.168.0.56:25565
, and application B is bound to 127.0.0.1:25565
they'll be no issue. But if application C wants to bind to either of those, or bind to 0.0.0.0:25565
, it'll be given a error.

Zoey Mertes
- 3,139
- 19
- 23
-
How do you make applications on the same server bind to different IPs? – Sotirios Delimanolis Mar 02 '14 at 21:08
-
-
1@SotiriosDelimanolis In Java you can specify an `InetSocketAddress` to the various socket constructors; the interface address can be specified there. – Jason C Mar 02 '14 at 21:17
2
For TCP: Yes, as long as they are bound to different interfaces (e.g. eth0
and loopback
). On the same interface, no.
For UDP: Yes.

NPE
- 486,780
- 108
- 951
- 1,012
-
If I remember right, this is true for TCP, but not for UDP, but my recollection might be wrong. – jpw Mar 02 '14 at 21:06