GetAddressByName()
gethostbyname()
都能返回多个IP地址的。

解决方案 »

  1.   

    先使用gethostbyname,然后使用gethostbyname,该函数返回一个hostent类型的结构,你可以通过里面的内容得到所有的ip.只要查看一下h_addr_list是否是null就可以知道是不是到最后了。结构的说明:
    hostent
    The Windows Sockets hostent structure is used by functions to store information about a given host, such as host name, IP address, and so forth. An application should never attempt to modify this structure or to free any of its components. Furthermore, only one copy of the hostent structure is allocated per thread, and an application should therefore copy any information that it needs before issuing any other Windows Sockets API calls.struct hostent {
      char FAR *       h_name;
      char FAR * FAR * h_aliases;
      short            h_addrtype;
      short            h_length;
      char FAR * FAR * h_addr_list;
    };
    Members
    h_name 
    Official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IP address. 
    h_aliases 
    Null-terminated array of alternate names. 
    h_addrtype 
    Type of address being returned. 
    h_length 
    Length of each address, in bytes. 
    h_addr_list 
    Null-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software. 
    另外,现在新版本的msdn中有好多这样的函数的。如GetAddressByName什么地,你可以看一下《windows网络编程》上面又很详细的程序的。
      

  2.   

    用gethostbyname或者gethostbyaddr返回LPHOSTENT是结构hostent的指针hostent.h_addr_list时保存地址的char FAR* FAR* 的NULL terminate字符数组可以有多个地址具体如下:
    int iNdx;
    LPHOSTENT lpHostEntry
    in_addr *pinAddr;
    ..
    ...lpHostEntry=gethostbyname(..
    ..
    for(iNdx=0;;iNdx++)
    {
      pinAddr=((LPIN_ADDR)lpHostEntry->h_addr_list[iNdx]);
      if(pinAddr==NULL) break;
      printf("Addr %d:%s\n",iNdx,inet_ntoa(*pinAddr));
    }
      

  3.   

    获得本机所有IP地址:char szHostName[128];if( gethostname(szHostName, 128) == 0 )
    {
    // Get host adresses
    struct hostent * pHost;
    int i;
     
    pHost = gethostbyname(szHostName);
     
    for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
      {
     CString str;
      int j;
     
      for( j = 0; j < pHost->h_length; j++ )
      {
     CString addr;
     
      if( j > 0 )
      str += ".";
     
      addr.Format("%u", (unsigned int)((unsigned
      char*)pHost->h_addr_list[i])[j]);
    str += addr;
      }
       // str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
      }
    }
      

  4.   

    获得本机所有IP地址:char szHostName[128];if( gethostname(szHostName, 128) == 0 )
    {
    // Get host adresses
    struct hostent * pHost;
    int i;
     
    pHost = gethostbyname(szHostName);
     
    for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
      {
     CString str;
      int j;
     
      for( j = 0; j < pHost->h_length; j++ )
      {
     CString addr;
     
      if( j > 0 )
      str += ".";
     
      addr.Format("%u", (unsigned int)((unsigned
      char*)pHost->h_addr_list[i])[j]);
    str += addr;
      }
       // str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
      }
    }
      

  5.   

    获得本机所有IP地址:char szHostName[128];if( gethostname(szHostName, 128) == 0 )
    {
    // Get host adresses
    struct hostent * pHost;
    int i;
     
    pHost = gethostbyname(szHostName);
     
    for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
      {
     CString str;
      int j;
     
      for( j = 0; j < pHost->h_length; j++ )
      {
     CString addr;
     
      if( j > 0 )
      str += ".";
     
      addr.Format("%u", (unsigned int)((unsigned
      char*)pHost->h_addr_list[i])[j]);
    str += addr;
      }
       // str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
      }
    }
      

  6.   

    char szHostName[128];if( gethostname(szHostName, 128) == 0 )
    {
    // Get host adresses
    struct hostent * pHost;
    int i;
     
    pHost = gethostbyname(szHostName);
     
    for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
      {
     CString str;
      int j;
     
      for( j = 0; j < pHost->h_length; j++ )
      {
     CString addr;
     
      if( j > 0 )
      str += ".";
     
      addr.Format("%u", (unsigned int)((unsigned
      char*)pHost->h_addr_list[i])[j]);
    str += addr;
      }
       // str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
      }
    }