int main(int argc, char *argv[])
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int s;
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
//s = getaddrinfo("download101.wanmei.com", NULL, &hints, &result);
s = getaddrinfo(argv[1], NULL, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
const struct sockaddr_in *sin = (const struct sockaddr_in *)rp->ai_addr;
printf("%s\n", inet_ntoa(sin->sin_addr));
}
freeaddrinfo(result); /* No longer needed */
exit(EXIT_SUCCESS);
}
|
这里有个问题不懂,不知道getaddrinfo函数中server参数是干什么用的。man手册说node和server这2个都可以设置,但是不能都为NULL,只能一个为空。没明白这参数具体怎么用。知道的麻烦说一声,谢谢
阅读(965) | 评论(0) | 转发(0) |