问题
I want to update the nameserver programmatically.
In this question programmatically-set-dns-nameserver-on-linux was mentioned to manipulate _res.nsaddr_list
-- so I did.
void setNameServer(const std::string& rstr_addr)
{
struct sockaddr_in x_addr;
inet_pton(AF_INET,rstr_addr.c_str(),&x_addr.sin_addr);
res_init();
_res.nsaddr_list[0] = x_addr;
}
This is not working. getaddrinfo
is now returning with error: "Name or service not known"
So there a basically two questions:
What is the correct way to set the nameserver ip programmatically
How to set it permanently in a proper way (ATM I just write it to
/etc/resolv.conf
by myself)
来源:https://stackoverflow.com/questions/55724014/set-dns-nameserver-programatically-and-permanently-on-linux