Boost asio set_option(boost::asio::ip::udp::socket::reuse_address(true)) 错误

发布于 2025-01-17 06:43:53 字数 547 浏览 2 评论 0原文

我有两个进程,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文