gpt4 book ai didi

c++ - 为什么我基于 boost::asio 的应用程序不接受新连接

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:43 29 4
gpt4 key购买 nike

我编写了一个测试应用程序,它创建了 2,000 个到我的 TCP 服务器的连接。终止/启动我的测试应用程序几次后,我的 TCP 服务器不能再接受新连接了吗?我不知道调试这个问题,有什么建议吗?下面列出了一些代码片段:

 Server(io_service& s, tcp::endpoint const& listen_endpoint, size_t thread_number):io_(s),
signals_(s),
acceptor_(io_, listen_endpoint),
thread_pool_size_(thread_number) {
signals_.add(SIGINT);
signals_.add(SIGTERM);
signals_.async_wait(bind(&Server::Stop, this));
shared_ptr<ClientType> c(new ClientType(io_));

acceptor_.async_accept(c->socket, bind(&Server::AfterAccept, this, c, _1));

//launch thread to preapre message
MessageThread d;
MyApp& app = AppHolder::Instance();
d.support_text_message = app.config().support_text_message;
d.support_digit_message = app.config().support_digit_message;
d.interval = app.config().interval;
boost::thread thrd1(d);
}

void AfterAccept(shared_ptr<ClientType>& c, error_code const& ec) {
// Check whether the server was stopped by a signal before this completion
// handler had a chance to run.
if (!acceptor_.is_open()) {
BOOSTER_INFO("Server") << "thread id: " << this_thread::get_id() << " acceptor is closed";
return;
}

if (!ec) {
c->StartJob();
shared_ptr<ClientType> c2(new ClientType(io_));
acceptor_.async_accept(c2->socket, bind(&Server::AfterAccept, this, c2, _1));
}
}

最佳答案

在调用 acceptor_.async_accept 之前尝试添加以下行:

acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));

关于c++ - 为什么我基于 boost::asio 的应用程序不接受新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300926/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com