
I submitted this question to boost-users Friday but it occurred to me that it should have been posted here. Apologies if I've erred. <original> I created a server something like this: handle_accept(){ do_the_right_thing(); } Class Foo { socket_acceptor acceptor_; Public: Foo(demuxer& d) : acceptor_( d, ipv4::tcp::endpoint(2525)){} Example(demuxer& d) { stream_socket* p = new stream_socket(d); acceptor_.async_accept(p, handle_accept); } } Everything works fine except when I (1) connect a client; (2) ^C the server; (3) try to restart the server - which, as expected, yields "Address already in use". Mumbling something about defaulting modern socket libraries to SO_REUSEADDR, I started trying to figure out where to call set_option(stream_socket::reuse_address(true)) - and can't work it out. I've set_option() on sockets and the acceptor but can't make the acceptor reuse the address as long as the connection that was established is still in TIME_WAIT. Do you have an example that shows how this can be done? </original> Regards, Dick Bridges

Hi Dick, --- BRIDGES Dick <Dick.Bridges@us.thalesgroup.com> wrote:
Everything works fine except when I (1) connect a client; (2) ^C the server; (3) try to restart the server - which, as expected, yields "Address already in use".
Mumbling something about defaulting modern socket libraries to SO_REUSEADDR,
Do you think I should change the default to set SO_REUSEADDR to true? At the moment I leave all options to default to whatever the OS provides. Is there a good reason why all OSes (that I know of) have it default to false I wonder? Or is it just historical?
I started trying to figure out where to call set_option(stream_socket::reuse_address(true)) - and can't work it out. I've set_option() on sockets and the acceptor but can't make the acceptor reuse the address as long as the connection that was established is still in TIME_WAIT. Do you have an example that shows how this can be done?
asio::ipv4::tcp::endpoint endpoint(port); acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::socket_acceptor::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); acceptor_.async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, asio::placeholders::error)); (From the new http example I've written: http://cvs.sourceforge.net/viewcvs.py/asio/asio/src/examples/http/server/server.cpp?rev=1.1&view=markup). Cheers, Chris

My bad for not checking it with 0.3.3 first, as this version will only compile with what's in CVS...
asio::ipv4::tcp::endpoint endpoint(port); acceptor_.open(endpoint.protocol());
This line:
acceptor_.set_option(asio::socket_acceptor::reuse_address(true));
for 0.3.3 needs to be: acceptor_.set_option(asio::socket_base::reuse_address(true)); Cheers, Chris

Hi Dick, --- BRIDGES Dick <Dick.Bridges@us.thalesgroup.com> wrote:
Everything works fine except when I (1) connect a client; (2) ^C the server; (3) try to restart the server - which, as expected, yields "Address already in use".
Mumbling something about defaulting modern socket libraries to SO_REUSEADDR,
Do you think I should change the default to set SO_REUSEADDR to true? At the moment I leave all options to default to whatever the OS provides. Is there a good reason why all OSes (that I know of) have it default to false I wonder? Or is it just historical?
I started trying to figure out where to call set_option(stream_socket::reuse_address(true)) - and can't work it out. I've set_option() on sockets and the acceptor but can't make the acceptor reuse the address as long as the connection that was established is still in TIME_WAIT. Do you have an example that shows how this can be done?
asio::ipv4::tcp::endpoint endpoint(port); acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::socket_acceptor::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); acceptor_.async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, asio::placeholders::error)); (From the new http example I've written: http://cvs.sourceforge.net/viewcvs.py/asio/asio/src/examples/http/server/server.cpp?rev=1.1&view=markup). Cheers, Chris
participants (2)
-
BRIDGES Dick
-
Christopher Kohlhoff