6

I am making a program for school in which two programs communicate with each other. So far I have not been able to connect the two programs. Whenever I try to connect to localhost:8888 or 127.0.0.1:8888, the error "Host not found (authoritative)" occurs.

So far my code is this:

Connection.cpp

Connection::Connection(std::string Arg) {
    try
    {
        tcp::resolver resolver(io_service);
        cout<<Arg<<endl;
        tcp::resolver::query query(Arg, "daytime");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        tcp::socket socket(io_service);
        socket_p = &socket;
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
        {
           socket.close();
           socket.connect(*endpoint_iterator++, error);
        }
        if (error)
           throw boost::system::system_error(error);
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}
void Connection::Receiver() {
    try{
        for (;;)
        {
            boost::array<char, 128> buf;
            boost::system::error_code error;

            size_t len = socket_p->read_some(boost::asio::buffer(buf), error);

            if (error == boost::asio::error::eof)
                break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.

            std::cout.write(buf.data(), len);
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
} 

In case this helps, I use fedora.

EDIT: the full code can be found here: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tutdaytime1.html i tried to make it OOP (not sure if i did a good job)

Sam Miller
  • 23,808
  • 4
  • 67
  • 87

1 Answers1

6

You should use:

tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::numeric_service);

The problem was that the constructor for query has the address_configured flag set by default which won't return an address if the loopback device is the only device with an address. By just settings flags to 0 or anything other than address_configured the problem is fixed.

How does Boost Asio's hostname resolution work on Linux? Is it possible to use NSS?

I will more help you if you paste the whole code. For the very beginning there is really useful piece of code here:

http://boost.2283326.n4.nabble.com/Simple-telnet-client-demonstration-with-boost-asio-asynchronous-I-O-td2583017.html

it works and you can test it with your local telnet.

Community
  • 1
  • 1
CyberGuy
  • 2,783
  • 1
  • 21
  • 31