我有一个小问题,我看不到很多人都使用了提升async_accept。
这就是我处理async_accept以及如何调用它的方式。
void Server::accept()
{
try {
Connection* connection = new Connection(io_service);
connections.push_back(connection);
acceptor->async_accept(connection->getSocket(), std::bind(&Server::onAccept, this, std::placeholders::_1, connection));
}
catch (const boost::system::system_error& e) {
std::cout << "> [Error - Server::start]: " << e.what() << std::endl;
}
}
void Server::onAccept(const boost::system::error_code& e, Connection* connection)
{
if (e) {
std::cout << "> [Error - Server::onAccept]: " << e.message() << std::endl;
connection->disconnect();
return;
}
connection->read();
accept();
}
方法 connection-&gt; getSocket()从连接类返回对 boost :: asio :: ip :: tcp :: socket socket 变量的引用。 接受连接,但是当调用onAccept时,有一个&#34;已经打开&#34;错误,我似乎无法找到解决方案。
答案 0 :(得分:0)
问题不在显示的代码中。
Google shows many people having the same issue,前3个匹配中的一个显示它是由正在使用客户端连接类的事实引起的,而这是一个服务器。
客户端连接启动连接,因此如果您复制/粘贴该代码,您将拥有一个打开的连接,当然“已经打开”。
检查您是否未在Connection
课程中打开套接字。或者,检查您是否在new Connection
(名称和命名空间)中实例化了正确的类型。