简体   繁体   中英

boost asio unix socket reuse

I want to use "each io_service per worker thread" design in my program. But I cant reuse unix socket:

io_service io_1;
io_service io_2;
::unlink("/tmp/test");
stream_protocol::endpoint ep("/tmp/test");
stream_protocol::acceptor acceptor_1(io_1, ep, true);
stream_protocol::acceptor acceptor_2(io_2, ep, true);
io_1.run();
io_2.run();

fails with "address already in use". When I open my asio/basic_socket_acceptor.hpp (boost 1.46.1) I see description to acceptor's constructor:

This constructor creates an acceptor and automatically opens it to listen for new connections on the specified endpoint.

@param reuse_addr whether the constructor should set the socket option socket_base::reuse_address.

basic_socket_acceptor(boost::asio::io_service& io_service, 
    const endpoint_type& endpoint, bool reuse_addr = true)

This a boost asio bug and I should send it to the bugtracker, am I right?

Because you should use 1 acceptor on one of the threads. Instead you created two acceptors.

So you need to run 1 acceptor and spread different connection sessions on different io_service s.

Note:

io_1.run();
io_2.run();

Does not run any thread...

Re-Read the examples and try to understand them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM