我该如何适应这个 C++测试用例以便它继续与新的网络设备一起工作?
如果您制作了网络状态指示器,这就是您可能想要做的事情。循环解析DNS并返回ping结果。
那么,当网络设备发生变化时,我们如何确保DNS解析继续工作呢?在这种情况下,请断开内置网络,然后连接 GPRS 调制解调器。
我使用的是 Fedora 13,但我猜测在许多其他基于 nix 的系统上也有相同的行为。
从下面的日志中可以看到,当切换时,它从“非权威”变为“权威”,但从未找到主机。
(请告诉我如何在预代码块内进行 markdown 转义 << 还是我必须使用 HTML 代码?)
#include <iostream>
#include <boost/asio.hpp>
int main(int argc, char *argv[]) {
std::string DNS = "www.google.com";
while (1) {
try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(DNS.c_str(), "");
boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
boost::asio::ip::tcp::resolver::iterator end;
if (iter != end) {
boost::asio::ip::address addr = (iter++)->endpoint().address();
std::cout << addr.to_string() << std::endl;
}
}
catch (std::exception &e) {
std::cerr << "Error: GetIP():" << e.what() << std::endl;
}
usleep(1000000);
}
}
[test@Test-Live-1010001 Downloads]$ g++ -o test -lboost_system -lpthread testcase_hostname_not_found.cpp
[test@Test-Live-1010001 Downloads]$ ./test
209.85.149.106
209.85.149.99
209.85.149.104
209.85.149.147
209.85.149.106
209.85.149.103
209.85.149.105
209.85.149.99
209.85.149.103
209.85.149.103
209.85.149.106
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
This is the sort of thing you might want to do if you made a network status indicator. In a loop, resolve the DNS and return the result of ping.
So, how can we make sure DNS resolve continues to work when the network device is changed? In this case by disconnecting the builtin network followed by connecting a GPRS modem.
I'm using Fedora 13 but I'm guessing it's the same behavior on many other nix-based systems.
As you can see from the log below, when switching it goes from "non-authoritative" to "authoritative" but the host is never found.
(Please tell me how to markdown escape << inside a pre code block or do i have to use HTML code?)
#include <iostream>
#include <boost/asio.hpp>
int main(int argc, char *argv[]) {
std::string DNS = "www.google.com";
while (1) {
try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(DNS.c_str(), "");
boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
boost::asio::ip::tcp::resolver::iterator end;
if (iter != end) {
boost::asio::ip::address addr = (iter++)->endpoint().address();
std::cout << addr.to_string() << std::endl;
}
}
catch (std::exception &e) {
std::cerr << "Error: GetIP():" << e.what() << std::endl;
}
usleep(1000000);
}
}
[test@Test-Live-1010001 Downloads]$ g++ -o test -lboost_system -lpthread testcase_hostname_not_found.cpp
[test@Test-Live-1010001 Downloads]$ ./test
209.85.149.106
209.85.149.99
209.85.149.104
209.85.149.147
209.85.149.106
209.85.149.103
209.85.149.105
209.85.149.99
209.85.149.103
209.85.149.103
209.85.149.106
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您运行的是 Fedora,当以太网出现故障时,NetworkManager 可能正在运行并自动从
/etc/resolv.conf
中删除 DHCP 学习的名称服务器。 (或者以其他方式修改resolv.conf
以响应接口更改)Since you're running Fedora, NetworkManager is probably running and automatically removing the DHCP-learned nameservers from
/etc/resolv.conf
when the Ethernet goes down. (Or otherwise modifyingresolv.conf
in response to the interface changes)