tags:

views:

178

answers:

2

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_service);
   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|

Can anyone tell how do i resolve address correctly?

+1  A: 

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);
Jerry Coffin
i had made these changes already and it went very badly. Lots of lots of compile errors about undefined reference
kurrata
@kurrata you probably need to link against -lboost_system, see my answer.
Sam Miller
+1  A: 

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 ~>
Sam Miller
tried compiling with "g++ -lboost_system client.ccp" still some undefined references and now it even started talking about threads.Could it be that i have something set up wrong for boost?
kurrata
found why i never could compile. i needed to add "-pthread" to linker options to.
kurrata