开发者

Boost Client getting stuck

http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/example/chat/chat_client.cpp

I am working on client application based on he example above.

I wanted to do the client connection in separte thread so that UI doesnot get stuck.Here UI is getiing stuck.

1. Can you tell me how to acheive this?

2. what is the meaning of the this line? boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

t.join();

Is this line create the separate thread for connection?

 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开发者_StackOverflow::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);    
 }
 }


The "t.join()" waits for the thread 't' to exit. Thread 't' is running the run() method on io_service and will exit when there is no remaining I/O to complete.

So, your Connect() method will block until all the I/O is finished, which is clear not what you want. If you are going to do asynchronous I/O so that your client doesn't block, you need to design a way for your I/O context to communicate with our UI context. It won't happen by magic.


  1. Can you tell me how to acheive this?

You could launch the boost thread in your main i.e. somewhere before you enter the UI event loop (the blocking call) but don't do a join.

Have a look at the HTTP server examples: In one of them it's shown how you can start your io_service in the main and stop it via CTRL-c. In your case you would probably do this using a GUI button or event. Once you call the io_service stop method, you can then do the join on the thread.

  1. what is the meaning of the this line? boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
  • Runs the io_service::run in a new thread

t.join();

  • Waits for the thread to finish like janm stated which will happen once the io_service runs out of work or once the io_service::stop method is called
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜