The image below shows 2 processes that attempted and successfully bound listen sockets (server) to port 10000 on my local machine:

and here’s the output of netstat (for confirmation):
netstat -a -n | find "10000"
TCP 0.0.0.0:10000 0.0.0.0:0 LISTENING
TCP 0.0.0.0:10000 0.0.0.0:0 LISTENING
TCP [::]:10000 [::]:0 LISTENING
(NB: The javaw.exe process was the first to open the listen socket on 10000).
While I am aware of situations under which multiple processes can indeed listen on the same port (SO_REUSEADDR), there are certain things that bother me in my specific scenario:
-
In my application, I specifically say to .NET that I want an exclusive listen socket (
SO_EXCLUSIVEADDRUSE) vialistener = new TcpListener(adr, ipport); listener.ExclusiveAddressUse = true; -
.NET does not throw any kind of exception / error / notification that the port is already in use. In fact, it actually believes that everything went fine.
-
My server application never wakes up from the
listener.AcceptTcpClient()call from this point on. The client application that is supposed to communicate with the server receives a valid connection but is unable to communicate with “my” server (supposedly because it establishes a connection to the “other” process which doesn’t speak its “protocol”).
In case someone wants to try and reproduce my findings: The 2nd process is the “Helios” release of Eclipse (PHP). But the specific process shouldn’t matter here: If one process can do weird things under an OS, so can others.
Any advice on how I can either get an error or prevent such a situation (by means of additional parameters) altogether?
The MSDN link you posted in your comment seems to answer your question.
It has to do with the java application binding to the wildcard IP endpoint (
0.0.0.0ip4;::for ip6, orIPEndpoint.ANY). I’m assuming that theadrvariable in your code snippet above is a specific IP address, and not also a wildcard address.Take a look at the tables in that article. It lists the outcomes of trying to bind to either a specific or wildcard endpoint a second time, with different combinations of socket options.
In short, the Java code is binding on the wildcard
0.0.0.0endpoint without theSO_EXCLUSIVEADDRUSEsocket option. The matrix shows that when this happens, you can successfully bind to a specific address and pass requesting exclusive address use.If you were to try and bind with a wildcard, the table shows that the call would fail.