I posted one question a few days ago, why my app based on boost::asio didn't accept new connection And I thought it was solved.
But it is not, today I can reproduce 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.