5
votes

I'm working on a multi-processes socket server with the boost library.

Each process run a io_service.

I want to this processes all accept on the same port.

I know SO_REUSEPORT (after linux kernel 3.9) will help.

like this python script

import socket                                                                                                                                                       

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                                                                                                               
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)                                                                                                             

s.bind(('0.0.0.0', 9091))
s.listen(1)

while True:
    conn, addr = s.accept()
    print "new connection"
    while True:
        data = conn.recv(100)
        print "got data", data
        if not data or data == 'exit':
            break
    conn.close()

But I don't know how to use this option in boost asio io_service ?

4
You can search for this. It's been oft discussed (with the accent on when to use/correctness). In the process you can see the various ways to set reuse-addr/reuse-port. Some of this is platform dependent IIRC - sehe
@sehe i have searched on google, but no help - Yueyoum

4 Answers

5
votes

For people reading this in 2019: Asio now includes a workaround in boost/asio/detail/impl/socket_ops.ipp:

#if defined(__MACH__) && defined(__APPLE__) \
  || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
    // To implement portable behaviour for SO_REUSEADDR with UDP sockets we
    // need to also set SO_REUSEPORT on BSD-based platforms.
    if ((state & datagram_oriented)
        && level == SOL_SOCKET && optname == SO_REUSEADDR)
    {
      call_setsockopt(&msghdr::msg_namelen, s,
          SOL_SOCKET, SO_REUSEPORT, optval, optlen);
    }
#endif

So, socket_->set_option(udp::socket::reuse_address(true)); will set the SO_REUSEPORT option automatically if needed.

4
votes

Following on from how boost/asio/socket_base.hpp defines reuse_address, I did it like this:

typedef boost::asio::detail::socket_option::boolean<SOL_SOCKET, SO_REUSEPORT> reuse_port;
socket_.set_option(reuse_port(true));
3
votes

Answer by my own.

#include <iostream>
#include <string>
#include <array>
#include <boost/asio.hpp>
#include <arpa/inet.h>


using boost::asio::ip::tcp;

int main()
{
    boost::asio::io_service io;
    tcp::acceptor acceptor(io);
    acceptor.open(tcp::v4());

    int one = 1;
    setsockopt(acceptor.native_handle(), SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &one, sizeof(one));

    acceptor.bind(tcp::endpoint(tcp::v4(), 9091));
    acceptor.listen();

    std::cout << "start" << std::endl;
    for(;;)
    {
        tcp::socket socket(io);

        acceptor.accept(socket);
        std::cout << "new connections" << std::endl;
        for(;;)
        {                                                                                                                                                               
            std::array<char, 4> buf;
            boost::system::error_code error;
            boost::asio::read(socket, boost::asio::buffer(buf), error);
            if(error)
            {
                std::cout << "read error: " << error << std::endl;
                break;
            }
            std::cout << "read: " << std::string(buf.data()) << std::endl;
        }
    }
}
1
votes

The HTTP server example shows one way: http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/example/cpp11/http/server/server.cpp

  // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
  boost::asio::ip::tcp::resolver resolver(io_service_);
  boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve({address, port});
  acceptor_.open(endpoint.protocol());
  acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  acceptor_.bind(endpoint);
  acceptor_.listen();

IIRC there's also an acceptor constructor that takes a boolean argument to set the reuse flag.