Yola
Yola

Reputation: 19033

Can't connect to a port, but netstat shows what port LISTENING

While trying to connect get error:

"No connection could be made because the target machine actively refused it."

But netstat shows:

TCP    0.0.0.0:MY_PORT           MY_PC:0        LISTENING

Only problem what i can think is what app bound to the port is under step-by-step debugging, so it is paused. How i can workaround it.

Basicly i need to know somehow if somebody already bound to a port. (i can't use SO_EXCLUSIVEADDRUSE)


i think i can't connect because app what uses port is in debug paused mode, so, first time i connect then it doesn't "clear connection".

Upvotes: 0

Views: 2001

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596256

If you just want to check if a given port is in use, you can bind() to it and check for failure. If you don't want to create a socket and bind it, then you can instead loop through the arrays returned by the GetTcpTable(), GetTcpTable2(), GetTcp6Table(), GetTcp6Table2(), GetUdpTable(), and GetUdp6Table() functions. The Table2() not only report the IPs and Ports in use, but also the Process IDs that own them (from which you can then access additional information, like file names). These are the same functions that netstat uses internally to get its information.

Upvotes: 1

Related Questions