我正在使用 boost::asio 通过 TCP 套接字在端口 443 上向 graph.facebook.com 发送 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 秒的延迟使应用程序无法使用。任何帮助表示赞赏。