我正在使用boost::asio::ip::tcp::iostream
和序列化通过tcp发送自定义结构。
我的代码是:
//服务器
while (!_exit)
{
boost::asio::io_service ios;
boost::asio::ip::tcp::endpoint endpoint
= boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 4444);
boost::asio::ip::tcp::acceptor acceptor(ios, endpoint);
boost::asio::ip::tcp::iostream stream;
// program stops here until client connects.
acceptor.accept(*stream.rdbuf());
connected = true;
while (connected == true)
{
try
{
boost::archive::binary_oarchive archive(stream);
archive << data_out;
}
catch (...) {
connected = false;
}
}
}
//客户端
while (ShutdownRequested == false)
{
do {
std::cout << "looking for connection..." << std::endl;
stream.clear();
stream.connect(ipline, portNum);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
} while (!stream && ShutdownRequested == false);
bConnected = true;
while (bConnected == true && ShutdownRequested == false)
{
try
{
boost::archive::binary_iarchive archive(stream);
archive >> data_in;
catch (...) {
bConnected = false;
}
这很好用,但是我想扩展它,以便多个客户端可以同时连接。现在,第二个尝试访问的客户端被卡在"looking for connection..."
上。
如何通过多个连接使它工作?