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?