首页 > 解决方案 > 使用 Websocket++ 绑定到特定的 IP 地址

问题描述

使用 websocket++ 作为客户端到 websocket 服务器我想使用我的客户端服务器的特定 IP 进行连接。所以我想弄清楚如何绑定到特定的 IP,但它没有成功。

我认为 on_socket_init 处理程序可能用于配置套接字,但是这个处理程序:

void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &s) {
    boost::asio::ip::tcp::endpoint localEndpoint(
            boost::asio::ip::address::from_string("192.168.178.28"), 0);

    std::cout << "Before bind is done " << s.lowest_layer().local_endpoint().address().to_string() << std::endl;
    s.lowest_layer().bind(localEndpoint);
    std::cout << "Bind is done " << s.lowest_layer().local_endpoint().address().to_string() << std::endl;

导致这种情况发生:

Before bind is done 192.168.178.28
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  bind: Invalid argument

在我的本地机器上,我没有多个 IP,所以我只是想告诉它绑定到我拥有的一个 IP,它现在不应该改变任何东西,除了验证我可以调用 bind()。但是它爆炸了,我不知道为什么。在具有第二个 IP 的服务器上测试我得到了同样的错误。

使用 gdb 我只发现异常是由检查某些内部绑定调用的某些错误代码的函数引发的。我不知道该内部绑定调用的代码在哪里,除了“无效参数”或错误代码 22 之外没有其他信息。这只是无效参数的编号。

尝试分配我的机器上没有的虚假地址会产生不同的错误“绑定:无法分配请求的地址”,所以猜测我传入的地址没问题。

这是这样做的正确方法吗?

标签: c++sslboostboost-asiowebsocket++

解决方案


推荐阅读