我在Windows上使用Boost :: ASIO版本1.52.0,在運行Win 8並使用VC++中的Visual Studio 2008代碼的測試計算機上使用SSL版本C.在asio中使用ip地址而不是url
我有一個成功連接到TCP服務器與給定的URL一些代碼:
// Create the resolver and query objects to resolve the host name in serverPath to an ip address.
boost::asio::ip::tcp::resolver resolver(*IOService);
boost::asio::ip::tcp::resolver::query query(serverPath, port);
boost::asio::ip::tcp::resolver::iterator EndpointIterator = resolver.resolve(query);
// Set up an SSL context.
boost::asio::ssl::context ctx(*IOService, boost::asio::ssl::context::tlsv1_client);
// Specify to not verify the server certificiate right now.
ctx.set_verify_mode(boost::asio::ssl::context::verify_none);
// Init the socket object used to initially communicate with the server.
pSocket = new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(*IOService, ctx);
//
// The thread we are on now, is most likely the user interface thread. Create a thread to handle all incoming socket work messages.
// This thread is created for each connection to a server.
if (!RcvThreadCreated)
{
WorkerThreads.create_thread(boost::bind(&SSLSocket::RcvWorkerThread, this));
RcvThreadCreated = true;
WorkerThreads.create_thread(boost::bind(&SSLSocket::SendWorkerThread, this));
}
// Try to connect to the server. Note - add timeout logic at some point.
boost::asio::async_connect(pSocket->lowest_layer(), EndpointIterator,
boost::bind(&SSLSocket::HandleConnect, this, boost::asio::placeholders::error));
基於一些研究,如果一個IP地址被替換不能使用能解決IP地址上面的代碼在serverPath中的URL。它看起來像正確的做法是創建一個這樣的端點:
const boost::asio::ip::address IP(boost::asio::ip::address::from_string(serverPath));
int iport = atoi(port.c_str());
boost::asio::ip::tcp::endpoint EP(IP, iport);
這部分似乎編譯好。但是,稍後在async_connect方法中:
// Try to connect to the server. Note - add timeout logic at some point.
boost::asio::async_connect(pSocket->lowest_layer(), EP,
boost::bind(&SSLSocket::HandleConnect, this, boost::asio::placeholders::error));
它會生成一個錯誤。我猜測,async_connect方法想要查看第二個參數的迭代器。但是,我不確定如何創建一個。
根據以下link,答案表明可以使用套接字對象上的connect方法。但是,當我嘗試這樣做:
pSocket->connect(EP);
編譯器提供了一個錯誤 - 錯誤C2039:
'connect' : is not a member of 'boost::asio::ssl::stream<Stream>'
因此,有人可以顯示一些簡單的代碼,它提供了一種與連接到服務器一個IP地址,而不是一個URL?
如果這是不可能的,那麼有沒有辦法從IP地址進行反向查找來獲取URL?
這不是很清楚,我在您的編譯失敗,你可以合併你的片段到一個單一的代碼塊的例子嗎? – 2013-02-22 16:55:17