BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / www-technology / #5509同步于 2008/12/21
WWWTechnology机器人发帖

[合集] 'bout socket

tntzwz
2008/12/21镜像同步0 回复
☆─────────────────────────────────────☆ wks (cloverprince) 于 (Mon Nov 17 13:46:52 2008) 提到: 按照某“摩登”一些的做法,我这样绑定我的服务器端口: int sockfd; struct addrinfo hints, *servinfo, *p; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP address getaddrinfo(NULL, "3490", &hints, &servinfo) for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("bind"); continue; } break; // if we get here, we must have connected successfully } if (p == NULL) { // looped off the end of the list with no successful bind fprintf(stderr, "failed to bind socket\n"); exit(2); } freeaddrinfo(servinfo); // all done with this structure 这样,用getaddrinfo获取的本地地址,一般来说会绑定那个127.0.0.1的地址。这样,用118.229.131.196这样的地址就无法使用了 记得以前有个什么INADDR_ANY可以绑定任意地址。用getaddrinfo这种方法怎么办呢? ☆─────────────────────────────────────☆ lanphon (灌水协会副主席|灌水帮少帮主 嗯) 于 (Mon Nov 17 13:52:48 2008) 提到: ang? 【 在 wks (cloverprince) 的大作中提到: 】 : 按照某“摩登”一些的做法,我这样绑定我的服务器端口: : int sockfd; : struct addrinfo hints, *servinfo, *p; : ................... ☆─────────────────────────────────────☆ wks (cloverprince) 于 (Mon Nov 17 13:57:37 2008) 提到: 问题解决。只要hint的flags里面有AI_PASSIVE,绑定的地址就是不确定的。也就是什么地址都行。
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。