我正在尝试编写一个允许多个客户端连接的简单服务器。当我运行我的程序时,我得到以下错误作为输出:
[3730] Press [return] to exit.
Listening on: 127.0.0.1:7777
[5d0] Thread Start
Error: system:995 The I/O operation has been aborted because of either a
thread exit or an application request
我认为这是因为我的客户端对象超出了范围,但我不完全确定这是否真的是问题,或者我的代码是否存在其他问题。
ClientNetwork::ClientNetwork(asio::io_service& ios, const asio::ip::tcp::endpoint& edp) : mIOService(ios), mAcceptor(ios, edp)
{
/*mAcceptor.open(edp.protocol());
mAcceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(false));*/
listen();
}
ClientNetwork::~ClientNetwork()
{
boost::system::error_code ec;
mAcceptor.close(ec);
mConnectionThreads.join_all();
mClients.clear();
std::cout << "KILLING NETWORK" << std::endl;
}
void ClientNetwork::listen()
{
mConnectionThreads.create_thread(boost::bind(&ClientNetwork::connectionThread, this));
Client* c = new Client(mIOService);
mAcceptor.listen(boost::asio::socket_base::max_connections);
mAcceptor.async_accept(*c->getSocket(), boost::bind(&ClientNetwork::handleAccept, this, _1, c));
}
void ClientNetwork::handleAccept(const boost::system::error_code& error, Client* client)
{
if (!error)
{
mClients.push_back(client);
mConnectionThreads.create_thread(boost::bind(&ClientNetwork::connectionThread, this));
std::cout << mConnectionThreads.size() << std::endl;
}
else
{
std::cout << "Error: " << error << " " << error.message() << std::endl;
}
}
这是我的主要内容(不确定它是否完全有用,但觉得包含它会很好:
int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::shared_ptr< boost::asio::io_service::strand > strand(
new boost::asio::io_service::strand(*io_service)
);
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Press [return] to exit." << std::endl;
global_stream_lock.unlock();
/*boost::thread_group worker_threads;
for (int x = 0; x < 2; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
}*/
/*boost::shared_ptr< boost::asio::ip::tcp::acceptor > acceptor(
new boost::asio::ip::tcp::acceptor(*io_service)
);
boost::shared_ptr< boost::asio::ip::tcp::socket > sock(
new boost::asio::ip::tcp::socket(*io_service)
);*/
try
{
boost::asio::ip::tcp::resolver resolver(*io_service);
boost::asio::ip::tcp::resolver::query query(
"127.0.0.1",
boost::lexical_cast< std::string >(7777)
);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
ClientNetwork clientNet(*io_service, endpoint);
global_stream_lock.lock();
std::cout << "Listening on: " << endpoint << std::endl;
global_stream_lock.unlock();
}
catch (std::exception & ex)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Exception: " << ex.what() << std::endl;
global_stream_lock.unlock();
}
std::cin.get();
//worker_threads.join_all();
return 0;
}
答案 0 :(得分:1)
创建ClientNetwork
对象后,首先启动异步操作时,应运行io_service::run
方法处理事件循环中的处理程序,然后程序运行,直到至少有一个异步操作要执行。
在try
范围结束时io_service->run()
:
ClientNetwork clientNet(*io_service, endpoint);
global_stream_lock.lock();
std::cout << "Listening on: " << endpoint << std::endl;
global_stream_lock.unlock();
io_service->run();
如果没有这个,ClientNetwork
类的析构函数被调用,mAcceptor
关闭并打印此错误消息
void ClientNetwork::handleAccept (...) {
...
std::cout << "Error: " << error << " " << error.message() << std::endl;
}