async_connect挂起 - Ubuntu

时间:2013-02-16 01:42:37

标签: c++ boost boost-asio

我使用boost :: asio to graph.facebook.com通过TCP套接字在端口443上发送HTTPS GET消息。当我在我的家庭Linux机器上执行此操作时,async_connect立即通过,一切都很好。但是当我在我的rackspace linux服务器(也是Ubuntu)上运行相同的代码时,async_connect每次挂起大约20-30秒,然后才能连接。这是我的代码

boost::asio::io_service Server::m_ioService;
boost::asio::ssl::context Server::m_ctx(boost::asio::ssl::context::sslv23);

boost::shared_ptr<boost::asio::io_service::work> m_work(new boost::asio::io_service::work(m_ioService);
boost::thread_group m_threads;
m_ctx.set_default_verify_paths();

for(int z=0; z < 4; ++z)
{
   m_threads.create_thread(boost::bind(&boost::asio::io_service::run, &m_ioService));
}

boost::asio::ip::tcp::resolver m_resolver;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> m_sslSocket;
boost::asio::streambuf m_request;

m_sslSocket.set_verify_mode(boost::asio::ssl::verify_peer);
m_sslSocket.set_verify_callback(boost::asio::ssl::rfc2818_verification("graph.facebook.com"));

std::ostream request_stream(&m_request);
request_stream << "GET " << a_path << " HTTP/1.0\r\n";
request_stream << "Host: " << "graph.facebook.com" << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

boost::asio::ip::tcp::resolver::query query("graph.facebook.com", "https");
m_resolver.async_resolve(query, boost::bind(&HandleResolve, this,  boost::asio::placeholders::error, boost::asio::placeholders::iterator));

void HandleResolve(const boost::system::error_code& a_err, boost::asio::ip::tcp::resolver::iterator a_endpointIterator)
{
   if(!a_err)
   {
       // Attempt a connection to each endpoint in the list until we
       // successfully establish a connection.
       std::cout << "Before connect" << std::endl;
       boost::asio::async_connect(m_sslSocket.lowest_layer(), a_endpointIterator, boost::bind(&HandleConnect, this, boost::asio::placeholders::error));
   }
   else
   {
         std::cout << "Error: " << a_err.message() << "\n";
   }
}

我没有收到错误并且连接最终有效,但是20-30秒的延迟使应用程序无法使用。任何帮助表示赞赏。

0 个答案:

没有答案