There's something I'm unable to figure out.
Below is a part of the code from the chat client example from boost asio: https://www.boost.org/doc/libs/1_70_0/doc/html/boost_asio/example/cpp11/chat/chat_client.cpp
int Tcp::client(std::string peer_ip)
{
try
{
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve(peer_ip, "1975");
chat_client c(io_context, endpoints);
std::thread t([&io_context]() { io_context.run(); });
char line[chat_message::max_body_length + 1];
while (std::cin.getline(line, chat_message::max_body_length + 1))
{
chat_message msg;
msg.body_length(std::strlen(line));
std::memcpy(msg.body(), line, msg.body_length());
msg.encode_header();
c.write(msg);
}
c.close();
t.join();
}
catch (std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
When the peer_ip variable is passed to the resolver as above, then I get: Exception: resolve: Host not found (authoritative)
auto endpoints = resolver.resolve(peer_ip, "1975");
And if the string of the ip address is given as below, then the client works and talks to the server.
auto endpoints = resolver.resolve("13.58.174.105", "1975");
I also tried passing a const variable and a reference, make the variabel std::move, but somehow my efforts didn't work out.
Do you know where the problem lies and what I should do?
TIA,
Nico
What is the value of the peer_ip
variable? If it's also "13.58.174.105" then it would be weird.
Otherwise it simply means that the host name cannot be resolved to the IP address.
If peer_ip
is known to always contain an IP address, there is no need to resolve anything anyways and you can just parse the address instead:
std::string peer_ip = "127.0.0.1";
tcp::endpoint endpoint {
boost::asio::ip::address_v4::from_string(peer_ip.c_str()), 1975 };