views:

487

answers:

3

The cross platform dev environment I use has a built in function that is supposed to indicate whether or not a tcp port is available. However, the function malfunctions under Vista and always returns "available".

Is there an easy way, such as a dll function, I can use to correctly determine this information without needing .Net?

+3  A: 

You can attempt to bind to and listen on the port using the usual Winsock functions. If something else is already listening on the same port, you will get an error (WSAEADDRINUSE from bind()).

Greg Hewgill
You may have to enumerate through all network interfaces and bind to them. I don't know about Windows, but Linux certainly allows multiple applications to bind and listen on the same port, provide they use different local IP addresses.
Tom
@Tom: I suppose it depends on whether the goal is to find out whether anything else is using a given port, or whether your application can listen on a given port. My solution would work for the latter question.
Greg Hewgill
A: 

To avoid bringing in added dependencies or windows-specific code you could simply try to use the port and catch any problems (such as interception by firewall software).

SpliFF
A: 

Simply use Win32 Nw apis to enumerate TCP ports.