简体   繁体   中英

How can I determine which port a network connection is using?

如何查询Windows以确定每个连接在C ++中使用哪个端口?

Have you tried getting the TCP table?

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.

Getting The Table: http://msdn.microsoft.com/en-us/library/windows/desktop/bb408406(v=vs.85).aspx

Table Entry: http://msdn.microsoft.com/en-us/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.

您可以通过使用getsockname()API查询套接字绑定到的端口,然后检查sockaddr_in结构的sin_port字段。

You can use the command netstat -b -a on Windows. If you must, this could be called as a SHELL call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM