C++ 在asio客户端应用程序中,当传递变量或放置具有相同内容的ip字符串时,会产生不同的结果

C++ 在asio客户端应用程序中,当传递变量或放置具有相同内容的ip字符串时,会产生不同的结果,c++,client,boost-asio,C++,Client,Boost Asio,有件事我想不通。 下面是boost asio聊天客户端示例的部分代码: 如果ip地址的字符串如下所示,则客户端工作并与服务器对话 auto endpoints = resolver.resolve("13.58.174.105", "1975"); 我还尝试传递一个const变量和一个引用,使variabel std::move,但不知何故,我的努力没有成功 你知道问题出在哪里吗?我该怎么办 TIA, Nicostd::string Tcp::clien

有件事我想不通。
下面是boost asio聊天客户端示例的部分代码:

如果ip地址的字符串如下所示,则客户端工作并与服务器对话

auto endpoints = resolver.resolve("13.58.174.105", "1975");
我还尝试传递一个const变量和一个引用,使variabel std::move,但不知何故,我的努力没有成功

你知道问题出在哪里吗?我该怎么办

TIA,

Nico

std::string 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”);p2p_client c(io_context,endpoints);std::thread t(&io_context{io_context.run()});charline[chat_message::max_body_length+1];while(std::cin.getline(行,chat_message::max_body_length+1)){chat_message msg;msg.body_length(std::strlen(行));std::memcpy(msg.body(),line,msg.body_length();msg.encode_header();c.write(msg);}c.close();t.join()}catch(std::exception&e){std::cerr对等ip变量的值是什么?如果它也是“13.58.174.105”那么这会很奇怪

否则,这意味着主机名无法解析为IP地址

如果已知
peer\u 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 };

发生了什么事?你是想在这里添加文本吗?不清楚这是如何回答问题的,格式也不清楚。peer_ip变量具有与“127.0.0.1”相同的字符串。聊天示例中的endpoints变量是tcp::resolver::results_类型,而不是tcp::endpoint,我也尝试过boost::asio::ip::address_v4::from_string。就像我说的“如果它也是“13.58.174.105”,那么它会很奇怪。”到那时,我只会检查字符串是否正确。例如,只需删除它并再次键入,只需确保它不包含奇怪的UNICODE字符或隐藏的空格或诸如此类的内容。这是罪魁祸首,两个字符串看起来相似,但不知怎的,它们并不相同。一个字符串来自json文件,而这两个字符串都有问题帽子string@NicoVerrijdt呵呵。不是一直都是这样吗。程序员80%的生活都是检查输入和怀疑假设:)很高兴你找到了
auto endpoints = resolver.resolve("13.58.174.105", "1975");
std::string peer_ip = "127.0.0.1";
tcp::endpoint endpoint {
    boost::asio::ip::address_v4::from_string(peer_ip.c_str()), 1975 };