⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0512001.htm

📁 VC知识库5_chm_decompile_20040520_210715
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>

<body>

<div align="justify">
  <table border="0" width="100%" class="font" height="57">
    <tr>
      <td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">VC知识库(五)</font>
      </td>
      <td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">www.vckbase.com</font>
      </td>
    </tr>
    <tr>
      <td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
      <hr>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="header" valign="top" align="center" colspan="2">

<small>

如何得到多穴主机的多个IP地址





</small>

      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="info" align="center" colspan="2">
      <small>闻怡洋 vchelp@163.net http://vchelp@163.net





</small>

      </td>  
    </tr>  
    <tr> 
      <td width="100%" height="22" class="font" colspan="2">
        <hr>
      </td>  
    </tr> 
    <tr> 
      <td width="100%" height="5" class="font" colspan="2"> 

<p>

<font size="3">

在网络中的多穴主机可能同时拥有多个IP地址,特别是在使用了动态主机地址分配时也很难知道主机上的IP地址是什么。下面我利用一段C程序来列举出主机上的所有IP地址。下面是具体代码:</font>



<pre><font size="3">

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 &amp;&amp; pHost-&gt;h_addr_list[i]!= NULL; i++ ) 	

    {/*对每一个IP地址进行处理*/

      pszAddr=inet_ntoa (*(struct in_addr *)pHost-&gt;h_addr_list[i]);

      printf(&quot;%s\n&quot;,pszAddr);/*打印*/

    }

  }

}

</font></pre>

      <font size="3">

介绍Socket编程的文章已经很多,所以接下来只对相关内容进行简单的讲解, 
 
函数gethostname将回返回给定主机名所对应的信息,在WinSock中struct hostent的定义如下:</font> 
 
<pre><font size="3">

struct hostent 

{

  char FAR *       h_name;

  char FAR * FAR * h_aliases;

  short            h_addrtype;

  short            h_length;

  char FAR * FAR * h_addr_list;

};

</font></pre>

      <font size="3">

对于结构中的内容和其中对取得多个IP地址我们有用的是最后一个分量h_add_list,它是一个列表,通过它我们可以得到所有的IP地址。 
 
 
 
下面举出一些具体的应用实例,(1)在一个主机同时拥有IP地址,并且同时属于两个网段时,可以利用不同的IP地址产生网络半关联,并向不同的网段产生连接请求。(2)在LAN山的主机利用拨号上网,可以得到由ISP动态分配的IP地址,这一IP地址可以和Internet上的其它主机连接。此外上面的代码可以直接在UNIX下使用而不局限于WinSock。 
 
如果有朋友对次有兴趣可以给我发EMail(vchelp@163.net),或者在我的主页(http://vchelp.163.net)给我留言。</font>  
      </td>     
    </tr>    
    <tr> 
      <td width="100%" height="12" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="6" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="8" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr>    
      <td width="100%" height="17" class="font" colspan="2"></td>     
    </tr>    
  </table>     
</div>     
     
</body>     
     
</html>     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -