Jay
Jay

Reputation: 10460

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

How can Windows be queried to determine which port each connection is using in C++?

Upvotes: 0

Views: 830

Answers (3)

HerrJoebob
HerrJoebob

Reputation: 2313

You can query what port a socket is bound to by using the getsockname() API and then checking the sin_port field of the sockaddr_in structure.

Upvotes: 2

Tocs
Tocs

Reputation: 752

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.

Upvotes: 3

Austin Henley
Austin Henley

Reputation: 4633

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

Upvotes: 1

Related Questions