豌笆 发表于 2025-10-14 13:50:00

子网掩码基础知识

<h1 id="子网掩码基础知识">子网掩码基础知识</h1>
<p>子网掩码由连续的1和连续的0组成,1表示网络位,0表示主机位。</p>
<h3 id="1-基础概念">1. <strong>基础概念</strong></h3>
<ul>
<li><strong>IPv4地址</strong>:由32位二进制组成,通常表示为4个十进制数(如<code>192.168.1.0</code>)。</li>
<li><strong>子网掩码</strong>:用于划分网络和主机部分。例如,<code>255.255.255.0</code>(即<code>/24</code>)表示前24位为网络号,后8位为主机号。</li>
</ul>
<h3 id="2-地址数量计算">2. <strong>地址数量计算</strong></h3>
<ul>
<li>
<p><strong>公式</strong>:<br>
可用主机地址数 = (2^{(32 - \text{子网掩码位数})} - 2)<br>
(减2是因为去掉网络地址和广播地址)</p>
</li>
<li>
<p><strong>常见子网示例</strong>:</p>
<ul>
<li><code>/24</code>(如<code>255.255.255.0</code>):<br>
(2^8 - 2 = 254)个可用地址(范围<code>192.168.1.1</code>~<code>192.168.1.254</code>)。</li>
<li><strong>超过256的情况</strong>:<br>
若子网掩码位数更小(如<code>/23</code>),则主机部分有9位,地址数为 (2^9 - 2 = 510)个。</li>
</ul>
</li>
</ul>
<h3 id="3-为什么会有误解">3. <strong>为什么会有误解?</strong></h3>
<ul>
<li><code>/24</code>子网确实有256个地址(含网络和广播地址),但<strong>可用</strong>主机地址是254个。</li>
<li>更大的子网(如<code>/16</code>)可包含(2^{16} - 2 = 65534)个可用地址。</li>
</ul>
<h3 id="4-关键结论">4. <strong>关键结论</strong></h3>
<ul>
<li><strong>子网地址数不限于256</strong>,实际由子网掩码决定。</li>
<li>最小子网是<code>/30</code>(4个地址,2个可用),最大可达<code>/8</code>(约1600万个地址)。</li>
</ul>
<h3 id="5-计算子网的ip数量方法">5. <strong>计算子网的ip数量方法</strong></h3>
<ul>
<li>子网掩码为255.255.240.0,计算为(256-240)*256=4096个</li>
<li>子网掩码为255.255.248.0,计算为(256-248)*256=2048个</li>
</ul>
<h3 id="示例表">示例表</h3>
<table>
<thead>
<tr>
<th>子网掩码</th>
<th>主机位数</th>
<th>总地址数</th>
<th>可用地址数</th>
<th>掩码</th>
</tr>
</thead>
<tbody>
<tr>
<td>/24 (前24位)</td>
<td>8</td>
<td>256</td>
<td>254</td>
<td>255.255.255.0</td>
</tr>
<tr>
<td>/23 (前23位)</td>
<td>9</td>
<td>512</td>
<td>510</td>
<td>前23位是1,后9位是0</td>
</tr>
<tr>
<td>/16 (前16位)</td>
<td>16</td>
<td>65536</td>
<td>65534</td>
<td>255.255.0.0</td>
</tr>
</tbody>
</table>
<h1 id="前23位掩码计算步骤">前23位掩码计算步骤:</h1>
<h4 id="二进制转换">二进制转换</h4>
前23位的二进制表示:
11111111.11111111.11111110.00000000
(前23位全1,第24位为0,剩余全0)
<h4 id="分段转换为十进制">分段转换为十进制</h4>
   第一段(前8位):11111111 → 255
   第二段(9-16位):11111111 → 255
   第三段(17-24位):11111110 → 254(因为11111110 = 128+64+32+16+8+4+2 = 254)
   第四段(剩余位):00000000 → 0
<h4 id="最终结果">最终结果</h4>
前23位的子网掩码为:
255.255.254.0
<h1 id="java中ip地址转数值的方式">java中ip地址转数值的方式</h1>
/**
   * ipV4地址转换成Long类型
   * @param ipAddress
   * @return
   */
    public static long ConvertIpv4ToLong(String ipAddress) {
      String[] ipSegments = ipAddress.split("\\.");
      long result = 0;
      for (int i = 0; i < ipSegments.length; i++) {
            int power = 3 - i;
            int segmentValue = Integer.parseInt(ipSegments);
            result += segmentValue * Math.pow(256, power);
      }
      return result;
    }

    /**
   * ipV6地址转换成Long类型
   * @param ipV6Address
   * @return
   */
    public static long ConvertIpv6ToLong(String ipV6Address) {
      String[] hexSegments = ipV6Address.split(":");
      long result = 0;
      for (int i = 0; i < hexSegments.length; i++) {
            int power = 7 - i;
            int segmentValue = Integer.parseInt(hexSegments, 16);
            result += segmentValue * Math.pow(65536, power);
      }
      return result;
    }<br>来源:程序园用户自行投稿发布,如果侵权,请联系站长删除<br>免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 子网掩码基础知识