[英]Boost.asio client/server TIME_WAIT why?
我正在嘗試對某些API進行一些功能測試。
我的API有一個客戶端和一個服務器端。 客戶端只是連接並設置一個標志。 服務器僅接受連接。
這是我有一個測試用例:
BOOST_AUTO_TEST_CASE(client_can_connect_to_server) {
boost::asio::io_service serverService;
std::thread serverLoop([&serverService] { serverService.run(); });
boost::asio::io_service clientService;
std::thread clientLoop([&clientService] { clientService.run(); });
// std::this_thread::sleep_for(10ms); Maybe wait for server loop to start...?
auto connectionSuccess = connectTo("127.0.0.1", "54321", kAuthData, ioService);
BOOST_REQUIRE(blockForDurationOrWhile
(timeout,
[&] { return connectionSuccess.wait_for(0s) != std::future_status::ready; }) == ExitStatus::ConditionSatisfied);
serverService.stop();
clientLoop.join();
serverService.join();
}
我在這里遇到兩件事:
通過成功的路徑完成程序直到測試結束時,netstat似乎顯示了某種類型的套接字泄漏,狀態為TIME_WAIT
。 我正在關閉和關閉套接字。 我只是不知道出什么問題了。 在應用退出后,顯示時間約為30-45秒:
tcp 0 0 ip6-localhost:52256 ip6-localhost:54321 TIME_WAIT
tcp 0 0 ip6-localhost:54321 ip6-localhost:52256 TIME_WAIT
客戶端和服務器代碼的代碼如下:
std::future<bool> connectTo(std::string const & host,
std::string const & port,
std::string const & authData,
boost::asio::io_service & s,
std::chrono::high_resolution_clock::duration timeout = kCortexTryConnectTimeout) {
using namespace boost::asio;
using boost::asio::ip::tcp;
std::promise<bool> p;
auto res = p.get_future();
spawn
(s,
[&s, host, port, p = std::move(p)](yield_context yield) mutable {
tcp::socket socket(s);
BOOST_SCOPE_EXIT(&socket) {
std::cout << "Closing client socket\n";
if (socket.is_open()) {
boost::system::error_code ec{};
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket.close();
std::cout << "Client socket closed\n";
}
} BOOST_SCOPE_EXIT_END
std::cout << "Client trying to connect\n";
tcp::resolver resolver(s);
boost::system::error_code ec{boost::asio::error::operation_aborted};
boost::asio::async_connect(socket, resolver.resolve({host, port}), yield[ec]);
std::cout << "Client Connected\n";
if (!ec) p.set_value(true);
else p.set_value(false);
});
return res;
}
服務器處理連接:
class ConnectionsAcceptorTask {
public:
//Session handling for Cortex. Will move out of here
class Session : public std::enable_shared_from_this<Session> {
public:
explicit Session(boost::asio::ip::tcp::socket socket) : _socket(std::move(socket)) {}
void start() {}
~Session() {
if (_socket.is_open()) {
boost::system::error_code ec{};
_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
_socket.close();
}
}
private:
boost::asio::ip::tcp::socket _socket;
};
ConnectionsAcceptorTask(unsigned int port,
io_service & s)
: _port(port),
_ioService(&s)
{}
void operator()() {
namespace ba = boost::asio;
using boost::asio::ip::tcp;
ba::spawn
(*_ioService,
[s = _ioService, port = this->_port](ba::yield_context yield) {
tcp::acceptor acceptor
(*s,
tcp::endpoint(tcp::v4(), port));
acceptor.set_option(boost::asio::socket_base::reuse_address(true));
BOOST_SCOPE_EXIT(&acceptor) {
std::cout << "Closing acceptor\n";
if (acceptor.is_open()) {
acceptor.close();
std::cout << "Acceptor closed\n";
}
} BOOST_SCOPE_EXIT_END
for (;;) {
boost::system::error_code ec{};
tcp::socket socket(*s);
acceptor.async_accept(socket, yield[ec]);
if (!ec) std::make_shared<Session>(std::move(socket))->start();
}
});
}
private:
unsigned int _port = 0;
boost::asio::io_service * _ioService;
};
TIME_WAIT狀態不是套接字泄漏。 這是RFC 793中指定的TCP連接拆除的正常部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.