Boost asio set_option(boost::asio::ip::udp::socket::reuse_address(true)) 错误
我有两个进程,在ubuntu 18.04中,都使用相同的端口。所以,我设置了它们重用地址。
一个使用套接字,代码是
int flag = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &flag, sizeof(flag);
另一个使用boost::asio,代码是
boost::asio::io_service io_service_;
std::unique_ptr<boost::asio::ip::udp::socket> ins;
ins = std::make_unique<boost::asio::ip::udp::socket>(io_service_);
ins->set_option(boost::asio::ip::udp::socket::reuse_address(true));
当我一起运行两个代码时,在后期运行的程序之一中发生错误。“地址已在使用中”。但是,当我运行代码之一两次。没关系。
I have two processes,in ubuntu 18.04,both use the same port. So, I set them reuse address.
one use socket, the code is
int flag = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &flag, sizeof(flag);
and other use boost::asio, the code is
boost::asio::io_service io_service_;
std::unique_ptr<boost::asio::ip::udp::socket> ins;
ins = std::make_unique<boost::asio::ip::udp::socket>(io_service_);
ins->set_option(boost::asio::ip::udp::socket::reuse_address(true));
when I run the two codes together, An error occurred in one of the late running programs."Address already in use".But, when I run the one of code twice. it' ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论