BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #24997同步于 2008/4/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

[请教]bind IPV6 地址失败

flyingmiao
2008/4/20镜像同步3 回复
各位帮忙看看。 getaddrinfo得到IPV6地址后bind 失败 :Invalid argument (AF_INET6 + "0::1") 然而IPV4地址能够成功(AF_INET + “localhost”)。 输入 : ./1test.o 0::1 8888 输出: bind error:Invalid argument failed to to get a address for service 代码如下: int main(int argc, char *argv[]) { if (argc != 3) { cerr << "usage : " << argv[0] << " [host][port]" << endl; exit(1); } struct addrinfo hints, *res, *ressave; bzero(&hints, sizeof(struct addrinfo)); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; int err; if ((err = getaddrinfo(argv[1], argv[2], &hints, &res) ) != 0) { cerr << "getaddrinfo error: " << gai_strerror(err) << endl; exit(1); } ressave = res; int listenSock; if (!res) cerr << "res is NULL" << endl; char buf[32]; const char *addr; struct sockaddr_in *ptrAddrIn; while (res) { listenSock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (listenSock < 0) { cerr << "socket error:" << strerror(errno) << endl; continue; } const int on = 1; setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ); if (bind(listenSock, res->ai_addr, sizeof(*(res->ai_addr) ) ) == 0) break; else cerr << "bind error" << strerror(errno) << endl; close(listenSock); res = res->ai_next; } if (!res) { cerr << "failed to to get a address for service" << endl; exit(1); } if (listen(listenSock, 10) < 0) cerr << "listen error :" << strerror(errno) << endl; int acptSock; if ( (acptSock = accept(listenSock, NULL, NULL) ) < 0) cerr << "listen error :" << strerror(errno) << endl; cout << "accept a link, and we close it immediately" << endl; close(acptSock); cout << "success" << endl; close(listenSock); return 0; }
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
flyingmiao机器人#1 · 2008/4/20
搞定...改成bind(listenSock, res->ai_addr, res->ai_addrlen)就ok了 估计是ai_addr中的长度于sizeof的结果不一致,应该是有部分空间空闲导致
CNLAS机器人#2 · 2008/4/20
sizeof的返回值各种不靠谱。。。能不用就不用。。。 【 在 flyingmiao (amiao) 的大作中提到: 】 : 搞定...改成bind(listenSock, res->ai_addr, res->ai_addrlen)就ok了 : 估计是ai_addr中的长度于sizeof的结果不一致,应该是有部分空间空闲导致
flyingmiao机器人#3 · 2008/4/20
【 在 CNLAS 的大作中提到: 】 : sizeof的返回值各种不靠谱。。。能不用就不用。。。 记下了,卡了我俩个小时=v=