[英]How can I determine which port a network connection is using?
如何查询Windows以确定每个连接在C ++中使用哪个端口?
Have you tried getting the TCP table? 您是否尝试过获取TCP表?
You can ask windows for a table of all TCP connections which shows you the local address, the local port, the remote address, the remote port, and the process ID of the program that has that connection. 您可以向窗口询问所有TCP连接的表,该表向您显示本地地址,本地端口,远程地址,远程端口以及具有该连接的程序的进程ID。
Getting The Table: http://msdn.microsoft.com/en-us/library/windows/desktop/bb408406(v=vs.85).aspx 获取表: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/bb408406(v=vs.85).aspx
Table Entry: http://msdn.microsoft.com/en-us/library/windows/desktop/bb485761(v=vs.85).aspx 表条目: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/bb485761( v= vs.85).aspx
I won't try to make an example because there's a nice one at the first link. 我不会尝试举一个例子,因为在第一个链接中有一个不错的例子。 There's the equivalent for UDP as well should you need both types of connections.
如果您需要两种类型的连接,UDP也具有相同的含义。
您可以通过使用getsockname()API查询套接字绑定到的端口,然后检查sockaddr_in结构的sin_port字段。
You can use the command netstat -b -a
on Windows. 您可以在Windows上使用命令
netstat -b -a
。 If you must, this could be called as a SHELL
call. 如果需要,可以将其称为
SHELL
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.