Ever tried to start a process on Windows, only to be told that the network port it needs is unavailable?
TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Production
Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=mypc.oracle.com)(PORT=1521)))
TNS-12542: TNS:address already in use
Bugger. Now, which process is using that port? The task manager doesn't show you any of these sorts of details.
There's two ways I know of to work out which process which already has the port open.
1. If you have cygwin or MKS or some Unix styled set of command utilities -and who using Windows commands shells wouldn't? -- then you can use the grep and ps commands it provides with the standard netstat command.
First off, use the netstat command with the -ao switch and pipe it into grep to locate the listening details. The -o switch tells netstat to output the process-id of the owner of the port.
D:\>netstat -ao | grep 1521
TCP mypc:1521 mypc:0 LISTENING 2944
Then using the ps command, search for the process-id:
D:\>ps -eaf | grep 2944
SYSTEM 2944 1488 0 11:37:44 CONIN$ 0:00 vmware-authd
This could be combined into a nice awk script I suspect.
2. Use the very handy SysInternals utilities to locate the owning process. These used to be available on the sysinternals.com website, but the site now redirects to Microsoft, so presumably they have purchased them. Good idea Microsoft.
The specific utility for this operation is TCPView:
This comes in two flavours, GUI and cmd line and identifies each process that is using a network port. The GUI is easy to use, just fire it up, look for the port in question which then identifies the process in the process table.
I prefer using the command line utility, tcpvcon.exe.
This results in a nicely formatted text output that shows the process and network port details for all processes that have a network port in use in some manner.
To make it easier to identify a specific process which owns a specific port, then you can ask the output to be provided in CSV format, which lists each process on a single line, and which you can then pipe into find to locate a specific port
D:\>tcpvcon.exe -c -a | find "1521"
Listening to: John Butler Trio - Good Excuse