开发者

resolve function doesn't work in boost/asio

I am learning boost/asio and writing example program which was in e-book. of course it didn't work ;)

#include <boost/asio.hpp>
#include <iostream>

int main () {
   boost::asio::io_service io_service;
   boost::asio::ip::tcp::resolver::query query("www.boost.org", "http");
   boost::asio::ip::tcp::resolver::iterator destination = boost::asio::ip::tcp::resolver::resolve(query); // if i have "." before "resolve" as in books example compilers says i need primary-expresion. 
   boost::asio::ip::tcp::endpoint endpoint;

   while ( destination != end ) {
      endpoint = *destination++;
      std::cout<<endpoint<<std::endl;
   }

   boost::asio::ip::tcp::socket socket(io_s开发者_JAVA百科ervice);
   socket.connect(endpoint);
   return 0;
}

compiler output with "::" before "resolve":

/home/martins/C++/boost_asio_client/client.cpp|7|error: cannot call member function 
‘boost::asio::ip::basic_resolver<InternetProtocol, ResolverService>::iterator 
boost::asio::ip::basic_resolver<InternetProtocol, 
ResolverService>::resolve(boost::asio::ip::basic_resolver<InternetProtocol,
 ResolverService>::query&) 
[with InternetProtocol = boost::asio::ip::tcp, ResolverService = 
boost::asio::ip::resolver_service<boost::asio::ip::tcp>, 
boost::asio::ip::basic_resolver<InternetProtocol, ResolverService>::iterator = 
boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>, 
boost::asio::ip::basic_resolver<InternetProtocol, ResolverService>::query = 
boost::asio::ip::basic_resolver_query<boost::asio::ip::tcp>]’ without object|

How do I resolve address correctly?


you need a resolver object. Also your iterator comparison was incorrect, you need to compare against the sentinel value ip::tcp::resolver::iterator().

#include <boost/asio.hpp>
#include <iostream>

int main () {
   boost::asio::io_service io_service;
   boost::asio::ip::tcp::resolver::query query("www.boost.org", "http");
   boost::asio::ip::tcp::resolver resolver( io_service );
   boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
   boost::asio::ip::tcp::endpoint endpoint;

   while ( destination != boost::asio::ip::tcp::resolver::iterator() ) {
     endpoint = *destination++;
     std::cout<<endpoint<<std::endl;
   }

   boost::asio::ip::tcp::socket socket(io_service);
   socket.connect(endpoint);
   return 0;
}

here is a sample compile and run of your code.

samm@macmini ~> g++ -lboost_system resolve.cc
samm@macmini ~> ./a.out 
129.79.245.252:80
samm@macmini ~> echo $?
0
samm@macmini ~>


resolve isn't a static member function, so you need to create a resolver object, and then invoke the member function on that object, something like (going from memory, so don't hold me to this being anywhere close to perfect):

int main () {
   boost::asio::io_service io_service; // existing
   boost::asio::ip::tcp::resolver::query query("www.boost.org", "http"); // existing

   boost::asio::ip::tcp::resolver resolver(io_service);  // added

   // modified to use object defined above:
   boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜