0

I am currently trying to get into network programming with boost asio.

I decided to try a simple connection to start with, but I am already having trouble with it. To get started I simply wanted to establish a connection between a server and a client, both running on the same machine.

This is my server code:

try
{
    tcp::endpoint endpoint(tcp::v4(), port);
    tcp::acceptor acceptor(m_io_service, endpoint);

    while (1) {
        tcp::socket socket(m_io_service);

        acceptor.accept(socket);

        std::cout << "Someone connected!";
    }

}
catch (std::exception& e)
{
    std::cerr << e.what() << std::endl;
}

Note that my io_service is a member variable, while port equals 10112.

And to follow it up, here is my client code:

try
{
    tcp::resolver resolver(m_io_service);
    tcp::resolver::query query(tcp::v4(), "10112");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    tcp::socket socket(m_io_service);
    boost::asio::connect(socket, endpoint_iterator);

    std::cout << "Client found server.";
}
catch (std::exception& e)
{
    std::cout << "Exception: " << e.what() << std::endl;
}

This is what I got out of several tutorials so far.

However, I can't manage to get to a connection so far. My guess would be that my query is delivering some wrong values, because I get an iterator of size one in both cases - server being up and down.

That seems like it would just end up with some default value, which doesn't resemble my actual server, even if it's up. This leads to a possible error in the query data I guess.

However, I don't really know how to get on from here.

Do you have any advice for me?

Sossenbinder
  • 4,852
  • 5
  • 35
  • 78

1 Answers1

0

Did you start the io_service object: http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service/run.html

And did you specify an work object to keep it running ?

rr_1993
  • 26
  • 2
  • I just tried to call run(), but it didn't change anything. What do you mean with an object to keep it running? – Sossenbinder Oct 23 '16 at 10:25
  • More information about the work object can be found here : http://stackoverflow.com/a/17157360/6928230 – rr_1993 Oct 23 '16 at 20:01