gpt4 book ai didi

c++ - boost asio : "host not found (authorative)"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:30:43 25 4
gpt4 key购买 nike

我正在为学校制作一个程序,其中两个程序相互通信。到目前为止,我还无法连接这两个程序。每当我尝试连接到 localhost:8888 或 127.0.0.1:8888 时,都会出现错误“找不到主机(权威)”。

到目前为止我的代码是这样的:

连接.cpp

Connection::Connection(std::string Arg) {
try
{
tcp::resolver resolver(io_service);
cout<<Arg<<endl;
tcp::resolver::query query(Arg, "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

tcp::socket socket(io_service);
socket_p = &socket;
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void Connection::Receiver() {
try{
for (;;)
{
boost::array<char, 128> buf;
boost::system::error_code error;

size_t len = socket_p->read_some(boost::asio::buffer(buf), error);

if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.

std::cout.write(buf.data(), len);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

如果这有帮助,我会使用 fedora。

编辑:完整的代码可以在这里找到: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tutdaytime1.html我试着让它 OOP(不确定我是否做得很好)

最佳答案

你应该使用:

tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::numeric_service);

The problem was that the constructor for query has the address_configured flag set by default which won't return an address if the loopback device is the only device with an address. By just settings flags to 0 or anything other than address_configured the problem is fixed.

How does Boost Asio's hostname resolution work on Linux? Is it possible to use NSS?

如果您粘贴整个代码,我会为您提供更多帮助。对于一开始,这里有一段非常有用的代码:

http://boost.2283326.n4.nabble.com/Simple-telnet-client-demonstration-with-boost-asio-asynchronous-I-O-td2583017.html

它可以工作,您可以使用本地 telnet 对其进行测试。

关于c++ - boost asio : "host not found (authorative)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com