c++ - 使用 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 之外没有其他信息。这只是无效参数的编号。
尝试分配我的机器上没有的虚假地址会产生不同的错误“绑定:无法分配请求的地址”,所以猜测我传入的地址没问题。
这是这样做的正确方法吗?
解决方案
推荐阅读
- postgresql - 线性回归 - 参考 madlib 的基线
- unity3d - 粒子/标准不亮在不同场景中表现不同
- python - 如何用 Python 绘图的 True、False 和颜色代码标记 Y 轴?
- java - 休眠性能 GetterFieldImpl
- php - 正则表达式前 3 个字母以某种方式表示某事
- ios - 创建子 UIView 的 UIView 正在生成层次约束警告
- python - 编写 Python 脚本来自动化 llvm 代码覆盖率测试
- java - 使用 Kafka-Spark Streaming API 处理流数据时重复
- ios - 如何访问创建的目录?- NSCocoaErrorDomain 代码=257
- javascript - Javascript:数组不保留值