我无法弄清某些事情。
以下是来自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;
}
当如上所述将peer_ip变量传递到解析器时,我得到:异常:resolve:找不到主机(权威)
auto endpoints = resolver.resolve(peer_ip, "1975");
如果IP地址的字符串如下所示,则客户端可以工作并与服务器对话。
auto endpoints = resolver.resolve("13.58.174.105", "1975");
我还尝试传递一个const变量和一个引用,使variabel std :: move生效,但是以某种方式我的努力没有奏效。
您知道问题出在哪里,我该怎么办?
TIA,
尼科
peer_ip
变量的值是多少?如果这也是“13.58.174.105”那么这将是不可思议。
否则,仅表示主机名无法解析为IP地址。
如果peer_ip
已知总是包含一个IP地址,则无论如何都无需解决任何问题,您可以解析该地址:
std::string peer_ip = "127.0.0.1";
tcp::endpoint endpoint {
boost::asio::ip::address_v4::from_string(peer_ip.c_str()), 1975 };
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句