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?