'Boost asio set_option(boost::asio::ip::udp::socket::reuse_address(true)) error

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source