http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/example/chat/chat_client.cppブーストクライアントは、私は上記の彼の例に基づいてクライアントアプリケーションに取り組んでいます
を立ち往生。
クライアント接続をseparteスレッドで実行して、UIがスタックしないようにしたかったのですが、ここでUIがスタックしています。
1.これを達成する方法を教えてください。
2.この行の意味は何ですか? boost :: thread t(boost :: bind(& boost :: asio :: io_service :: run、& io_service));
t.join();
この行は接続用の別のスレッドを作成していますか?
client::client(boost::asio::io_service& io_service, tcp::resolver::iterator endpoint_iterator)
: io_service_(io_service),
resolver_(io_service),
socket_(io_service_)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect( endpoint,
boost::bind(&client::handle_connect, this,boost::asio::placeholders::error,
++endpoint_iterator));
}
void client::handle_connect(const boost::system::error_code& error,
tcp::resolver::iterator endpoint_iterator)
{
strcpy(data_,"Hello");
if (!error)
{
/*boost::asio::async_read(socket_,
boost::asio::buffer(data_, MAX_PATH),
boost::bind(&client::handle_read, this,
boost::asio::placeholders::error));*/
boost::asio::async_write(socket_, boost::asio::buffer(data_, MAX_PATH),
boost::bind(&client::handle_read, this,
boost::asio::placeholders::error));
}
else if (endpoint_iterator != tcp::resolver::iterator())
{
socket_.close();
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect( endpoint,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
}
void client::handle_read(const boost::system::error_code& error)
{
if (!error)
{
memset(data_,0,MAX_PATH);
boost::asio::async_read( socket_,
boost::asio::buffer(data_, MAX_PATH),
boost::bind(&client::handle_read, this,
boost::asio::placeholders::error));
if (strcmp(data_,"Hello Response")==0)
{
MessageBox(NULL,_T("Regd Done"),_T("Vue"),1);
// return ;
}
}
}
CConnectionMgr::CConnectionMgr(void)
{
}
void CConnectionMgr::Connect()
{
try
{
char* host = "192.168.4.84";
char* port = "55555";
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(),host , port);
tcp::resolver::iterator iterator = resolver.resolve(query);
c = new client(io_service, iterator);
//boost::thread thrd(boost::bind(&boost::asio::io_service::run, &io_service));
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
t.join();
// MessageBox(NULL,_T("Join"),_T("ff"),1);
}
catch (std::exception& e)
{
CString csMsg(e.what());
MessageBox(NULL,csMsg,_T("ff"),1);
}
}
おかげでしようとしていますと呼ばれる::起こるであろうに述べたように終了します非同期I/O.janmを実行する方法をガイドすることができます。私は基本的なデザインを知り、UIが邪魔にならないようにしたいと思っていました。 –
+1それは魔法では起こらないでしょう。 @Chris基本的なデザインは、UIイベントループスレッドとは別のスレッドでio_serviceを実行します。 janmが暗示しているように、2つのスレッドが通信する方法を設計する必要があります。特定の質問がある場合は、個別に質問してください。 –