我正在尝试检查 C++ 中是否正在使用特定端口。我不会出于任何原因试图让 C++ 程序在该端口上监听,只是检查它是否正在被使用。将有另一个程序监听该端口,如果它停止,我希望我的程序做一些事情。所以,它会每 10 秒左右检查一次,如果端口正在使用,它什么都不做,但如果端口可用,就会发生一些事情。
我一直在查看 boost ASIO 库,但我似乎无法弄清楚如何完成它。
最佳答案
这里有两个选项。
如果你真的想检查端口是否被使用,只需尝试绑定(bind):
bool port_in_use(unsigned short port) {
using namespace boost::asio;
using ip::tcp;
io_service svc;
tcp::acceptor a(svc);
boost::system::error_code ec;
a.open(tcp::v4(), ec) || a.bind({ tcp::v4(), port }, ec);
return ec == error::address_in_use;
}
现场观看: Live On Coliru ,正确打印
Port 1078 is in use
CAVEAT there could be other reasons why you can't bind to a local endpoint; check that you have the required permissions first (the permission error is being swallowed here)
如果您真的想检查连接是否被接受,则必须建立连接。这可能会更耗时,因此您可能希望在超时的情况下运行它:
bool accepting_connections(unsigned short port) {
using namespace boost::asio;
using ip::tcp;
using ec = boost::system::error_code;
bool result = false;
try
{
io_service svc;
tcp::socket s(svc);
deadline_timer tim(svc, boost::posix_time::seconds(1));
tim.async_wait([&](ec) { s.cancel(); });
s.async_connect({{}, port}, [&](ec ec) {
result = !ec;
});
svc.run();
} catch(...) { }
return result;
}
测试:
int main() {
using namespace std;
if (accepting_connections(22))
cout << "Port 22 is accepting connections\n";
}
关于c++ - 使用 C++ 和 Boost(或不使用?)检查是否正在使用特定端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358321/