2
我使用boost日间例程作为需要机器间双向通信的项目的启动器,现在需要在自己的线程中启动asio io_service,以便可以跨数据传递seperatley。这里是我的代码的基础:http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tutdaytime7/src.html在boost线程中运行boost asio io_service
如果我调用该服务的主要有
io_service.run()
但是,如果我尝试创建一个线程组,并推出有像这样这一切工作正常:
int main()
{
boost::thread_group tgroup;
try
{
boost::asio::io_service io_service;
tcp_server server1(io_service);
udp_server server2(io_service);
tgroup.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
std::cout << "Server running on TCP port " << tcpport << std::endl << "Server running on UDP port " << udpport << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
tgroup.join_all();
return 0;
}
代码编译并运行时,端口号被cout正确引用,但似乎并未打开侦听端口,因为客户端获取连接时被拒绝,尽管服务器程序似乎在等待连接。
这里发生了什么事?