Linux系统调用:getaddrinfo返回-2
我正在使用系统调用 getaddrinfo,它返回 -2。我尝试知道这个错误是什么,并发现这是“名称或服务未知”。 名称 - 这是我的主机名,我确信它是已知的。但服务的编号在每次运行时都会发生变化。我怎么知道我带来了正确的参数?
我的代码:
int GetSockPeerIPs(int sock, AddressList &addresses, int &error,
int family, bool zeroport)
{
struct sockaddr_storage ss;
socklen_t salen = sizeof(ss);
struct sockaddr *sa;
struct addrinfo hints, *paddr, *paddrp;
sa = (struct sockaddr *)&ss;
if (getpeername(sock, sa, &salen) != 0) {
error = errno;
return -1;
}
char hbuf[NI_MAXHOST];
char pbuf[NI_MAXSERV];
if (0 != (error = getnameinfo(sa, salen,
hbuf, sizeof(hbuf),
pbuf, sizeof(pbuf),
0))) {
return -1;
}
memset(&hints, 0, sizeof(hints));
if (ATNetworkTool::AF_XINETX == family) {
hints.ai_family = PF_UNSPEC;
} else {
hints.ai_family = family;
}
hints.ai_socktype = SOCK_STREAM;
if (0 != (error = getaddrinfo(hbuf, pbuf, &hints, &paddrp))) {
return -1;
}
addresses.clear();
for (paddr = paddrp; paddr; paddr = paddr->ai_next) {
if (ATNetworkTool::AF_XINETX == family) {
if (!ATAddress::saIsInet(paddr->ai_addr)) {
continue;
}
}
if (zeroport) {
addresses.insert(ATAddress(paddr->ai_addr, 0));
} else {
addresses.insert(paddr->ai_addr);
}
}
freeaddrinfo(paddrp);
return 0;
}
谢谢! 谷氨酸
I'm using the system call getaddrinfo and it return -2. I try to know what is this error and get that ths is "name or service not known".
the name - it is my host name and I'm sure it is known. but the service is a number that is changed from run to run. how can I know that I am bringing the correct parameter?
my code:
int GetSockPeerIPs(int sock, AddressList &addresses, int &error,
int family, bool zeroport)
{
struct sockaddr_storage ss;
socklen_t salen = sizeof(ss);
struct sockaddr *sa;
struct addrinfo hints, *paddr, *paddrp;
sa = (struct sockaddr *)&ss;
if (getpeername(sock, sa, &salen) != 0) {
error = errno;
return -1;
}
char hbuf[NI_MAXHOST];
char pbuf[NI_MAXSERV];
if (0 != (error = getnameinfo(sa, salen,
hbuf, sizeof(hbuf),
pbuf, sizeof(pbuf),
0))) {
return -1;
}
memset(&hints, 0, sizeof(hints));
if (ATNetworkTool::AF_XINETX == family) {
hints.ai_family = PF_UNSPEC;
} else {
hints.ai_family = family;
}
hints.ai_socktype = SOCK_STREAM;
if (0 != (error = getaddrinfo(hbuf, pbuf, &hints, &paddrp))) {
return -1;
}
addresses.clear();
for (paddr = paddrp; paddr; paddr = paddr->ai_next) {
if (ATNetworkTool::AF_XINETX == family) {
if (!ATAddress::saIsInet(paddr->ai_addr)) {
continue;
}
}
if (zeroport) {
addresses.insert(ATAddress(paddr->ai_addr, 0));
} else {
addresses.insert(paddr->ai_addr);
}
}
freeaddrinfo(paddrp);
return 0;
}
thanks!
gln
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个错误代码。你有没有想过找出它的含义?这次我已经为你做到了。但这是我所做的,以便您下次可以自己查找。
查看 getaddrinfo() 的手册页,我发现它可以返回许多错误代码例如EAI_AGAIN。数值将在头文件中的某个位置定义,所以我这样做了
Thisidentified netdb.h。所以我在 vi 中打开它,它是这样说的:
所以基本上, getaddrinfo 不知道您传入的名称或服务。如果我是你,我会检查前两个参数是否合理。
You have an error code. Have you thought about finding out what it means? On this occasion I have done it for you. But here's what I did so you can look it up for yourself next time.
Looking at the man page for getaddrinfo() I found it can return a number of error codes e.g. EAI_AGAIN. The numeric values will be defined in a header file somewhere, so I did
This identified netdb.h. So I opened that in vi and this is what it said:
So basically, the name or service that you are passing in is unknown to getaddrinfo. I'd check to see whether the first two parameters are reasonable, if I were you.
getaddrinfo
对数据的格式很挑剔,在解析配置文件时,请确保主机名参数中没有多余的空格,通常尾随空格,否则您将收到此错误,在这种情况下,错误是正确的,只是信息不多。getaddrinfo
is picky about the format of the data, in parsing config files make sure you have no extra spaces normally trailing spaces in the host name parameter or you will get this error, in this instance the error is correct, just not very informative.