陈抒
2013-12-04 08:25:34 UTC
Hi,
I posted one question a few days ago, why my app based on boost::asio
didn't accept new
connection<http://stackoverflow.com/questions/20300926/why-my-app-based-on-boostasio-didnt-accept-new-connection>And
I thought it was solved.
But it is not, today I can re-produce the error sometimes. After
stopping/starting the 2000 client apps which connect to my TCP server. In
some cases, many connections are shown as CLOSE_WAIT.
I close the connection manually using below code:
void CloseSocket() {
try {
socket.shutdown(tcp::socket::shutdown_both);
socket.close();
BOOST_LOG_TRIVIAL(warning) << "close the connection";
} catch (std::exception& e) {
BOOST_LOG_TRIVIAL(warning) << "thread id: " << this_thread::get_id()
<< " " << e.what();
}
}
Also set address to be reused
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
Some people said it's not recommended that closing the socket manually, let
asio do it for you. But some people said you should call shutdown before
close the socket. I use the latter way, but the problem still exists.
I have posted the question on stackoverflow, but it might be the right
place to post here. To get an official answer.
Thanks.
Dean Chen
Best regards
http://blog.csdn.net/csfreebird
I posted one question a few days ago, why my app based on boost::asio
didn't accept new
connection<http://stackoverflow.com/questions/20300926/why-my-app-based-on-boostasio-didnt-accept-new-connection>And
I thought it was solved.
But it is not, today I can re-produce the error sometimes. After
stopping/starting the 2000 client apps which connect to my TCP server. In
some cases, many connections are shown as CLOSE_WAIT.
I close the connection manually using below code:
void CloseSocket() {
try {
socket.shutdown(tcp::socket::shutdown_both);
socket.close();
BOOST_LOG_TRIVIAL(warning) << "close the connection";
} catch (std::exception& e) {
BOOST_LOG_TRIVIAL(warning) << "thread id: " << this_thread::get_id()
<< " " << e.what();
}
}
Also set address to be reused
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
Some people said it's not recommended that closing the socket manually, let
asio do it for you. But some people said you should call shutdown before
close the socket. I use the latter way, but the problem still exists.
I have posted the question on stackoverflow, but it might be the right
place to post here. To get an official answer.
Thanks.
Dean Chen
Best regards
http://blog.csdn.net/csfreebird