给定的程序使用什么端口?

我希望能够找出一个特定的程序正在使用什么端口。是否有任何程序可在线或随附的窗口,将告诉我哪些进程正在使用我的计算机上的端口?

另外,在你因为这不是一个编程问题而感到失望之前,我正在寻找这个程序来测试一些网络代码。

225892 次浏览

most decent firewall programs should allow you to access this information. I know that Agnitum OutpostPro Firewall does.

At a command line, netstat -a will give you lots o' info.

Windows comes with the netstat utility, which should do exactly what you want.

netstat -b -a lists the ports in use and gives you the executable that's using each one. I believe you need to be in the administrator group to do this, and I don't know what security implications there are on Vista.

I usually add -n as well to make it a little faster, but adding -b can make it quite slow.

Edit: If you need more functionality than netstat provides, vasac suggests that you try TCPView.

You can use the 'netstat' command for this. There's a description of doing this sort of thing here.

TCPView can do what you asked for.

"netstat -natp" is what I always use.

If your prefer a GUI interface CurrPorts is free and works with all versions of windows. Shows ports and what process has them open.

You may already have Process Explorer (from Sysinternals, now part of Microsoft) installed. If not, go ahead and install it now -- it's just that cool.

In Process Explorer: locate the process in question, right-click and select the TCP/IP tab. It will even show you, for each socket, a stack trace representing the code that opened that socket.

On Vista, you do need elevated privileges to use the -b option with netstat. To get around that, you could run "netstat -ano" which will show all open ports along with the associated process id. You could then use tasklist to lookup which process has the corresponding id.

C:\>netstat -ano


Active Connections


Proto  Local Address          Foreign Address        State           PID
...
TCP    [::]:49335             [::]:0                 LISTENING       1056
...


C:\>tasklist /fi "pid eq 1056"


Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
sqlservr.exe                  1056 Services                   0     66,192 K

Windows 8 (and likely 7 + Vista) also provide a view in Resource Monitor. If you select the Network tab, there's a section called 'Listening Ports'. Can sort by port number, and see which process is using it.