使用C ++和升压(或不?),以检查是否正在使用一个特定的端口? [英] Using C++ and Boost (or not?) to check if a specific port is being used?

查看:144
本文介绍了使用C ++和升压(或不?),以检查是否正在使用一个特定的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否在C ++中使用特定端口。我不是有C ++程序侦听端口以任何原因,只检查是否正在使用它。将有另一个程序侦听该端口,如果它停了,我想我的程序做一些事情。因此,它会检查每10秒左右,如果端口在使用中,它会做什么,但如果端口可用时,会发生点什么。

我一直在寻找升压ASIO库,但我似乎无法弄清楚如何做到这一点。


解决方案

这里有两个选项。

如果你真的想检查端口使用时,只需尝试绑定:

 布尔port_in_use(无符号短端口){
    使用空间boost :: ASIO;
    使用IP :: TCP;    io_service对象SVC;
    TCP ::受体一个(SVC);    提高::系统::错误_ code EC;
    a.open(TCP :: V4(),EC)|| a.bind({TCP :: V4(),端口},EC);    返回EC ==错误:: ADDRESS_IN_USE;
}

亲身体验: <大骨节病> 住在Coliru ,正确打印

 端口1078是在使用


  

CAVEAT 有可能是你为什么不能绑定到本地端点的其他原因;检查是否有所需的权限第一(这里正在吞噬权限错误)


如果你真的想检查连接被接受,你将不得不进行连接。这可能是较为耗时,所以你可能要超时下运行这样的:

 布尔accepting_connections(无符号短端口){
    使用空间boost :: ASIO;
    使用IP :: TCP;
    使用EC =的boost ::系统::错误_ code;    布尔结果= FALSE;    尝试
    {
        io_service对象SVC;
        TCP ::插座S(SVC);
        deadline_timer恬(SVC,提振::了posix_time ::秒(1));        tim.async_wait([安培](EC){s.cancel();});
        s.async_connect({{}}端口,[&安培;(EC指令){
                结果= EC!;
            });        svc.run();
    }赶上(...){}    返回结果;
}

测试:

  INT的main(){
    使用命名空间std;    如果(accepting_connections(22))
        COUT&LT;&LT; 22号端口接受连接\\ N的;
}

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:

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;
}

See it live: Live On Coliru, correctly printing

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)

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:

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;
}

Test:

int main() {
    using namespace std;

    if (accepting_connections(22))
        cout << "Port 22 is accepting connections\n";
}

这篇关于使用C ++和升压(或不?),以检查是否正在使用一个特定的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆