发新话题
打印

请教高手:gethostbyname 得不到实际的IP地址?

请教高手:gethostbyname 得不到实际的IP地址?

我的机子装有Redhat 9.2,机子上网和工作一切正常,但我编写如下程序段,只显示输出了一个回送地址“127.0.0.1”,却没有显示实际的IP地址。请问程序哪有问题?
   
   char name[255];
   if( (gethostname(name, sizeof(name) ) ) < 0 )
    {
        fprintf(stderr,"%s: gethostname\n",strerror(errno));
        exit(1);
    }
    struct hostent *hp;
    hp = gethostbyname(name);
    if(!hp)
    {
        fprintf(stderr,"%s: gethostbyname %s\n",hstrerror
                (errno),name);
        exit(1);
    }
    char *ip;
    int i;
    ip = inet_ntoa( *(struct in_addr * ) *hp->h_addr_list );
    for( i=0; hp->h_addr_list; i++)
    {
        printf("IP[%d]=%s",i,hp->h_addr_list);
    }

TOP

发新话题