Thursday, 24 June 2010

What is using a port on my machine?

Problem

My service connects to a TCP server over a particular port and when establishing the connection throws the exception: “SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted”.

Diagnosing the Issue

Use netstat at the command line to see what is using the port.

netstat –a
Displays all the ports in use.

netstat –b
Displays all the active connections and the process/exe that is using them

In my case a network error had occurred and I believe my service had left the client open, knew it was dead and so was trying to reconnect – throwing the exception.  Restarting the service resolved the issue but the code will need reviewing.