I am trying to check if a specific port is being used in C++. I am not trying to have the C++ program listen on that port for any reason, just check to see if it is being used. There will be another program listening on that port, and if it stops, I want my program to do something. So, it will check every 10 seconds or so, and if the port is in use, it will do nothing, but if the port becomes available, something will happen.
I've been looking at the boost ASIO library, but I can't seem to figure out how to accomplish this.
There's two options here.
If you actually want to check the port is use, simply attempt a bind:
See it live: Live On Coliru, correctly printing
If you actually want to check that connections are being accepted, you will have to make a connection. This could be somewhat more time consuming so you might want to run this under a timeout:
Test: