Question

Is there a way to check availability of a port number, without connecting, listening or binding?

Is there any function like bool CheckPort(UINT port_num)?

Was it helpful?

Solution

Without any connect/listen/bind, you need some OS magic to do it. For example in Windows you can try functions like GetExtendedTcpTable or GetExtendedUdpTable.

The GetExtendedUdpTable/GetExtendedTcpTable function retrieves a table that contains a list of UDP/TCP endpoints available to the application.

These retrieve lists so that you can iterate over it and get useful information. Check if any executing program is listening to that specific port or not.

OTHER TIPS

Maybe you can try this: If you bind to port 0, a random port will be allocated. Then the getsockname() function can be used to find out the actual port used.

The simple answer is no, because not connecting to a socket while still trying to check for availability doesn't really make much sense. How are you guaranteed that nobody else used that port while you're checking? You must connect to the port first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top