基于 boost::asio 客户端/服务器关系,只有当服务器线程处于“等待连接”状态时,我才必须从服务器程序启动客户端程序。
我的问题是如何知道那个状态?
作为示例,使用asio 示例/序列化 链接,并将 server.cpp 的主要功能替换为该代码:
#include <conio.h>
#include <concrt.h> // wait function
#include <future>
#include <thread>
void server_thread( std::promise<bool>& run )
{
boost::asio::io_service io_service;
s11n_example::server server(io_service, 123);
// too early to run.set_value( true );
io_service.run();
// too late to run.set_value( true );
}
int main(int argc, char* argv[])
{
std::promise<bool> run;
std::thread thrd( server_thread, boost::ref( run ) );
thrd.detach();
bool launched = run.get_future().get();
// server is waiting for connection
// launch the client
if( launched )
{
int rc = system( "start client.exe localhost 123" );
if( rc )
std::cerr << "system failed returning " << rc << std::endl ;
}
else
std::cerr << "server_thread failure" << std::endl ;
std::cout << "hit a key to exit" ;
while( !_kbhit() )
Concurrency::wait( 100 );
return 0;
}
谢谢,