问题是这样,用gethostbyname可以得到主机的信息从而得到主机的地址。那么如果机器上装有多块网卡,有多个地址,我有如何通过主机名得到这些地址?

解决方案 »

  1.   

    用ip help中的函数吧,DWORD GetAdaptersInfo(
      PIP_ADAPTER_INFO pAdapterInfo,
      PULONG pOutBufLen
    );Parameters
    pAdapterInfo 
    [out] Pointer to a buffer that receives a linked list of IP_ADAPTER_INFO structures. 
    pOutBufLen 
    [in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW. 
    Return Values
    If the function succeeds, the return value is ERROR_SUCCESS.If the function fails, the return value is one of the following error codes.Return Code Description ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size. 
    ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter. 
    ERROR_NO_DATA No adapter information exists for the local computer. 
    ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer. 
    Other If the function fails, use FormatMessage to obtain the message string for the returned error. 
    Res
    The order in which adapters appear in the list returned by this function can be controlled from the Network Connections folder: select the Advanced Settings menu item from the Advanced menu. The GetAdaptersInfo and GetInterfaceInfo functions do not return information about the loopback interface.
      

  2.   

    如何得到多穴主机的多个IP地址  
    闻怡洋 [email protected] http://[email protected]  --------------------------------------------------------------------------------
     
    在网络中的多穴主机可能同时拥有多个IP地址,特别是在使用了动态主机地址分配时也很难知道主机上的IP地址是什么。下面我利用一段C程序来列举出主机上的所有IP地址。下面是具体代码: void print_all_ip(void){  char szHostName[128];  const char* pszAddr;  struct hostent * pHost;   int i,j;   if( gethostname(szHostName, 128) == 0 )  {    pHost = gethostbyname(szHostName);     for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )      {/*对每一个IP地址进行处理*/      pszAddr=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);      printf("%s\n",pszAddr);/*打印*/    }  }}
    介绍Socket编程的文章已经很多,所以接下来只对相关内容进行简单的讲解, 函数gethostname将回返回给定主机名所对应的信息,在WinSock中struct hostent的定义如下: 
    struct hostent {  char FAR *       h_name;  char FAR * FAR * h_aliases;  short            h_addrtype;  short            h_length;  char FAR * FAR * h_addr_list;};
    对于结构中的内容和其中对取得多个IP地址我们有用的是最后一个分量h_add_list,它是一个列表,通过它我们可以得到所有的IP地址。 下面举出一些具体的应用实例,(1)在一个主机同时拥有IP地址,并且同时属于两个网段时,可以利用不同的IP地址产生网络半关联,并向不同的网段产生连接请求。(2)在LAN山的主机利用拨号上网,可以得到由ISP动态分配的IP地址,这一IP地址可以和Internet上的其它主机连接。此外上面的代码可以直接在UNIX下使用而不局限于WinSock。 如果有朋友对次有兴趣可以给我发EMail([email protected]),或者在我的主页(http://vchelp.163.net)给我留言。  
     
     
     
     
      

  3.   

    本机可能会有多个IP,下面这段程序可以获得本机IP:struct hostent *h;char host[100];int i;gethostname(host, 100);if ((h = gethostbyname(host)) == NULL) {printf("Error : %s!\n", hstrerror(h_errno));return;};printf("Default IP: %s\n", inet_ntoa (*((struct in_addr *)h->h_addr)));for (i = 0; i< h->h_length / sizeof(int); i++) {printf("IP %d : %s\n", i+1, inet_ntoa (*((struct in_addr *)h->h_addr_list[i])));};