Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120971
  • 博文数量: 20
  • 博客积分: 1627
  • 博客等级: 上尉
  • 技术积分: 383
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 16:11
文章分类

全部博文(20)

文章存档

2012年(5)

2011年(2)

2010年(12)

2009年(1)

我的朋友

分类: LINUX

2010-04-02 14:24:50

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,只能一个为空。没明白这参数具体怎么用。知道的麻烦说一声,谢谢
阅读(936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~