---
title: 湖科大教书匠计算机网络微课堂
author: 萑澈
pubDatetime: 2026-11-09T08:29:46Z
featured: false
draft: false
printable: true
copyable: true
tags:
  - 计算机网络
  - cs408
  - 考研
  - 湖科大教书匠
description: 湖科大教书匠《计算机网络微课堂》全课程总结。视频：https://www.bilibili.com/video/BV1c4411d7jb/。覆盖概述、物理层、数据链路层、网络层、运输层、应用层，含 86 张结构/时序 SVG
---



## 第1章 概述

### 1.1 计算机网络在信息时代的作用

计算机网络已由过去的一种<strong>通信基础设施</strong>，发展为重要的<strong>信息服务基础设施</strong>，像水、电、煤气一样成为生活中不可或缺的一部分。CNNIC（中国互联网络信息中心）定期发布《中国互联网络发展状况统计报告》，是获取我国互联网发展数据的一手来源。

| 指标（2018 年） | 数值 |
|---|---|
| 网民规模 | 8.29 亿（2008 年为 2.98 亿） |
| 互联网普及率 | 59.6%（2008 年为 22.6%） |
| 手机网民规模 / 占网民比例 | 8.17 亿 / 98.6% |
| 网民人均每周上网时长 | 27.6 小时（日均约 4 小时） |

互联网应用使用率靠前的类别包括即时通信、搜索引擎、网络新闻、网络视频、网络购物、网上支付、网络音乐、网络游戏、网络文学、网上银行、旅行预订、外卖、直播、微博、网约车、在线教育、互联网理财、短视频等。

> 互联网之父温顿·瑟夫：“You can't escape from contact with the Internet, so why not get to know it and use it.”

### 1.2 网络、互连网与因特网

<strong>网络</strong>由若干<strong>节点</strong>和连接这些节点的<strong>链路</strong>组成。节点可以是笔记本电脑、台式机、网络打印机，把节点互联起来的交换机本身也是一个节点；链路可以是有线链路，也可以是无线链路。

多个网络再通过<strong>路由器</strong>互联，就构成范围更大的<strong>互连网</strong>，因此互连网也称为<strong>网络的网络</strong>。

<figure class="cpd-figure not-content" id="cn-w1-fig-1">
  <svg viewBox="0 0 700 286" role="img" aria-label="网络、互连网与因特网的拓扑关系示意图">
    <defs>
      <marker id="cn-w1-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="20" width="206" height="174" rx="8"/>
    <rect class="fig-box-subtle" x="304" y="20" width="148" height="174" rx="8"/>
    <rect class="fig-box-accent" x="530" y="34" width="152" height="146" rx="22"/>
    <line class="fig-line" x1="100" y1="67" x2="140" y2="98"/>
    <line class="fig-line" x1="100" y1="113" x2="140" y2="113"/>
    <line class="fig-line" x1="100" y1="159" x2="140" y2="128"/>
    <line class="fig-line-accent" x1="210" y1="113" x2="240" y2="113"/>
    <line class="fig-line-accent" x1="288" y1="113" x2="318" y2="113"/>
    <line class="fig-line" x1="378" y1="92" x2="378" y2="126"/>
    <line class="fig-line-accent" x1="438" y1="74" x2="468" y2="105"/>
    <line class="fig-line-accent" x1="516" y1="113" x2="530" y2="113"/>
    <rect class="fig-box" x="30" y="52" width="70" height="30" rx="4"/>
    <rect class="fig-box" x="30" y="98" width="70" height="30" rx="4"/>
    <rect class="fig-box" x="30" y="144" width="70" height="30" rx="4"/>
    <rect class="fig-box-blue" x="140" y="91" width="70" height="44" rx="5"/>
    <circle class="fig-box-orange" cx="264" cy="113" r="24"/>
    <rect class="fig-box-blue" x="318" y="56" width="120" height="36" rx="5"/>
    <rect class="fig-box" x="328" y="126" width="100" height="32" rx="4"/>
    <circle class="fig-box-orange" cx="492" cy="113" r="24"/>
    <rect class="fig-box" x="18" y="210" width="206" height="56" rx="6"/>
    <rect class="fig-box-orange" x="236" y="210" width="216" height="56" rx="6"/>
    <rect class="fig-box-accent" x="464" y="210" width="218" height="56" rx="6"/>
    <text class="fig-title" x="121" y="40" font-size="12" text-anchor="middle">网络 N1（局域网）</text>
    <text class="fig-label" x="65" y="71" font-size="12" text-anchor="middle">笔记本</text>
    <text class="fig-label" x="65" y="117" font-size="12" text-anchor="middle">台式机</text>
    <text class="fig-label" x="65" y="163" font-size="12" text-anchor="middle">打印机</text>
    <text class="fig-blue" x="175" y="111" font-size="12" text-anchor="middle">交换机</text>
    <text class="fig-muted" x="175" y="126" font-size="10" text-anchor="middle">网内节点</text>
    <text class="fig-orange" x="264" y="117" font-size="12" text-anchor="middle">R1</text>
    <text class="fig-muted" x="264" y="153" font-size="11" text-anchor="middle">路由器</text>
    <text class="fig-title" x="378" y="40" font-size="12" text-anchor="middle">网络 N2</text>
    <text class="fig-blue" x="378" y="78" font-size="12" text-anchor="middle">交换机 S2</text>
    <text class="fig-label" x="378" y="146" font-size="12" text-anchor="middle">主机 / 服务器</text>
    <text class="fig-orange" x="492" y="117" font-size="12" text-anchor="middle">R2</text>
    <text class="fig-muted" x="492" y="153" font-size="11" text-anchor="middle">路由器</text>
    <text class="fig-accent" x="606" y="88" font-size="14" text-anchor="middle">因特网 Internet</text>
    <text class="fig-label" x="606" y="112" font-size="11" text-anchor="middle">全球最大“网络的网络”</text>
    <text class="fig-muted" x="606" y="132" font-size="11" text-anchor="middle">统一采用 TCP/IP 协议族</text>
    <text class="fig-muted" x="606" y="150" font-size="10" text-anchor="middle">（云内省略海量路由器细节）</text>
    <text class="fig-label" x="121" y="232" font-size="12" text-anchor="middle">① 网络 (Network)</text>
    <text class="fig-muted" x="121" y="252" font-size="11" text-anchor="middle">若干节点 + 连接链路</text>
    <text class="fig-orange" x="344" y="232" font-size="12" text-anchor="middle">② 互连网 (internet，通用)</text>
    <text class="fig-muted" x="344" y="252" font-size="11" text-anchor="middle">多网络经路由器互连，协议任意</text>
    <text class="fig-accent" x="573" y="232" font-size="12" text-anchor="middle">③ 因特网 (Internet，专指)</text>
    <text class="fig-muted" x="573" y="252" font-size="11" text-anchor="middle">前身 ARPANET，强制 TCP/IP</text>
  </svg>
  <figcaption>图 1-1　网络、互连网（internet）与因特网（Internet）的层级演进：交换机互连节点构成网络，路由器互连网络构成互连网</figcaption>
</figure>

<strong>因特网</strong>是世界上最大的互连网：用户数以亿计，互连的网络数以百万计。课程中常用<strong>一朵云</strong>表示因特网，连接在云上的计算机称为<strong>主机</strong>，云内部路由器如何互联的细节通常省略。

> <strong>注意</strong>：小写 `internet` 是通用名词“互连网”，泛指多个计算机网络互联而成、网间协议可任意的网络；大写 `Internet` 是专用名词“因特网”，指当前全球最大的、开放的、由众多网络互联而成的特定网络，统一采用 <strong>TCP/IP 协议族</strong>，前身是美国的 ARPANET。

### 1.3 因特网的发展阶段与标准化

因特网基础结构大体经历三个阶段：

| 阶段 | 时间 | 关键事件 |
|---|---|---|
| 一：从 ARPANET 向互连网发展 | 1969 起 | 20 世纪 70 年代中期认识到单个网络无法满足所有通信需求，出现互连网；<strong>1983 年 TCP/IP 成为 ARPANET 标准协议</strong>，所有使用 TCP/IP 的计算机可互相通信，故 1983 年被视为因特网诞生时间 |
| 二：三级结构 | 1985 起 | NSF 围绕 6 个大型计算机中心建成 <strong>NSFNET</strong>，分主干网、地区网、校园网（企业网）三级；1990 年 ARPANET 正式关闭；1991 年 NSF 决定将主干网交私人公司运营并开始收费 |
| 三：多层次 ISP 结构 | 1993 起 | 政府资助的 NSFNET 逐渐被商用主干网取代，改由 <strong>ISP</strong> 运营；1994 年 WWW 技术被广泛使用；1995 年 NSFNET 停止运作，因特网彻底商业化 |

<figure class="cpd-figure not-content" id="cn-w1-fig-2">
  <svg viewBox="0 0 700 284" role="img" aria-label="因特网发展三阶段时间轴与RFC正式标准四阶段演进">
    <defs>
      <marker id="cn-w1-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="148" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="176" width="664" height="92" rx="8"/>
    <line class="fig-line" x1="36" y1="114" x2="662" y2="114" marker-end="url(#cn-w1-arr-2)"/>
    <line class="fig-line" x1="72" y1="106" x2="72" y2="122"/>
    <line class="fig-line-accent" x1="172" y1="104" x2="172" y2="124"/>
    <line class="fig-line" x1="264" y1="106" x2="264" y2="122"/>
    <line class="fig-line" x1="362" y1="106" x2="362" y2="122"/>
    <line class="fig-line" x1="462" y1="106" x2="462" y2="122"/>
    <line class="fig-line" x1="576" y1="106" x2="576" y2="122"/>
    <line class="fig-line-accent" x1="176" y1="226" x2="210" y2="226" marker-end="url(#cn-w1-arr-2)"/>
    <line class="fig-line-accent" x1="336" y1="226" x2="370" y2="226" marker-end="url(#cn-w1-arr-2)"/>
    <line class="fig-line-accent" x1="496" y1="226" x2="530" y2="226" marker-end="url(#cn-w1-arr-2)"/>
    <rect class="fig-box-blue" x="42" y="42" width="166" height="46" rx="6"/>
    <rect class="fig-box-purple" x="234" y="42" width="186" height="46" rx="6"/>
    <rect class="fig-box-green" x="446" y="42" width="206" height="46" rx="6"/>
    <circle class="fig-dot" cx="172" cy="114" r="4"/>
    <rect class="fig-box" x="36" y="204" width="140" height="44" rx="6"/>
    <rect class="fig-box-blue" x="212" y="204" width="124" height="44" rx="6"/>
    <rect class="fig-box-orange" x="372" y="204" width="124" height="44" rx="6"/>
    <rect class="fig-box-green" x="532" y="204" width="132" height="44" rx="6"/>
    <text class="fig-title" x="36" y="34" font-size="12">因特网基础结构演进三阶段</text>
    <text class="fig-blue" x="125" y="61" font-size="12" text-anchor="middle">① 阶段一（1969 起）</text>
    <text class="fig-muted" x="125" y="79" font-size="11" text-anchor="middle">ARPANET → 互连网探索</text>
    <text class="fig-purple" x="327" y="61" font-size="12" text-anchor="middle">② 阶段二（1985 起）</text>
    <text class="fig-muted" x="327" y="79" font-size="11" text-anchor="middle">NSFNET 主干/地区/校园三级网</text>
    <text class="fig-green" x="549" y="61" font-size="12" text-anchor="middle">③ 阶段三（1993 起）</text>
    <text class="fig-muted" x="549" y="79" font-size="11" text-anchor="middle">多层次商用 ISP + WWW 普及</text>
    <text class="fig-label" x="72" y="138" font-size="11" text-anchor="middle">1969</text>
    <text class="fig-muted" x="72" y="153" font-size="10" text-anchor="middle">ARPANET</text>
    <text class="fig-accent" x="172" y="138" font-size="11" text-anchor="middle">1983 ★</text>
    <text class="fig-accent" x="172" y="153" font-size="10" text-anchor="middle">TCP/IP 定标（诞生）</text>
    <text class="fig-label" x="264" y="138" font-size="11" text-anchor="middle">1985</text>
    <text class="fig-muted" x="264" y="153" font-size="10" text-anchor="middle">建 NSFNET</text>
    <text class="fig-label" x="362" y="138" font-size="11" text-anchor="middle">1991</text>
    <text class="fig-muted" x="362" y="153" font-size="10" text-anchor="middle">商业化收费</text>
    <text class="fig-label" x="462" y="138" font-size="11" text-anchor="middle">1993</text>
    <text class="fig-muted" x="462" y="153" font-size="10" text-anchor="middle">多层 ISP</text>
    <text class="fig-label" x="576" y="138" font-size="11" text-anchor="middle">1995</text>
    <text class="fig-muted" x="576" y="153" font-size="10" text-anchor="middle">NSFNET 停运</text>
    <text class="fig-title" x="36" y="195" font-size="12">标准化组织（ISOC → IAB → IETF / IRTF）与正式标准四阶段（★ 并非所有 RFC 都是标准）</text>
    <text class="fig-label" x="106" y="223" font-size="12" text-anchor="middle">① 因特网草案</text>
    <text class="fig-muted" x="106" y="240" font-size="10" text-anchor="middle">Internet Draft（非 RFC）</text>
    <text class="fig-blue" x="274" y="223" font-size="12" text-anchor="middle">② 建议标准</text>
    <text class="fig-muted" x="274" y="240" font-size="10" text-anchor="middle">开始成为 RFC 文档</text>
    <text class="fig-orange" x="434" y="223" font-size="12" text-anchor="middle">③ 草案标准</text>
    <text class="fig-muted" x="434" y="240" font-size="10" text-anchor="middle">RFC 互操作验证</text>
    <text class="fig-green" x="598" y="223" font-size="12" text-anchor="middle">④ 因特网标准</text>
    <text class="fig-muted" x="598" y="240" font-size="10" text-anchor="middle">正式国际标准（少数 RFC）</text>
  </svg>
  <figcaption>图 1-2　因特网发展的三个历史阶段（1983 年 TCP/IP 成为标准标志因特网诞生）与因特网正式标准四阶段</figcaption>
</figure>

<strong>标准化特点</strong>：面向公众，所有 RFC 技术文档可免费下载，任何人可用电子邮件对某个文档发表意见。组织关系为 <strong>ISOC</strong>（因特网协会，全面管理）→ <strong>IAB</strong>（管理协议开发）→ <strong>IETF</strong>（工程部，研究短期工程问题，负责协议开发与标准化）与 <strong>IRTF</strong>（研究部，长期理论研究）。

正式标准需经四个阶段：

| 阶段 | 是否 RFC |
|---|---|
| 因特网草案 | 否 |
| 建议标准 | 是（从此开始成为 RFC） |
| 草案标准 | 是 |
| 因特网标准 | 是 |

> <strong>★考点</strong>：并非所有 RFC 文档都是因特网标准，只有一小部分最终成为标准。

### 1.4 ISP 的层次结构与因特网的组成

普通用户通过 ISP（因特网服务提供者）接入因特网。ISP 从因特网管理机构申请到成块的 <strong>IP 地址</strong>，并拥有通信线路、路由器等联网设备；用户缴纳费用后从 ISP 取得 IP 地址——因特网上的主机<strong>必须有 IP 地址</strong>才能通信。我国的三大 ISP 是中国电信、中国联通、中国移动。

<figure class="cpd-figure not-content" id="cn-w1-fig-3">
  <svg viewBox="0 0 700 304" role="img" aria-label="三层ISP因特网服务提供者互联结构示意图">
    <defs>
      <marker id="cn-w1-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="84" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="110" width="664" height="82" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="202" width="664" height="86" rx="8"/>
    <line class="fig-line-accent" x1="260" y1="62" x2="294" y2="62"/>
    <line class="fig-line-accent" x1="426" y1="62" x2="460" y2="62"/>
    <line class="fig-line" x1="194" y1="82" x2="230" y2="132" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="360" y1="82" x2="275" y2="132" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="360" y1="82" x2="445" y2="132" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="526" y1="82" x2="495" y2="132" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line-dashed" x1="322" y1="154" x2="398" y2="154"/>
    <line class="fig-line" x1="230" y1="174" x2="170" y2="224" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="265" y1="174" x2="345" y2="224" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="455" y1="174" x2="375" y2="224" marker-end="url(#cn-w1-arr-3)"/>
    <line class="fig-line" x1="490" y1="174" x2="550" y2="224" marker-end="url(#cn-w1-arr-3)"/>
    <rect class="fig-box-accent" x="128" y="42" width="132" height="40" rx="6"/>
    <rect class="fig-box-accent" x="294" y="42" width="132" height="40" rx="6"/>
    <rect class="fig-box-accent" x="460" y="42" width="132" height="40" rx="6"/>
    <rect class="fig-box-blue" x="182" y="134" width="140" height="40" rx="6"/>
    <rect class="fig-box-purple" x="334" y="141" width="52" height="26" rx="4"/>
    <rect class="fig-box-blue" x="398" y="134" width="140" height="40" rx="6"/>
    <rect class="fig-box-green" x="108" y="226" width="124" height="40" rx="6"/>
    <rect class="fig-box-green" x="298" y="226" width="124" height="40" rx="6"/>
    <rect class="fig-box-green" x="488" y="226" width="124" height="40" rx="6"/>
    <text class="fig-title" x="32" y="36" font-size="11">主干层</text>
    <text class="fig-muted" x="32" y="54" font-size="10">国际覆盖</text>
    <text class="fig-muted" x="32" y="69" font-size="10">对等直连</text>
    <text class="fig-accent" x="194" y="59" font-size="12" text-anchor="middle">第一层 ISP A</text>
    <text class="fig-muted" x="194" y="74" font-size="10" text-anchor="middle">高速主干网</text>
    <text class="fig-accent" x="360" y="59" font-size="12" text-anchor="middle">第一层 ISP B</text>
    <text class="fig-muted" x="360" y="74" font-size="10" text-anchor="middle">高速主干网</text>
    <text class="fig-accent" x="526" y="59" font-size="12" text-anchor="middle">第一层 ISP C</text>
    <text class="fig-muted" x="526" y="74" font-size="10" text-anchor="middle">高速主干网</text>
    <text class="fig-title" x="32" y="132" font-size="11">区域层</text>
    <text class="fig-muted" x="32" y="150" font-size="10">国家级覆盖</text>
    <text class="fig-muted" x="32" y="165" font-size="10">向顶层付费</text>
    <text class="fig-blue" x="252" y="151" font-size="12" text-anchor="middle">第二层 ISP 1</text>
    <text class="fig-muted" x="252" y="166" font-size="10" text-anchor="middle">区域 / 大公司接入</text>
    <text class="fig-purple" x="360" y="158" font-size="11" text-anchor="middle">IXP</text>
    <text class="fig-blue" x="468" y="151" font-size="12" text-anchor="middle">第二层 ISP 2</text>
    <text class="fig-muted" x="468" y="166" font-size="10" text-anchor="middle">区域 / 大公司接入</text>
    <text class="fig-title" x="32" y="224" font-size="11">接入层</text>
    <text class="fig-muted" x="32" y="242" font-size="10">分配 IP 地址</text>
    <text class="fig-muted" x="32" y="257" font-size="10">连接端系统</text>
    <text class="fig-green" x="170" y="243" font-size="12" text-anchor="middle">本地 ISP</text>
    <text class="fig-muted" x="170" y="258" font-size="10" text-anchor="middle">校园网 / 企业网</text>
    <text class="fig-green" x="360" y="243" font-size="12" text-anchor="middle">本地 ISP</text>
    <text class="fig-muted" x="360" y="258" font-size="10" text-anchor="middle">住宅宽带用户</text>
    <text class="fig-green" x="550" y="243" font-size="12" text-anchor="middle">本地 ISP</text>
    <text class="fig-muted" x="550" y="258" font-size="10" text-anchor="middle">无线移动用户</text>
  </svg>
  <figcaption>图 1-3　三层 ISP 互联结构示意图（IXP 因特网交换点支持同层 ISP 快速交换流量，主机须从 ISP 获取 IP 地址）</figcaption>
</figure>

| 层次 | 覆盖范围 | 用户 |
|---|---|---|
| 第一层 ISP | 国际性区域，故称<strong>因特网主干网</strong>，拥有高速链路和交换设备 | 彼此直接互联；第二层 ISP 和大公司是它的用户 |
| 第二层 ISP | 区域性、国家性 | 与少数第一层 ISP 相连；大公司也是其用户 |
| 第三层 ISP（本地 ISP） | 本地范围 | 校园网、企业网、住宅用户、无线移动用户 |

因特网规模太大，难以细致描述整体结构，但“相隔较远的两台主机通信需经过多个 ISP”是常见情形。一个用户接入因特网后，只要购买调制解调器或路由器等设备，自己也可以成为 ISP，因此该层次图只是<strong>示意图</strong>。

从功能上看，因特网由两部分组成：

| 部分 | 构成 | 作用 |
|---|---|---|
| <strong>边缘部分</strong> | 所有连接在因特网上的主机（台式机、服务器、笔记本、平板、手机、智能手表、物联网硬件） | 用户直接使用，进行<strong>通信和资源共享</strong> |
| <strong>核心部分</strong> | 大量网络 + 连接这些网络的路由器 | 为边缘部分提供<strong>连通性和交换</strong> |

<strong>路由器</strong>是专用计算机（不称主机），是<strong>实现分组交换的关键构件</strong>，任务是<strong>转发收到的分组</strong>，这是网络核心部分最重要的功能。

<figure class="cpd-figure not-content" id="cn-w1-fig-4">
  <svg viewBox="0 0 700 264" role="img" aria-label="因特网边缘部分与核心部分的功能组成及CS和P2P通信方式">
    <defs>
      <marker id="cn-w1-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="18" width="194" height="228" rx="8"/>
    <rect class="fig-box-subtle" x="230" y="18" width="240" height="228" rx="8"/>
    <rect class="fig-box-subtle" x="488" y="18" width="194" height="228" rx="8"/>
    <line class="fig-line-accent" x1="176" y1="84" x2="270" y2="96"/>
    <line class="fig-line-accent" x1="176" y1="158" x2="270" y2="156"/>
    <line class="fig-line" x1="292" y1="96" x2="408" y2="96"/>
    <line class="fig-line" x1="292" y1="96" x2="350" y2="156"/>
    <line class="fig-line" x1="408" y1="96" x2="350" y2="156"/>
    <line class="fig-line" x1="292" y1="156" x2="350" y2="156"/>
    <line class="fig-line" x1="350" y1="156" x2="408" y2="156"/>
    <line class="fig-line-accent" x1="430" y1="96" x2="524" y2="84"/>
    <line class="fig-line-accent" x1="430" y1="156" x2="524" y2="158"/>
    <line class="fig-line-dashed" x1="585" y1="106" x2="585" y2="136" marker-start="url(#cn-w1-arr-4)" marker-end="url(#cn-w1-arr-4)"/>
    <rect class="fig-box-blue" x="42" y="64" width="134" height="42" rx="6"/>
    <rect class="fig-box-green" x="42" y="136" width="134" height="44" rx="6"/>
    <circle class="fig-box-orange" cx="292" cy="96" r="22"/>
    <circle class="fig-box-orange" cx="408" cy="96" r="22"/>
    <circle class="fig-box-orange" cx="292" cy="156" r="22"/>
    <circle class="fig-box-orange" cx="350" cy="156" r="22"/>
    <circle class="fig-box-orange" cx="408" cy="156" r="22"/>
    <rect class="fig-box-purple" x="524" y="64" width="132" height="42" rx="6"/>
    <rect class="fig-box-purple" x="524" y="136" width="132" height="42" rx="6"/>
    <text class="fig-title" x="115" y="40" font-size="12" text-anchor="middle">边缘部分（C/S 方式）</text>
    <text class="fig-blue" x="109" y="82" font-size="12" text-anchor="middle">客户机 Client</text>
    <text class="fig-muted" x="109" y="97" font-size="10" text-anchor="middle">主动请求服务</text>
    <text class="fig-green" x="109" y="155" font-size="12" text-anchor="middle">服务器 Server</text>
    <text class="fig-muted" x="109" y="171" font-size="10" text-anchor="middle">被动提供资源共享</text>
    <text class="fig-muted" x="115" y="208" font-size="11" text-anchor="middle">端系统（主机）运行</text>
    <text class="fig-accent" x="115" y="226" font-size="11" text-anchor="middle">完整五层协议栈（进程通信）</text>
    <text class="fig-title" x="350" y="40" font-size="12" text-anchor="middle">核心部分（网状路由）</text>
    <text class="fig-muted" x="350" y="60" font-size="10" text-anchor="middle">大量网络 + 专用路由器</text>
    <text class="fig-orange" x="292" y="100" font-size="11" text-anchor="middle">R1</text>
    <text class="fig-orange" x="408" y="100" font-size="11" text-anchor="middle">R2</text>
    <text class="fig-orange" x="292" y="160" font-size="11" text-anchor="middle">R3</text>
    <text class="fig-orange" x="350" y="160" font-size="11" text-anchor="middle">R4</text>
    <text class="fig-orange" x="408" y="160" font-size="11" text-anchor="middle">R5</text>
    <text class="fig-muted" x="350" y="208" font-size="11" text-anchor="middle">路由器是专用计算机（非主机）</text>
    <text class="fig-orange" x="350" y="226" font-size="11" text-anchor="middle">核心任务：分组存储转发（连通+交换）</text>
    <text class="fig-title" x="585" y="40" font-size="12" text-anchor="middle">边缘部分（P2P 方式）</text>
    <text class="fig-purple" x="590" y="82" font-size="12" text-anchor="middle">对等主机 Peer A</text>
    <text class="fig-muted" x="590" y="97" font-size="10" text-anchor="middle">既是客户又是服务器</text>
    <text class="fig-purple" x="590" y="154" font-size="12" text-anchor="middle">对等主机 Peer B</text>
    <text class="fig-muted" x="590" y="169" font-size="10" text-anchor="middle">双向对等下载 / 共享</text>
    <text class="fig-muted" x="585" y="208" font-size="11" text-anchor="middle">边缘部分：用户直接使用</text>
    <text class="fig-purple" x="585" y="226" font-size="11" text-anchor="middle">本质仍是主机内进程间通信</text>
  </svg>
  <figcaption>图 1-4　因特网的功能划分：边缘部分端系统负责通信与资源共享（C/S 或 P2P），核心部分路由器群提供连通性与分组交换</figcaption>
</figure>

### 1.5 三种交换方式

<strong>电路交换</strong>的由来：n 部电话两两相连需要 $\frac{n(n-1)}{2}$ 对电线，数量过大，于是用<strong>电话交换机作为中间设备，把需要通信的任意两部电话按需接通。电话交换机接通电话线的方式称为电路交换。从通信资源分配角度看，交换就是按某种方式动态地分配传输线路资源</strong>。

电路交换分三步：

1. <strong>建立连接</strong>：拨号请求，从主叫端到被叫端建立一条专用的物理通路，通信资源被双方独占；
2. <strong>通话</strong>：双方通话期间，已分配的通信资源始终被占用；
3. <strong>释放连接</strong>：挂机后交换机归还全部通信资源。

> <strong>注意</strong>：用户线归电话用户专用，交换机之间的中继线则是许多用户共享的。

电路交换传计算机数据可行但<strong>传输效率很低</strong>：计算机数据是<strong>突发式</strong>出现在线路上的，用户输入、编辑文件时占用的通信资源暂时闲置，却不能被其他用户使用。因此计算机网络通常采用分组交换。

<figure class="cpd-figure not-content" id="cn-w1-fig-5">
  <svg viewBox="0 0 720 336" role="img" aria-label="电路交换、报文交换与分组交换的时空对比图">
    <defs>
      <marker id="cn-w1-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="218" height="304" rx="8"/>
    <rect class="fig-box-subtle" x="251" y="16" width="218" height="304" rx="8"/>
    <rect class="fig-box-subtle" x="486" y="16" width="218" height="304" rx="8"/>
    <line class="fig-line" x1="56" y1="62" x2="56" y2="294" marker-end="url(#cn-w1-arr-5)"/>
    <line class="fig-line-dashed" x1="102" y1="62" x2="102" y2="288"/>
    <line class="fig-line-dashed" x1="148" y1="62" x2="148" y2="288"/>
    <line class="fig-line-dashed" x1="194" y1="62" x2="194" y2="288"/>
    <line class="fig-line-accent" x1="56" y1="74" x2="194" y2="102" marker-end="url(#cn-w1-arr-5)"/>
    <line class="fig-line-dashed" x1="194" y1="104" x2="56" y2="128" marker-end="url(#cn-w1-arr-5)"/>
    <polygon class="fig-box-blue" points="56,132 194,160 194,224 56,196"/>
    <line class="fig-line-dashed" x1="56" y1="234" x2="194" y2="262" marker-end="url(#cn-w1-arr-5)"/>
    <line class="fig-line" x1="291" y1="62" x2="291" y2="294" marker-end="url(#cn-w1-arr-5)"/>
    <line class="fig-line-dashed" x1="337" y1="62" x2="337" y2="288"/>
    <line class="fig-line-dashed" x1="383" y1="62" x2="383" y2="288"/>
    <line class="fig-line-dashed" x1="429" y1="62" x2="429" y2="288"/>
    <polygon class="fig-box-orange" points="291,74 337,84 337,140 291,130"/>
    <polygon class="fig-box-orange" points="337,144 383,154 383,210 337,200"/>
    <polygon class="fig-box-orange" points="383,214 429,224 429,280 383,270"/>
    <line class="fig-line" x1="526" y1="62" x2="526" y2="294" marker-end="url(#cn-w1-arr-5)"/>
    <line class="fig-line-dashed" x1="572" y1="62" x2="572" y2="288"/>
    <line class="fig-line-dashed" x1="618" y1="62" x2="618" y2="288"/>
    <line class="fig-line-dashed" x1="664" y1="62" x2="664" y2="288"/>
    <polygon class="fig-box-green" points="526,74 572,84 572,102 526,92"/>
    <polygon class="fig-box-green" points="572,104 618,114 618,132 572,122"/>
    <polygon class="fig-box-green" points="618,134 664,144 664,162 618,152"/>
    <polygon class="fig-box-accent" points="526,94 572,104 572,122 526,112"/>
    <polygon class="fig-box-accent" points="572,124 618,134 618,152 572,142"/>
    <polygon class="fig-box-accent" points="618,154 664,164 664,182 618,172"/>
    <polygon class="fig-box-purple" points="526,114 572,124 572,142 526,132"/>
    <polygon class="fig-box-purple" points="572,144 618,154 618,172 572,162"/>
    <polygon class="fig-box-purple" points="618,174 664,184 664,202 618,192"/>
    <rect class="fig-box" x="90" y="70" width="70" height="16" rx="3"/>
    <rect class="fig-box" x="530" y="216" width="130" height="38" rx="4"/>
    <text class="fig-title" x="125" y="36" font-size="12" text-anchor="middle">① 电路交换</text>
    <text class="fig-label" x="56" y="56" font-size="11" text-anchor="middle">A</text>
    <text class="fig-label" x="102" y="56" font-size="11" text-anchor="middle">B</text>
    <text class="fig-label" x="148" y="56" font-size="11" text-anchor="middle">C</text>
    <text class="fig-label" x="194" y="56" font-size="11" text-anchor="middle">D</text>
    <text class="fig-accent" x="125" y="82" font-size="10" text-anchor="middle">呼叫建立 s</text>
    <text class="fig-blue" x="125" y="182" font-size="11" text-anchor="middle">比特流直通</text>
    <text class="fig-muted" x="125" y="198" font-size="10" text-anchor="middle">中间不存储转发</text>
    <text class="fig-muted" x="125" y="264" font-size="10" text-anchor="middle">释放连接</text>
    <text class="fig-muted" x="34" y="294" font-size="10">时间</text>
    <text class="fig-muted" x="135" y="308" font-size="10" text-anchor="middle">独占信道，突发数据效率低</text>
    <text class="fig-title" x="360" y="36" font-size="12" text-anchor="middle">② 报文交换</text>
    <text class="fig-label" x="291" y="56" font-size="11" text-anchor="middle">A</text>
    <text class="fig-label" x="337" y="56" font-size="11" text-anchor="middle">B</text>
    <text class="fig-label" x="383" y="56" font-size="11" text-anchor="middle">C</text>
    <text class="fig-label" x="429" y="56" font-size="11" text-anchor="middle">D</text>
    <text class="fig-orange" x="314" y="112" font-size="10" text-anchor="middle">整报</text>
    <text class="fig-orange" x="360" y="182" font-size="10" text-anchor="middle">整存</text>
    <text class="fig-orange" x="406" y="252" font-size="10" text-anchor="middle">整转</text>
    <text class="fig-muted" x="269" y="294" font-size="10">时间</text>
    <text class="fig-muted" x="370" y="308" font-size="10" text-anchor="middle">无需建连，转发时延大、占大缓存</text>
    <text class="fig-title" x="595" y="36" font-size="12" text-anchor="middle">③ 分组交换（流水线）</text>
    <text class="fig-label" x="526" y="56" font-size="11" text-anchor="middle">A</text>
    <text class="fig-label" x="572" y="56" font-size="11" text-anchor="middle">B</text>
    <text class="fig-label" x="618" y="56" font-size="11" text-anchor="middle">C</text>
    <text class="fig-label" x="664" y="56" font-size="11" text-anchor="middle">D</text>
    <text class="fig-green" x="514" y="88" font-size="10" text-anchor="end">P1</text>
    <text class="fig-accent" x="514" y="108" font-size="10" text-anchor="end">P2</text>
    <text class="fig-purple" x="514" y="128" font-size="10" text-anchor="end">P3</text>
    <text class="fig-green" x="595" y="230" font-size="11" text-anchor="middle">流水线并行存储转发！</text>
    <text class="fig-muted" x="595" y="248" font-size="10" text-anchor="middle">B 转发 P1 同时正在收 P2</text>
    <text class="fig-muted" x="504" y="294" font-size="10">时间</text>
    <text class="fig-accent" x="605" y="308" font-size="10" text-anchor="middle">总时延最小，缓存固定，利用率高</text>
  </svg>
  <figcaption>图 1-5　三种交换方式时空图对比（横轴节点 A→D，纵轴时间向下）：分组交换通过拆包流水线大幅压缩存储转发总时延</figcaption>
</figure>

<strong>报文交换</strong>：把表示消息的整块数据称为<strong>报文</strong>，整个报文先送到相邻节点交换机，全部存储下来后查表转发。对报文大小没有限制，主要用于早期电报通信网，现已被分组交换取代。

<strong>分组交换</strong>：发送前先把较长的报文划分成一个个<strong>更小的等长数据段</strong>，每个数据段前加上必要的控制信息组成的<strong>首部</strong>，构成<strong>分组</strong>。

> <strong>★考点</strong>：首部不是多余的——它携带目的地址等控制信息，分组交换机正是按首部中的目的地址<strong>查表转发</strong>，否则不知道如何转发。

工作过程：主机 H6 依次发送各分组 → 各分组交换机<strong>存储转发</strong>（先缓存、再检查首部、按目的地址查表、从合适接口转发）→ 目的主机 H2 去掉首部、把各数据段组合<strong>还原出原始报文</strong>。

三方的任务划分：

| 角色 | 任务 |
|---|---|
| 发送方 | 构造分组、发送分组 |
| 分组交换机（路由器） | 缓存分组、转发分组，合称<strong>存储转发</strong> |
| 接收方 | 接收分组、还原报文 |

本例只演示了两种情形：各分组可走<strong>不同的路径（路由）</strong>；分组可能<strong>乱序</strong>到达。实际还可能出现分组丢失、误码、重复，这些由后续章节处理。

<figure class="cpd-figure not-content" id="cn-w1-fig-6">
  <svg viewBox="0 0 700 256" role="img" aria-label="分组交换报文拆分加首部、多路径存储转发与目的端按序重组过程">
    <defs>
      <marker id="cn-w1-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="18" width="184" height="220" rx="8"/>
    <rect class="fig-box-subtle" x="218" y="18" width="264" height="220" rx="8"/>
    <rect class="fig-box-subtle" x="498" y="18" width="184" height="220" rx="8"/>
    <line class="fig-line" x1="110" y1="80" x2="110" y2="98" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line-accent" x1="192" y1="116" x2="246" y2="118" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line" x1="290" y1="106" x2="346" y2="78" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line" x1="290" y1="126" x2="346" y2="162" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line" x1="390" y1="78" x2="430" y2="108" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line" x1="390" y1="162" x2="430" y2="132" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line-accent" x1="474" y1="120" x2="510" y2="120" marker-end="url(#cn-w1-arr-6)"/>
    <line class="fig-line" x1="590" y1="146" x2="590" y2="166" marker-end="url(#cn-w1-arr-6)"/>
    <rect class="fig-box" x="34" y="48" width="152" height="30" rx="4"/>
    <rect class="fig-box-orange" x="34" y="104" width="22" height="24" rx="2"/>
    <rect class="fig-box-blue" x="56" y="104" width="30" height="24" rx="2"/>
    <rect class="fig-box-orange" x="92" y="104" width="22" height="24" rx="2"/>
    <rect class="fig-box-blue" x="114" y="104" width="30" height="24" rx="2"/>
    <rect class="fig-box-orange" x="150" y="104" width="18" height="24" rx="2"/>
    <rect class="fig-box-blue" x="168" y="104" width="22" height="24" rx="2"/>
    <circle class="fig-box-orange" cx="268" cy="118" r="22"/>
    <circle class="fig-box-orange" cx="368" cy="76" r="22"/>
    <circle class="fig-box-orange" cx="368" cy="164" r="22"/>
    <circle class="fig-box-orange" cx="452" cy="118" r="22"/>
    <rect class="fig-box-purple" x="514" y="104" width="48" height="38" rx="4"/>
    <rect class="fig-box-purple" x="566" y="104" width="48" height="38" rx="4"/>
    <rect class="fig-box-purple" x="618" y="104" width="48" height="38" rx="4"/>
    <rect class="fig-box-green" x="514" y="170" width="152" height="32" rx="4"/>
    <text class="fig-title" x="110" y="38" font-size="12" text-anchor="middle">① 发送主机 H6（拆包）</text>
    <text class="fig-label" x="110" y="67" font-size="11" text-anchor="middle">长报文（D1 + D2 + D3）</text>
    <text class="fig-orange" x="45" y="120" font-size="9" text-anchor="middle">H</text>
    <text class="fig-blue" x="71" y="120" font-size="9" text-anchor="middle">D1</text>
    <text class="fig-orange" x="103" y="120" font-size="9" text-anchor="middle">H</text>
    <text class="fig-blue" x="129" y="120" font-size="9" text-anchor="middle">D2</text>
    <text class="fig-orange" x="159" y="120" font-size="9" text-anchor="middle">H</text>
    <text class="fig-blue" x="179" y="120" font-size="9" text-anchor="middle">D3</text>
    <text class="fig-muted" x="110" y="152" font-size="10" text-anchor="middle">★ 首部 H 含目的地址与序号</text>
    <text class="fig-muted" x="110" y="170" font-size="10" text-anchor="middle">每个数据段独立封装为分组</text>
    <text class="fig-orange" x="110" y="192" font-size="10" text-anchor="middle">代价：增加额外控制信息开销</text>
    <text class="fig-title" x="350" y="38" font-size="12" text-anchor="middle">② 路由器群（独立选路存储转发）</text>
    <text class="fig-orange" x="268" y="122" font-size="11" text-anchor="middle">R1</text>
    <text class="fig-orange" x="368" y="80" font-size="11" text-anchor="middle">R2</text>
    <text class="fig-orange" x="368" y="168" font-size="11" text-anchor="middle">R3</text>
    <text class="fig-orange" x="452" y="122" font-size="11" text-anchor="middle">R4</text>
    <text class="fig-blue" x="314" y="76" font-size="10" text-anchor="middle">P1, P3</text>
    <text class="fig-purple" x="314" y="162" font-size="10" text-anchor="middle">P2 绕行</text>
    <text class="fig-muted" x="350" y="206" font-size="10" text-anchor="middle">入队缓存 → 读首部目的地址 → 查转发表</text>
    <text class="fig-accent" x="350" y="222" font-size="10" text-anchor="middle">故障时可动态改走备用路由</text>
    <text class="fig-title" x="590" y="38" font-size="12" text-anchor="middle">③ 目的主机 H2（重组）</text>
    <text class="fig-muted" x="590" y="88" font-size="10" text-anchor="middle">到达顺序可能失序：</text>
    <text class="fig-purple" x="538" y="127" font-size="11" text-anchor="middle">P1</text>
    <text class="fig-purple" x="590" y="127" font-size="11" text-anchor="middle">P3</text>
    <text class="fig-purple" x="642" y="127" font-size="11" text-anchor="middle">P2</text>
    <text class="fig-green" x="590" y="190" font-size="11" text-anchor="middle">去首部重排 → D1 + D2 + D3</text>
    <text class="fig-muted" x="590" y="220" font-size="10" text-anchor="middle">注意处理失序、丢包、误码、重复</text>
  </svg>
  <figcaption>图 1-6　分组交换端到端流程：发送端拆包加首部，路由器按首部目的地址动态查表存储转发，接收端按序去首部还原报文</figcaption>
</figure>

<strong>三种方式的比较</strong>

| | 电路交换 | 报文交换 | 分组交换 |
|---|---|---|---|
| 连接 | 通信前必须建立连接 | 无需建立连接 | 无需建立连接 |
| 数据单元 | 比特流直通 | 整个报文 | 分组 |
| 转发方式 | 中间节点直通 | 存储转发 | 存储转发 |
| 缓存要求 | 无 | 需较大缓存（报文长度不限） | 缓存较小且固定（分组等长） |
| 时延 | 通信时延小 | 有转发时延 | 转发时延比报文交换小 |
| 线路利用率 | 低（独占） | 较高（分段占用） | 高（分段占用） |

<strong>电路交换</strong>

- 优点：① 通信时延小（专用线路、数据直达，连续传大量数据时优势明显）；② 有序传输（只有一条专用线路，不存在失序）；③ 没有冲突（双方拥有不同信道，不会争用物理信道）；④ 适用于传输数字信号；⑤ 实时性强；⑥ 控制简单。
- 缺点：① 建立连接时间长；② 线路独占、使用效率低（线路空闲也不能给别人用）；③ 灵活性差（通路任一点故障都必须重新拨号）；④ 难以规格化（不同类型、规格、速率的终端很难互通，也难以在通信过程中进行差错控制）。

<strong>报文交换</strong>

- 优点：① 无需建立连接，不存在建立连接时延；② 动态分配线路（先存整个报文，再选空闲线路发出）；③ 提高线路可靠性（某路径故障可改走另一条）；④ 提高线路利用率（不同时间分段占用物理线路）；⑤ 可提供多目标服务（一个报文同时发往多个目的地址）。
- 缺点：① 引起转发时延；② 需要较大的存储缓存空间；③ 需传输额外的信息量（目的地址、源地址等）。

<strong>分组交换</strong>

- 优点：① 无需建立连接；② 线路利用率高；③ 简化了存储管理（分组长度固定，缓冲区大小固定）；④ 可流水线处理——后一个分组的存储操作与前一个分组的转发操作可同时进行；⑤ 减少出错概率和重发数据量（分组小，出错只需重传出错部分，提高可靠性又减少传输时延）。
- 缺点：① 引起转发时延；② 需传输额外的信息量（每个数据块都要加源地址、目的地址等控制信息）；③ 采用<strong>数据报</strong>服务时可能失序、丢失或重复，到达目的节点后还原报文较麻烦；采用<strong>虚电路</strong>服务虽无失序问题，但有呼叫建立、数据传输、虚电路释放三个过程。

> <strong>★考点</strong>：分组交换是本节的绝对重点，尤其是“分组”与“存储转发”两个概念；三种方式的差别常以“传送同样大小的数据，哪种时延更小”的形式考查。

### 1.6 体系结构分层的必要性

计算机网络是非常复杂的系统，ARPANET 最初设计时就提出了<strong>分层</strong>理念：把庞大而复杂的问题转化为若干较小的局部问题，每个局部问题都容易研究和处理。

按由简单到复杂的顺序，实现计算机网络要解决的问题可逐层划归：

| 层次 | 要解决的主要问题 | 具体内容 |
|---|---|---|
| <strong>物理层</strong> | 用何种信号传输比特 | 采用怎样的传输媒体（双绞线等）、计算机上采用怎样的物理接口（如 RJ-45 以太网接口）、用怎样的信号表示 0 和 1 |
| <strong>数据链路层</strong> | 分组在一个网络/一段链路上传输 | 主机编址（MAC 地址）、帧的封装格式（使目的主机能从比特流中区分地址和数据）、总线争用/介质访问控制；现代用<strong>以太网交换机</strong>构成交换式以太网 |
| <strong>网络层</strong> | 分组在多个网络间传输与路由 | 网络与主机的共同编址（IP 地址：网络号 + 主机号）、路由器如何转发分组、如何选择路由 |
| <strong>运输层</strong> | 进程之间基于网络的通信 | 标识与网络通信相关的应用进程（端口）、传输出现误码或路由器因繁忙丢包时如何处理（可靠传输） |
| <strong>应用层</strong> | 通过应用进程交互实现特定网络应用 | 制定 HTTP、SMTP、FTP 等应用层协议并编写相应应用程序 |

> <strong>注意：严格来说，传输媒体并不属于物理层</strong>，它不在体系结构之内；课程中把信号画成方波只是为了便于初学者理解，实际网络中的信号并非方波。

只要依次解决物理层、数据链路层的问题，就能实现分组<strong>在一个网络上</strong>传输；再加上网络层，才能实现分组<strong>在多个网络间</strong>传输；再加上运输层，才能实现<strong>进程之间</strong>的通信；最后靠应用层完成具体应用。

> <strong>★考点</strong>：五层各自的一句话职责是必背内容——物理层传比特，链路层传帧（一段链路），网络层传分组（多网络路由），运输层传报文段（进程间），应用层完成特定应用。

<figure class="cpd-figure not-content" id="cn-w1-fig-7">
  <svg viewBox="0 0 720 356" role="img" aria-label="OSI七层、TCPIP四层与五层原理体系结构对照及各层职责设备映射">
    <defs>
      <marker id="cn-w1-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="324" rx="8"/>
    <line class="fig-line-dashed" x1="164" y1="54" x2="486" y2="54"/>
    <line class="fig-line-dashed" x1="164" y1="150" x2="486" y2="150"/>
    <line class="fig-line-dashed" x1="164" y1="190" x2="486" y2="190"/>
    <line class="fig-line-dashed" x1="164" y1="230" x2="486" y2="230"/>
    <line class="fig-line-dashed" x1="164" y1="306" x2="486" y2="306"/>
    <rect class="fig-box-purple" x="30" y="56" width="134" height="28" rx="4"/>
    <rect class="fig-box-purple" x="30" y="88" width="134" height="28" rx="4"/>
    <rect class="fig-box-purple" x="30" y="120" width="134" height="28" rx="4"/>
    <rect class="fig-box-blue" x="30" y="154" width="134" height="32" rx="4"/>
    <rect class="fig-box-orange" x="30" y="194" width="134" height="32" rx="4"/>
    <rect class="fig-box-green" x="30" y="234" width="134" height="32" rx="4"/>
    <rect class="fig-box" x="30" y="272" width="134" height="32" rx="4"/>
    <rect class="fig-box-purple" x="190" y="56" width="136" height="92" rx="4"/>
    <rect class="fig-box-blue" x="190" y="154" width="136" height="32" rx="4"/>
    <rect class="fig-box-orange" x="190" y="194" width="136" height="32" rx="4"/>
    <rect class="fig-box" x="190" y="234" width="136" height="70" rx="4"/>
    <rect class="fig-box-purple" x="352" y="56" width="134" height="92" rx="4"/>
    <rect class="fig-box-blue" x="352" y="154" width="134" height="32" rx="4"/>
    <rect class="fig-box-orange" x="352" y="194" width="134" height="32" rx="4"/>
    <rect class="fig-box-green" x="352" y="234" width="134" height="32" rx="4"/>
    <rect class="fig-box" x="352" y="272" width="134" height="32" rx="4"/>
    <rect class="fig-box-dashed" x="30" y="310" width="456" height="24" rx="4"/>
    <rect class="fig-box" x="502" y="56" width="188" height="92" rx="4"/>
    <rect class="fig-box" x="502" y="154" width="188" height="32" rx="4"/>
    <rect class="fig-box" x="502" y="194" width="188" height="32" rx="4"/>
    <rect class="fig-box" x="502" y="234" width="188" height="32" rx="4"/>
    <rect class="fig-box" x="502" y="272" width="188" height="32" rx="4"/>
    <text class="fig-title" x="97" y="42" font-size="12" text-anchor="middle">OSI 七层（法律标准）</text>
    <text class="fig-title" x="258" y="42" font-size="12" text-anchor="middle">TCP/IP 四层（事实标准）</text>
    <text class="fig-title" x="419" y="42" font-size="12" text-anchor="middle">五层原理（教学体系）</text>
    <text class="fig-title" x="596" y="42" font-size="12" text-anchor="middle">作用域 / PDU / 典型设备</text>
    <text class="fig-purple" x="97" y="74" font-size="11" text-anchor="middle">7 应用层</text>
    <text class="fig-purple" x="97" y="106" font-size="11" text-anchor="middle">6 表示层</text>
    <text class="fig-purple" x="97" y="138" font-size="11" text-anchor="middle">5 会话层</text>
    <text class="fig-blue" x="97" y="174" font-size="11" text-anchor="middle">4 运输层</text>
    <text class="fig-orange" x="97" y="214" font-size="11" text-anchor="middle">3 网络层</text>
    <text class="fig-green" x="97" y="254" font-size="11" text-anchor="middle">2 数据链路层</text>
    <text class="fig-label" x="97" y="292" font-size="11" text-anchor="middle">1 物理层</text>
    <text class="fig-purple" x="258" y="98" font-size="12" text-anchor="middle">应用层</text>
    <text class="fig-muted" x="258" y="116" font-size="10" text-anchor="middle">HTTP / DNS / SMTP</text>
    <text class="fig-blue" x="258" y="174" font-size="11" text-anchor="middle">运输层 (TCP / UDP)</text>
    <text class="fig-orange" x="258" y="214" font-size="11" text-anchor="middle">网际层 IP</text>
    <text class="fig-label" x="258" y="266" font-size="11" text-anchor="middle">网络接口层</text>
    <text class="fig-muted" x="258" y="284" font-size="10" text-anchor="middle">（未规定具体内容）</text>
    <text class="fig-purple" x="419" y="98" font-size="12" text-anchor="middle">5 应用层</text>
    <text class="fig-muted" x="419" y="116" font-size="10" text-anchor="middle">特定网络应用交互</text>
    <text class="fig-blue" x="419" y="174" font-size="11" text-anchor="middle">4 运输层</text>
    <text class="fig-orange" x="419" y="214" font-size="11" text-anchor="middle">3 网络层</text>
    <text class="fig-green" x="419" y="254" font-size="11" text-anchor="middle">2 数据链路层</text>
    <text class="fig-label" x="419" y="292" font-size="11" text-anchor="middle">1 物理层</text>
    <text class="fig-red" x="258" y="326" font-size="10" text-anchor="middle">★ 传输媒体（双绞线 / 光纤 / 无线信道）：第 0 层，严格不属于网络体系结构！</text>
    <text class="fig-label" x="596" y="96" font-size="11" text-anchor="middle">应用进程交互 · 报文</text>
    <text class="fig-muted" x="596" y="114" font-size="10" text-anchor="middle">端系统实现（网关）</text>
    <text class="fig-blue" x="596" y="174" font-size="10" text-anchor="middle">进程间通信 · 报文段(端口)</text>
    <text class="fig-orange" x="596" y="214" font-size="10" text-anchor="middle">跨网路由 · 分组 · 路由器</text>
    <text class="fig-green" x="596" y="254" font-size="10" text-anchor="middle">单段链路 · 帧 · 交换机/网桥</text>
    <text class="fig-muted" x="596" y="292" font-size="10" text-anchor="middle">传比特流 · 集线器/中继器</text>
  </svg>
  <figcaption>图 1-7　OSI 七层、TCP/IP 四层与五层原理体系结构的层间映射及各层 PDU、职责范围与典型硬件设备</figcaption>
</figure>

### 1.7 分层思想举例：一次 HTTP 访问

主机（网络 N1）用浏览器访问 Web 服务器（网络 N2），二者经路由器互联。通信的本质是<strong>浏览器进程与 Web 服务器进程之间</strong>基于网络的通信。以主机发送 HTTP 请求为例，自顶向下逐层封装：

| 层 | 操作 | 结果 | 首部作用 |
|---|---|---|---|
| 应用层 | 按 HTTP 协议构建请求报文 | HTTP 请求报文 | — |
| 运输层 | 加 TCP 首部 | TCP 报文段 | 区分应用进程、实现可靠传输 |
| 网络层 | 加 IP 首部 | IP 数据报 | 使数据报能在互联网上被路由器转发 |
| 数据链路层 | 加帧首部<strong>和帧尾部</strong> | 帧 | 首部使帧能在一段链路/一个网络上传输并被目的主机接收；尾部供目的主机<strong>检查误码</strong> |
| 物理层 | 视为比特流，加<strong>前导码</strong>，变换为信号 | 信号 | 前导码让目的主机做好接收帧的准备 |

<figure class="cpd-figure not-content" id="cn-w1-fig-8">
  <svg viewBox="0 0 700 296" role="img" aria-label="五层体系结构自顶向下逐层封装示意图">
    <defs>
      <marker id="cn-w1-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="264" rx="8"/>
    <line class="fig-line-accent" x1="128" y1="36" x2="128" y2="232" marker-end="url(#cn-w1-arr-8)"/>
    <line class="fig-line-dashed" x1="360" y1="62" x2="360" y2="78"/>
    <line class="fig-line-dashed" x1="530" y1="62" x2="530" y2="78"/>
    <line class="fig-line-dashed" x1="296" y1="108" x2="296" y2="124"/>
    <line class="fig-line-dashed" x1="530" y1="108" x2="530" y2="124"/>
    <line class="fig-line-dashed" x1="232" y1="154" x2="232" y2="170"/>
    <line class="fig-line-dashed" x1="530" y1="154" x2="530" y2="170"/>
    <line class="fig-line-dashed" x1="168" y1="200" x2="232" y2="216"/>
    <line class="fig-line-dashed" x1="594" y1="200" x2="594" y2="216"/>
    <rect class="fig-box" x="30" y="34" width="82" height="28" rx="4"/>
    <rect class="fig-box" x="30" y="80" width="82" height="28" rx="4"/>
    <rect class="fig-box" x="30" y="126" width="82" height="28" rx="4"/>
    <rect class="fig-box" x="30" y="172" width="82" height="28" rx="4"/>
    <rect class="fig-box" x="30" y="218" width="82" height="28" rx="4"/>
    <rect class="fig-box-purple" x="360" y="34" width="170" height="28" rx="4"/>
    <rect class="fig-box-blue" x="296" y="80" width="64" height="28" rx="4"/>
    <rect class="fig-box-purple" x="360" y="80" width="170" height="28" rx="4"/>
    <rect class="fig-box-orange" x="232" y="126" width="64" height="28" rx="4"/>
    <rect class="fig-box" x="296" y="126" width="234" height="28" rx="4"/>
    <rect class="fig-box-green" x="168" y="172" width="64" height="28" rx="4"/>
    <rect class="fig-box" x="232" y="172" width="298" height="28" rx="4"/>
    <rect class="fig-box-red" x="530" y="172" width="64" height="28" rx="4"/>
    <rect class="fig-box-accent" x="168" y="218" width="64" height="28" rx="4"/>
    <rect class="fig-box" x="232" y="218" width="362" height="28" rx="4"/>
    <text class="fig-label" x="71" y="52" font-size="11" text-anchor="middle">5 应用层</text>
    <text class="fig-label" x="71" y="98" font-size="11" text-anchor="middle">4 运输层</text>
    <text class="fig-label" x="71" y="144" font-size="11" text-anchor="middle">3 网络层</text>
    <text class="fig-label" x="71" y="190" font-size="11" text-anchor="middle">2 数据链路层</text>
    <text class="fig-label" x="71" y="236" font-size="11" text-anchor="middle">1 物理层</text>
    <text class="fig-accent" x="142" y="138" font-size="10">封装</text>
    <text class="fig-purple" x="445" y="52" font-size="11" text-anchor="middle">HTTP 请求报文</text>
    <text class="fig-blue" x="328" y="98" font-size="10" text-anchor="middle">TCP 首部</text>
    <text class="fig-purple" x="445" y="98" font-size="11" text-anchor="middle">HTTP 请求报文</text>
    <text class="fig-muted" x="542" y="98" font-size="10">→ TCP 报文段</text>
    <text class="fig-orange" x="264" y="144" font-size="10" text-anchor="middle">IP 首部</text>
    <text class="fig-label" x="413" y="144" font-size="11" text-anchor="middle">TCP 报文段（数据载荷）</text>
    <text class="fig-muted" x="542" y="144" font-size="10">→ IP 数据报</text>
    <text class="fig-green" x="200" y="190" font-size="10" text-anchor="middle">帧首部</text>
    <text class="fig-label" x="381" y="190" font-size="11" text-anchor="middle">IP 数据报（数据载荷）</text>
    <text class="fig-red" x="562" y="190" font-size="10" text-anchor="middle">帧尾部</text>
    <text class="fig-muted" x="604" y="190" font-size="10">→ 帧</text>
    <text class="fig-accent" x="200" y="236" font-size="10" text-anchor="middle">前导码</text>
    <text class="fig-label" x="413" y="236" font-size="11" text-anchor="middle">以太网帧比特流（101010... 变换为物理信号）</text>
    <text class="fig-muted" x="350" y="268" font-size="10" text-anchor="middle">★ 唯独数据链路层同时加“帧首部（MAC 寻址）”和“帧尾部（FCS 差错检测）”；物理层加“前导码”实现位同步</text>
  </svg>
  <figcaption>图 1-8　发送主机自顶向下五层封装过程：上层完整 PDU 作为下层数据载荷，逐层附加控制首部（链路层另加帧尾 FCS）</figcaption>
</figure>

<strong>路由器的处理</strong>：物理层把信号变回比特流并去掉前导码 → 数据链路层去掉帧首部和帧尾部，交出 IP 数据报 → 网络层解析 IP 首部，提取<strong>目的网络地址</strong>，查找自身<strong>路由表</strong>确定转发端口 → 再交回链路层加上新的帧首尾部 → 物理层加前导码、变换为信号发往下一段链路。路由器<strong>没有运输层和应用层</strong>，因为它的任务只是转发。

<strong>服务器的处理</strong>：逐层解封（去前导码 → 去帧首尾 → 去 IP 首 → 去 TCP 首），交出 HTTP 请求报文；解析后构造 HTTP 响应报文，再逐层封装、经路由器转发回主机。

<figure class="cpd-figure not-content" id="cn-w1-fig-9">
  <svg viewBox="0 0 720 278" role="img" aria-label="主机、交换机、路由器与服务器在跨网络通信中的协议栈穿透层次对比">
    <defs>
      <marker id="cn-w1-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="246" rx="8"/>
    <line class="fig-line-dashed" x1="158" y1="68" x2="562" y2="68" marker-end="url(#cn-w1-arr-9)"/>
    <line class="fig-line-dashed" x1="158" y1="100" x2="562" y2="100" marker-end="url(#cn-w1-arr-9)"/>
    <line class="fig-line-accent" x1="96" y1="212" x2="96" y2="236"/>
    <line class="fig-line-accent" x1="96" y1="236" x2="246" y2="236"/>
    <line class="fig-line-accent" x1="246" y1="236" x2="246" y2="212" marker-end="url(#cn-w1-arr-9)"/>
    <line class="fig-line-accent" x1="274" y1="212" x2="274" y2="236"/>
    <line class="fig-line-accent" x1="274" y1="236" x2="416" y2="236"/>
    <line class="fig-line-accent" x1="416" y1="236" x2="416" y2="212" marker-end="url(#cn-w1-arr-9)"/>
    <line class="fig-line-accent" x1="454" y1="212" x2="454" y2="236"/>
    <line class="fig-line-accent" x1="454" y1="236" x2="624" y2="236"/>
    <line class="fig-line-accent" x1="624" y1="236" x2="624" y2="212" marker-end="url(#cn-w1-arr-9)"/>
    <rect class="fig-box-purple" x="34" y="54" width="124" height="26" rx="3"/>
    <rect class="fig-box-blue" x="34" y="86" width="124" height="26" rx="3"/>
    <rect class="fig-box-orange" x="34" y="118" width="124" height="26" rx="3"/>
    <rect class="fig-box-green" x="34" y="150" width="124" height="26" rx="3"/>
    <rect class="fig-box" x="34" y="182" width="124" height="26" rx="3"/>
    <rect class="fig-box-green" x="200" y="150" width="120" height="26" rx="3"/>
    <rect class="fig-box" x="200" y="182" width="120" height="26" rx="3"/>
    <rect class="fig-box-orange" x="364" y="118" width="142" height="26" rx="3"/>
    <rect class="fig-box-green" x="364" y="150" width="142" height="26" rx="3"/>
    <rect class="fig-box" x="364" y="182" width="142" height="26" rx="3"/>
    <rect class="fig-box-purple" x="562" y="54" width="124" height="26" rx="3"/>
    <rect class="fig-box-blue" x="562" y="86" width="124" height="26" rx="3"/>
    <rect class="fig-box-orange" x="562" y="118" width="124" height="26" rx="3"/>
    <rect class="fig-box-green" x="562" y="150" width="124" height="26" rx="3"/>
    <rect class="fig-box" x="562" y="182" width="124" height="26" rx="3"/>
    <rect class="fig-box" x="370" y="92" width="130" height="18" rx="3"/>
    <text class="fig-title" x="96" y="40" font-size="12" text-anchor="middle">主机 H1（网络 N1）</text>
    <text class="fig-title" x="260" y="138" font-size="11" text-anchor="middle">交换机（仅到 2 层）</text>
    <text class="fig-title" x="435" y="106" font-size="11" text-anchor="middle">路由器（仅到 3 层 ★）</text>
    <text class="fig-title" x="624" y="40" font-size="12" text-anchor="middle">Web 服务器（网络 N2）</text>
    <text class="fig-muted" x="360" y="62" font-size="10" text-anchor="middle">HTTP 逻辑通信（端到端进程）</text>
    <text class="fig-purple" x="96" y="71" font-size="11" text-anchor="middle">5 应用层 (HTTP)</text>
    <text class="fig-blue" x="96" y="103" font-size="11" text-anchor="middle">4 运输层 (TCP)</text>
    <text class="fig-orange" x="96" y="135" font-size="11" text-anchor="middle">3 网络层 (IP)</text>
    <text class="fig-green" x="96" y="167" font-size="11" text-anchor="middle">2 数据链路层</text>
    <text class="fig-label" x="96" y="199" font-size="11" text-anchor="middle">1 物理层</text>
    <text class="fig-green" x="260" y="167" font-size="10" text-anchor="middle">查 MAC 表转发帧</text>
    <text class="fig-label" x="260" y="199" font-size="10" text-anchor="middle">物理层</text>
    <text class="fig-orange" x="435" y="135" font-size="10" text-anchor="middle">查 IP 路由表转发</text>
    <text class="fig-green" x="435" y="167" font-size="10" text-anchor="middle">剥旧帧 → 换新帧首尾</text>
    <text class="fig-label" x="435" y="199" font-size="10" text-anchor="middle">物理层</text>
    <text class="fig-purple" x="624" y="71" font-size="11" text-anchor="middle">5 应用层 (HTTP)</text>
    <text class="fig-blue" x="624" y="103" font-size="11" text-anchor="middle">4 运输层 (TCP)</text>
    <text class="fig-orange" x="624" y="135" font-size="11" text-anchor="middle">3 网络层 (IP)</text>
    <text class="fig-green" x="624" y="167" font-size="11" text-anchor="middle">2 数据链路层</text>
    <text class="fig-label" x="624" y="199" font-size="11" text-anchor="middle">1 物理层</text>
    <text class="fig-accent" x="360" y="253" font-size="10" text-anchor="middle">实际物理比特流路径：H1 封装下沉 → 交换机二层转发 → 路由器剥旧帧、查路由表、换新 MAC 帧 → 服务器逐层解封上交</text>
  </svg>
  <figcaption>图 1-9　跨网络 HTTP 访问的协议栈穿透流程：交换机最高到数据链路层，路由器最高到网络层（无运输层与应用层）</figcaption>
</figure>

### 1.8 体系结构专用术语

术语来源于 OSI 七层体系结构，也适用于 TCP/IP 四层和五层原理体系结构。三个最具代表性的分类名称是<strong>实体、协议、服务</strong>。

<strong>实体与对等实体</strong>：实体是任何可发送或接收信息的<strong>硬件或软件进程</strong>；对等实体是通信双方<strong>相同层次</strong>中的实体。按此定义，通信双方的网卡是对等实体，双方正在通信的应用进程也是对等实体。

<strong>协议</strong>：控制两个对等实体进行<strong>逻辑通信</strong>的规则集合。“逻辑”是因为这种通信并不真实存在，只是为便于单独研究某一层面假设出来的。协议三要素：

| 要素 | 含义 | 例子 |
|---|---|---|
| <strong>语法</strong> | 定义所交换信息的<strong>格式</strong>：有哪些字段、按什么顺序构成 | IP 数据报格式中每个小格是一个字段，格中数字表示字段长度（单位 bit） |
| <strong>语义</strong> | 定义通信双方收到分组后要完成的<strong>操作</strong> | 主机发 HTTP GET 请求，服务器解析后查找内容并回送 HTTP 响应 |
| <strong>同步</strong> | 定义通信双方的<strong>时序关系</strong>（不是时钟频率同步） | TCP 三报文握手建立连接的过程与状态转换 |

<strong>服务</strong>：在协议控制下，两个对等实体间的逻辑通信使本层能向上一层提供服务；要实现本层协议，又需要使用下面一层提供的服务。

> <strong>★考点</strong>：<strong>协议是水平的（同层对等实体之间），服务是垂直的</strong>（下层向上层提供）。实体看得见下层提供的服务，却不知道实现该服务的具体协议——即<strong>下层的协议对上层的实体是透明的</strong>。

<strong>服务访问点 SAP</strong>：同一系统中相邻两层实体交换信息的<strong>逻辑接口</strong>，用于区分不同的服务类型。

| 层 | SAP |
|---|---|
| 数据链路层 | 帧的<strong>类型</strong>字段 |
| 网络层 | IP 数据报首部中的<strong>协议</strong>字段 |
| 运输层 | <strong>端口号</strong> |

<strong>服务原语</strong>：上层使用下层所提供的服务时，必须与下层交换的一些命令。

<strong>PDU 与 SDU</strong>：

| 层 | 协议数据单元 PDU 的名称 |
|---|---|
| 应用层 | 应用报文 |
| 运输层 | TCP 报文段 / UDP 用户数据报 |
| 网络层 | 分组（使用 IP 协议时称 IP 数据报） |
| 数据链路层 | 帧 |
| 物理层 | 比特流 |

SDU（服务数据单元）是同一系统内<strong>层与层之间</strong>交换的数据包。多个 SDU 可合成为一个 PDU，一个 SDU 也可划分为几个 PDU。

<figure class="cpd-figure not-content" id="cn-w1-fig-10">
  <svg viewBox="0 0 720 276" role="img" aria-label="对等实体水平协议与相邻层垂直服务、SAP及PDU与SDU关系示意图">
    <defs>
      <marker id="cn-w1-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="388" height="244" rx="8"/>
    <rect class="fig-box-subtle" x="418" y="16" width="286" height="244" rx="8"/>
    <line class="fig-line-dashed" x1="146" y1="78" x2="274" y2="78" marker-start="url(#cn-w1-arr-10)" marker-end="url(#cn-w1-arr-10)"/>
    <line class="fig-line-dashed" x1="146" y1="184" x2="274" y2="184" marker-start="url(#cn-w1-arr-10)" marker-end="url(#cn-w1-arr-10)"/>
    <line class="fig-line-accent" x1="90" y1="100" x2="90" y2="160" marker-start="url(#cn-w1-arr-10)" marker-end="url(#cn-w1-arr-10)"/>
    <line class="fig-line-accent" x1="330" y1="100" x2="330" y2="160" marker-start="url(#cn-w1-arr-10)" marker-end="url(#cn-w1-arr-10)"/>
    <line class="fig-line" x1="34" y1="130" x2="386" y2="130"/>
    <line class="fig-line-dashed" x1="516" y1="88" x2="516" y2="148" marker-end="url(#cn-w1-arr-10)"/>
    <line class="fig-line-dashed" x1="674" y1="88" x2="674" y2="148" marker-end="url(#cn-w1-arr-10)"/>
    <rect class="fig-box-purple" x="34" y="56" width="112" height="44" rx="5"/>
    <rect class="fig-box-purple" x="274" y="56" width="112" height="44" rx="5"/>
    <rect class="fig-box-orange" x="62" y="118" width="56" height="24" rx="4"/>
    <rect class="fig-box-orange" x="302" y="118" width="56" height="24" rx="4"/>
    <rect class="fig-box-blue" x="34" y="162" width="112" height="44" rx="5"/>
    <rect class="fig-box-blue" x="274" y="162" width="112" height="44" rx="5"/>
    <rect class="fig-box-purple" x="516" y="56" width="158" height="32" rx="4"/>
    <rect class="fig-box-orange" x="444" y="150" width="72" height="34" rx="4"/>
    <rect class="fig-box-blue" x="516" y="150" width="158" height="34" rx="4"/>
    <rect class="fig-box-dashed" x="438" y="144" width="242" height="46" rx="6"/>
    <text class="fig-title" x="210" y="38" font-size="12" text-anchor="middle">① 协议（水平）vs 服务（垂直）</text>
    <text class="fig-purple" x="90" y="76" font-size="11" text-anchor="middle">系统 A</text>
    <text class="fig-muted" x="90" y="92" font-size="10" text-anchor="middle">第 n+1 层实体</text>
    <text class="fig-purple" x="330" y="76" font-size="11" text-anchor="middle">系统 B</text>
    <text class="fig-muted" x="330" y="92" font-size="10" text-anchor="middle">第 n+1 层实体</text>
    <text class="fig-label" x="210" y="72" font-size="10" text-anchor="middle">第 n+1 层协议</text>
    <text class="fig-muted" x="210" y="92" font-size="9" text-anchor="middle">（对等实体逻辑通信）</text>
    <text class="fig-orange" x="90" y="134" font-size="10" text-anchor="middle">SAP</text>
    <text class="fig-orange" x="330" y="134" font-size="10" text-anchor="middle">SAP</text>
    <text class="fig-accent" x="210" y="124" font-size="10" text-anchor="middle">垂直交换服务原语</text>
    <text class="fig-muted" x="210" y="145" font-size="9" text-anchor="middle">下层向上层提供服务（协议透明）</text>
    <text class="fig-blue" x="90" y="182" font-size="11" text-anchor="middle">系统 A</text>
    <text class="fig-muted" x="90" y="198" font-size="10" text-anchor="middle">第 n 层实体</text>
    <text class="fig-blue" x="330" y="182" font-size="11" text-anchor="middle">系统 B</text>
    <text class="fig-muted" x="330" y="198" font-size="10" text-anchor="middle">第 n 层实体</text>
    <text class="fig-label" x="210" y="178" font-size="10" text-anchor="middle">第 n 层协议</text>
    <text class="fig-muted" x="210" y="198" font-size="9" text-anchor="middle">（语法 · 语义 · 同步）</text>
    <text class="fig-muted" x="210" y="232" font-size="10" text-anchor="middle">SAP 实例：运输层端口 / IP 协议字段 / 帧类型</text>
    <text class="fig-title" x="561" y="38" font-size="12" text-anchor="middle">② SDU + PCI = PDU 封装</text>
    <text class="fig-purple" x="595" y="76" font-size="11" text-anchor="middle">(n+1)-PDU</text>
    <text class="fig-muted" x="595" y="118" font-size="10" text-anchor="middle">经 SAP 向下交付</text>
    <text class="fig-orange" x="480" y="171" font-size="11" text-anchor="middle">n-PCI</text>
    <text class="fig-blue" x="595" y="171" font-size="11" text-anchor="middle">n-SDU</text>
    <text class="fig-accent" x="561" y="212" font-size="11" text-anchor="middle">组装为第 n 层 n-PDU</text>
    <text class="fig-muted" x="561" y="232" font-size="10" text-anchor="middle">PCI 为本层首部（及尾部）控制信息</text>
  </svg>
  <figcaption>图 1-10　体系结构核心术语关系：协议控制水平对等实体逻辑通信，下层经 SAP 向上层垂直提供服务，n-PCI + n-SDU = n-PDU</figcaption>
</figure>

### 1.9 时延的构成与典型计算

时延由<strong>发送时延、传播时延、处理时延、排队时延</strong>四部分构成，其中处理时延一般不方便计算。

$$
T_{\mathrm{total}} = t_s + t_p + t_{proc} + t_{queue}
$$

<strong>发送时延</strong>（传输时延）：主机或路由器把分组的所有比特推向链路所需的时间。

$$
t_s = \frac{L}{R}
$$

其中 $L$ 为分组长度（bit），$R$ 为发送速率（bit/s）。<strong>发送速率应取网卡速率、信道带宽、对端接口速率三者中的最小者</strong>。

<strong>传播时延</strong>：电磁波在信道中传播一定距离所需的时间。

$$
t_p = \frac{D}{v}
$$

其中 $D$ 为信道长度（m），$v$ 为电磁波在介质中的传播速率。不同介质略有差异：铜缆约 $2.3\times10^{8}$ m/s，光纤约 $2.0\times10^{8}$ m/s，做题建议记 $2\times10^{8}$ m/s。

<figure class="cpd-figure not-content" id="cn-w1-fig-11">
  <svg viewBox="0 0 720 330" role="img" aria-label="四种时延的发生位置与单链路发送时延传播时延时空图">
    <defs>
      <marker id="cn-w1-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="96" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="124" width="688" height="190" rx="8"/>
    <line class="fig-line-accent" x1="142" y1="68" x2="366" y2="68" marker-end="url(#cn-w1-arr-11)"/>
    <line class="fig-line-accent" x1="538" y1="68" x2="584" y2="68" marker-end="url(#cn-w1-arr-11)"/>
    <line class="fig-line" x1="88" y1="162" x2="88" y2="282"/>
    <line class="fig-line" x1="596" y1="162" x2="596" y2="282"/>
    <line class="fig-line" x1="56" y1="274" x2="664" y2="274" marker-end="url(#cn-w1-arr-11)"/>
    <polygon class="fig-box-blue" points="88,180 218,180 518,274 388,274"/>
    <line class="fig-line-accent" x1="88" y1="168" x2="218" y2="168" marker-start="url(#cn-w1-arr-11)" marker-end="url(#cn-w1-arr-11)"/>
    <line class="fig-line-accent" x1="88" y1="290" x2="388" y2="290" marker-start="url(#cn-w1-arr-11)" marker-end="url(#cn-w1-arr-11)"/>
    <rect class="fig-box-blue" x="30" y="48" width="112" height="40" rx="5"/>
    <rect class="fig-box-orange" x="368" y="48" width="90" height="40" rx="5"/>
    <rect class="fig-box-purple" x="458" y="48" width="80" height="40" rx="5"/>
    <rect class="fig-box-green" x="586" y="48" width="98" height="40" rx="5"/>
    <text class="fig-title" x="30" y="36" font-size="12">① 四种时延在传输路径上的发生位置：总时延 = 发送 + 传播 + 排队 + 处理</text>
    <text class="fig-blue" x="86" y="65" font-size="11" text-anchor="middle">发送端网卡</text>
    <text class="fig-muted" x="86" y="80" font-size="10" text-anchor="middle">发送时延 L/R</text>
    <text class="fig-accent" x="254" y="61" font-size="11" text-anchor="middle">物理传输媒体 D</text>
    <text class="fig-muted" x="254" y="84" font-size="10" text-anchor="middle">传播时延 D/v（约 2×10⁸ m/s）</text>
    <text class="fig-orange" x="413" y="65" font-size="11" text-anchor="middle">路由器队列</text>
    <text class="fig-muted" x="413" y="80" font-size="10" text-anchor="middle">排队时延</text>
    <text class="fig-purple" x="498" y="65" font-size="11" text-anchor="middle">查表检错</text>
    <text class="fig-muted" x="498" y="80" font-size="10" text-anchor="middle">处理时延</text>
    <text class="fig-green" x="635" y="65" font-size="11" text-anchor="middle">下一跳链路</text>
    <text class="fig-muted" x="635" y="80" font-size="10" text-anchor="middle">继续转发</text>
    <text class="fig-title" x="30" y="146" font-size="12">② 单段链路发送时延与传播时延时空几何关系</text>
    <text class="fig-label" x="80" y="170" font-size="10" text-anchor="end">发送端 (x=0)</text>
    <text class="fig-label" x="604" y="170" font-size="10" text-anchor="start">接收端 (x=D)</text>
    <text class="fig-blue" x="153" y="162" font-size="11" text-anchor="middle">发送时延 ts = L / R</text>
    <text class="fig-label" x="303" y="232" font-size="11" text-anchor="middle">L 比特分组在途传播带</text>
    <text class="fig-accent" x="238" y="305" font-size="11" text-anchor="middle">首比特传播时延 tp = D / v</text>
    <text class="fig-green" x="518" y="266" font-size="10" text-anchor="middle">尾比特收完 t = ts + tp</text>
    <text class="fig-muted" x="54" y="224" font-size="10" text-anchor="middle">距离</text>
    <text class="fig-label" x="652" y="266" font-size="11">时间 t</text>
  </svg>
  <figcaption>图 1-11　四种时延的发生部位与时空几何模型：增加分组长度 L 仅展宽横向发送时延 L/R，斜边水平投影传播时延 D/v 不变</figcaption>
</figure>

<strong>时延带宽积</strong> = 传播时延 × 带宽，表示链路中正在传播的比特数，又称“以比特为单位的链路长度”。

<figure class="cpd-figure not-content" id="cn-w1-fig-12">
  <svg viewBox="0 0 700 206" role="img" aria-label="时延带宽积比特管道模型示意图">
    <defs>
      <marker id="cn-w1-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="174" rx="8"/>
    <line class="fig-line-accent" x1="126" y1="52" x2="574" y2="52" marker-start="url(#cn-w1-arr-12)" marker-end="url(#cn-w1-arr-12)"/>
    <line class="fig-line-accent" x1="596" y1="68" x2="596" y2="132" marker-start="url(#cn-w1-arr-12)" marker-end="url(#cn-w1-arr-12)"/>
    <rect class="fig-box-blue" x="34" y="74" width="78" height="52" rx="6"/>
    <rect class="fig-box" x="126" y="68" width="448" height="64" rx="14"/>
    <rect class="fig-box-green" x="608" y="74" width="58" height="52" rx="6"/>
    <rect class="fig-box-accent" x="144" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="184" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="224" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="264" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="304" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="344" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="384" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="424" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="464" y="84" width="32" height="32" rx="4"/>
    <rect class="fig-box-accent" x="504" y="84" width="32" height="32" rx="4"/>
    <text class="fig-accent" x="350" y="44" font-size="11" text-anchor="middle">管道长度 = 传播时延 tp（单位：s）</text>
    <text class="fig-blue" x="73" y="97" font-size="11" text-anchor="middle">发送端</text>
    <text class="fig-muted" x="73" y="113" font-size="10" text-anchor="middle">持续注入</text>
    <text class="fig-accent" x="160" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-accent" x="200" y="104" font-size="11" text-anchor="middle">0</text>
    <text class="fig-accent" x="240" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-accent" x="280" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-accent" x="320" y="104" font-size="11" text-anchor="middle">0</text>
    <text class="fig-accent" x="360" y="104" font-size="11" text-anchor="middle">0</text>
    <text class="fig-accent" x="400" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-accent" x="440" y="104" font-size="11" text-anchor="middle">0</text>
    <text class="fig-accent" x="480" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-accent" x="520" y="104" font-size="11" text-anchor="middle">1</text>
    <text class="fig-muted" x="585" y="150" font-size="10" text-anchor="middle">截面积 = 带宽 B</text>
    <text class="fig-green" x="637" y="97" font-size="11" text-anchor="middle">接收端</text>
    <text class="fig-muted" x="637" y="113" font-size="10" text-anchor="middle">首比特刚到</text>
    <text class="fig-label" x="330" y="156" font-size="11" text-anchor="middle">管道容积 = 时延带宽积 = 传播时延 tp × 带宽 B（以比特为单位的链路长度）</text>
    <text class="fig-muted" x="330" y="175" font-size="10" text-anchor="middle">含义：发送端连续发送且首比特刚抵达接收端时，链路上正在传播的全部“在途比特”总数</text>
  </svg>
  <figcaption>图 1-12　时延带宽积管道模型：链路如同横截面为带宽 B、长度为传播时延 tp 的管道，容积即最大在途比特数</figcaption>
</figure>

<strong>往返时间 RTT</strong>：通信双方交互一次所耗费的时间。忽略处理时延时，链路上的 RTT 约为 $2t_p$。

<strong>例题 1（发送时延与传播时延的量级）</strong>

链路长度 60 m，带宽 10 Mbit/s，传播速率 $2\times10^{8}$ m/s，主机发送 <strong>1 bit</strong>，另一台主机接收完需要多久？

- 发送时延：$\frac{1}{10\times10^{6}} = 10^{-7}$ s，此时信号前端已传出 $2\times10^{8}\times10^{-7}=20$ m；
- 传播时延：$\frac{60}{2\times10^{8}} = 3\times10^{-7}$ s；
- 总时间：$4\times10^{-7}$ s。

| 链路长度 | 发送时延 | 传播时延 | 接收完 1 bit 总时间 |
|---|---|---|---|
| 60 m | $10^{-7}$ s | $3\times10^{-7}$ s | $4\times10^{-7}$ s |
| 20 m | $10^{-7}$ s | $10^{-7}$ s | $2\times10^{-7}$ s |
| 10 m | $10^{-7}$ s | $0.5\times10^{-7}$ s | $1.5\times10^{-7}$ s |

> <strong>★考点</strong>：若连续发送 <strong>n 个比特</strong>，总时间 = $n\times10^{-7} + 3\times10^{-7}$ s，而<strong>不是</strong> $n\times4\times10^{-7}$ s。比特是一个接一个向前传播的，增加比特数只增大<strong>发送时延</strong>，传播时延不变。

<strong>例题 2（n 个分组、m 段链路）</strong>

设各分组等长、各链路长度相同、带宽相同，且忽略路由器的处理时延，则总时延为：

$$
T = n\frac{p}{b} + (m-1)\frac{p}{b} + m d
$$

其中 $n$ 个分组的发送时延为 $n\frac{p}{b}$，$m$ 段链路共有 $m-1$ 次转发，每次转发耗时 $\frac{p}{b}$，$m$ 段链路的传播时延为 $m d$。

> <strong>注意</strong>：两个分组在三段链路上的总时延<strong>不是</strong>一个分组的三倍——主机发送第二个分组的同时，第一个路由器已经在转发第一个分组，流水线掩盖了部分时延。该结论的三个前提（等长分组、等长链路、忽略处理时延）缺一不可。

<figure class="cpd-figure not-content" id="cn-w1-fig-13">
  <svg viewBox="0 0 720 278" role="img" aria-label="n个等长分组跨越m段链路存储转发流水线总时延拆解图">
    <defs>
      <marker id="cn-w1-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="246" rx="8"/>
    <line class="fig-line" x1="88" y1="54" x2="660" y2="54" marker-end="url(#cn-w1-arr-13)"/>
    <line class="fig-line" x1="88" y1="100" x2="660" y2="100"/>
    <line class="fig-line" x1="88" y1="146" x2="660" y2="146"/>
    <line class="fig-line" x1="88" y1="192" x2="660" y2="192"/>
    <polygon class="fig-box-blue" points="104,54 174,54 234,100 164,100"/>
    <polygon class="fig-box-blue" points="174,54 244,54 304,100 234,100"/>
    <polygon class="fig-box-accent" points="244,54 314,54 374,100 304,100"/>
    <polygon class="fig-box-blue" points="234,100 304,100 364,146 294,146"/>
    <polygon class="fig-box-blue" points="304,100 374,100 434,146 364,146"/>
    <polygon class="fig-box-orange" points="374,100 444,100 504,146 434,146"/>
    <polygon class="fig-box-blue" points="364,146 434,146 494,192 424,192"/>
    <polygon class="fig-box-blue" points="434,146 504,146 564,192 494,192"/>
    <polygon class="fig-box-green" points="504,146 574,146 634,192 564,192"/>
    <line class="fig-line-dashed" x1="104" y1="54" x2="104" y2="232"/>
    <line class="fig-line-dashed" x1="314" y1="54" x2="314" y2="232"/>
    <line class="fig-line-dashed" x1="454" y1="146" x2="454" y2="232"/>
    <line class="fig-line-dashed" x1="634" y1="192" x2="634" y2="232"/>
    <line class="fig-line-accent" x1="104" y1="224" x2="314" y2="224" marker-start="url(#cn-w1-arr-13)" marker-end="url(#cn-w1-arr-13)"/>
    <line class="fig-line-accent" x1="314" y1="224" x2="454" y2="224" marker-start="url(#cn-w1-arr-13)" marker-end="url(#cn-w1-arr-13)"/>
    <line class="fig-line-accent" x1="454" y1="224" x2="634" y2="224" marker-start="url(#cn-w1-arr-13)" marker-end="url(#cn-w1-arr-13)"/>
    <text class="fig-title" x="30" y="36" font-size="12">n=3 个等长分组跨越 m=3 段链路（H1 → R1 → R2 → H2）流水线总时延拆解</text>
    <text class="fig-label" x="76" y="58" font-size="11" text-anchor="end">源 H1</text>
    <text class="fig-label" x="76" y="104" font-size="11" text-anchor="end">路由 R1</text>
    <text class="fig-label" x="76" y="150" font-size="11" text-anchor="end">路由 R2</text>
    <text class="fig-label" x="76" y="196" font-size="11" text-anchor="end">目的 H2</text>
    <text class="fig-label" x="676" y="58" font-size="10">时间 t</text>
    <text class="fig-blue" x="150" y="74" font-size="10" text-anchor="middle">P1</text>
    <text class="fig-blue" x="220" y="74" font-size="10" text-anchor="middle">P2</text>
    <text class="fig-accent" x="290" y="74" font-size="10" text-anchor="middle">P3 (末)</text>
    <text class="fig-orange" x="424" y="122" font-size="10" text-anchor="middle">R1 转 P3</text>
    <text class="fig-green" x="554" y="168" font-size="10" text-anchor="middle">R2 转 P3</text>
    <text class="fig-accent" x="209" y="244" font-size="11" text-anchor="middle">① 主机发完 n 包：n·(p/b)</text>
    <text class="fig-orange" x="384" y="244" font-size="11" text-anchor="middle">② 末包 (m-1) 次转发</text>
    <text class="fig-green" x="544" y="244" font-size="11" text-anchor="middle">③ m 段传播时延：m·d</text>
  </svg>
  <figcaption>图 1-13　多分组多跳流水线时延公式几何证明：紧盯“源主机连续发完 n 包 + 最后一个分组经历 (m-1) 次转发与 m 段传播”</figcaption>
</figure>

<strong>例题 3（电路交换与分组交换的比较）</strong>

报文共 $x$ bit，源到目的共 $k$ 段链路，每段传播时延 $d$，带宽 $b$，电路建立时间 $s$；分组长度为 $p$ bit，首部和排队时间忽略。电路交换时延为 $s+\frac{x}{b}+kd$，分组交换时延为 $\frac{x}{b}+(k-1)\frac{p}{b}+kd$，令前者大于后者：

$$
s + \frac{x}{b} + kd > \frac{x}{b} + (k-1)\frac{p}{b} + kd \;\Longrightarrow\; s > (k-1)\frac{p}{b}
$$

即<strong>电路建立时间大于 $(k-1)$ 个分组的发送时延</strong>时，分组交换更快。

<strong>例题 4（最优分组长度）</strong>

报文长度 $x$，分组数据部分 $p$，首部 $h$（固定），共 $k$ 段链路，带宽 $b$，忽略传播时延与排队时间。总时间

$$
D(p) = \frac{x}{p}\cdot\frac{p+h}{b} + (k-1)\frac{p+h}{b}
$$

对 $p$ 求导并令其为零：

$$
\frac{dD}{dp} = -\frac{xh}{bp^{2}} + \frac{k-1}{b} = 0 \;\Longrightarrow\; p = \sqrt{\frac{xh}{k-1}}
$$

> <strong>★考点</strong>：真题结论——2013 年统考中，报文交换总时延 1600 ms、分组交换总时延 801 ms，选 <strong>D</strong>；2010 年第 34 题答案为 <strong>C</strong>。做时延题务必先画时间-链路示意图，把“发送”与“传播”分开看。

### 1.10 本章小结

本章共六节，前五节是概念与指标，第六节体系结构是<strong>重点和难点</strong>。

| 小节 | 核心内容 | 要求 |
|---|---|---|
| 作用 | 通信基础设施 → 信息服务基础设施；CNNIC 数据 | 理解 |
| 因特网概述 | 网络/互连网/因特网概念、三阶段、标准化、ISP、边缘与核心 | 第一部分理解，其余了解 |
| 三种交换 | 电路、报文、分组；分组与存储转发是重点 | 理解区别 |
| 定义与分类 | 定义、分类角度 | 能自行给出定义、列举分类 |
| 性能指标 | 八个指标，重点是时延与利用率 | 重点掌握 |
| 体系结构 | 三种体系结构、分层必要性、分层举例、专用术语 | 重点掌握 |

<strong>定义与分类</strong>（小结中的补充）：最简单的定义是“一些<strong>互联的、自治的</strong>计算机的集合”，不同阶段有不同定义，反映当时的技术水平。分类角度：

| 角度 | 类型 |
|---|---|
| 交换技术 | 电路交换网、分组交换网、报文交换网 |
| 使用者 | 公用网、专用网 |
| 传输介质 | 有线网、无线网 |
| 覆盖范围 | 广域网 WAN、城域网 MAN、局域网 LAN、个域网 PAN |
| 拓扑结构 | 总线型、星型、环形、网状型 |

<strong>八个性能指标</strong>：

| 指标 | 含义 |
|---|---|
| 速率 | 每秒传输的比特数，也称比特率、数据率 |
| 带宽 | 通信线路传送数据的能力，即最高数据率 |
| 吞吐量 | 单位时间内通过某个网络或信道接口的数据量 |
| 时延 | 发送时延 + 传播时延 + 处理时延（+ 排队时延） |
| 时延带宽积 | 传播时延 × 带宽，即“以比特为单位的链路长度” |
| 往返时间 RTT | 通信双方交互一次所耗费的时间 |
| 利用率 | 信道利用率（有数据通过的时间比例）与网络利用率（各信道利用率的加权平均） |
| 丢包率 | 丢失分组数与总分组数之比；丢包源于误码被丢弃或队列已满被丢弃 |

> <strong>注意</strong>：根据排队论，信道利用率增大时，该信道引起的时延也迅速增大，因此利用率<strong>并非越高越好</strong>，但太低又浪费通信资源。

<figure class="cpd-figure not-content" id="cn-w1-fig-14">
  <svg viewBox="0 0 720 256" role="img" aria-label="瓶颈链路吞吐量模型与排队论信道利用率时延双曲线关系">
    <defs>
      <marker id="cn-w1-arr-14" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="332" height="224" rx="8"/>
    <rect class="fig-box-subtle" x="364" y="16" width="340" height="224" rx="8"/>
    <rect class="fig-box-blue" x="92" y="90" width="80" height="34" rx="4"/>
    <rect class="fig-box-red" x="192" y="99" width="76" height="16" rx="3"/>
    <rect class="fig-box-green" x="32" y="84" width="60" height="46" rx="5"/>
    <circle class="fig-box-orange" cx="182" cy="107" r="14"/>
    <rect class="fig-box-purple" x="268" y="84" width="64" height="46" rx="5"/>
    <line class="fig-line" x1="416" y1="204" x2="674" y2="204" marker-end="url(#cn-w1-arr-14)"/>
    <line class="fig-line" x1="416" y1="204" x2="416" y2="54" marker-end="url(#cn-w1-arr-14)"/>
    <line class="fig-line-dashed" x1="636" y1="58" x2="636" y2="204"/>
    <line class="fig-line-dashed" x1="416" y1="150" x2="526" y2="150"/>
    <line class="fig-line-dashed" x1="526" y1="150" x2="526" y2="204"/>
    <path class="fig-line-accent" d="M 416 177 Q 492 174 526 150 Q 596 105 628 62" fill="none"/>
    <circle class="fig-dot" cx="416" cy="177" r="3.5"/>
    <circle class="fig-dot" cx="526" cy="150" r="3.5"/>
    <text class="fig-title" x="182" y="38" font-size="12" text-anchor="middle">① 带宽（上限）vs 吞吐量（瓶颈受限）</text>
    <text class="fig-green" x="62" y="105" font-size="10" text-anchor="middle">服务器</text>
    <text class="fig-muted" x="62" y="119" font-size="9" text-anchor="middle">发送端</text>
    <text class="fig-blue" x="132" y="82" font-size="10" text-anchor="middle">带宽 100 Mb/s</text>
    <text class="fig-orange" x="182" y="111" font-size="10" text-anchor="middle">R</text>
    <text class="fig-red" x="230" y="88" font-size="10" text-anchor="middle">瓶颈 10 Mb/s</text>
    <text class="fig-purple" x="300" y="105" font-size="10" text-anchor="middle">客户机</text>
    <text class="fig-muted" x="300" y="119" font-size="9" text-anchor="middle">接收端</text>
    <text class="fig-label" x="182" y="162" font-size="11" text-anchor="middle">端到端实际吞吐量 ≤ min(100, 10) = 10 Mb/s</text>
    <text class="fig-muted" x="182" y="184" font-size="10" text-anchor="middle">带宽是理想最高速率；吞吐量是实测速率</text>
    <text class="fig-muted" x="182" y="204" font-size="10" text-anchor="middle">多用户共享瓶颈链路时吞吐量还会进一步平分</text>
    <text class="fig-title" x="534" y="38" font-size="12" text-anchor="middle">② 时延与信道利用率关系 D = D₀ / (1 - U)</text>
    <text class="fig-label" x="408" y="62" font-size="10" text-anchor="end">时延 D</text>
    <text class="fig-muted" x="408" y="181" font-size="10" text-anchor="end">D₀</text>
    <text class="fig-muted" x="408" y="154" font-size="10" text-anchor="end">2D₀</text>
    <text class="fig-muted" x="416" y="220" font-size="10" text-anchor="middle">0</text>
    <text class="fig-accent" x="526" y="220" font-size="10" text-anchor="middle">0.5</text>
    <text class="fig-red" x="636" y="220" font-size="10" text-anchor="middle">U = 1</text>
    <text class="fig-label" x="674" y="220" font-size="10" text-anchor="middle">利用率</text>
    <text class="fig-red" x="536" y="76" font-size="10" text-anchor="middle">U→1 时延急剧趋向 ∞</text>
    <text class="fig-muted" x="534" y="168" font-size="10">U=0.5 时 D=2D₀</text>
  </svg>
  <figcaption>图 1-14　性能指标核心规律：端到端吞吐量取决于瓶颈链路带宽；信道利用率 U 趋近 1 时排队时延呈双曲线暴增甚至引发丢包</figcaption>
</figure>

<strong>三种体系结构</strong>：

| 体系结构 | 层次（自下而上） | 地位 |
|---|---|---|
| OSI | 物理层、数据链路层、网络层、运输层、会话层、表示层、应用层 | <strong>法律上的</strong>国际标准 |
| TCP/IP | 网络接口层、网际层、运输层、应用层 | <strong>事实上的</strong>国际标准 |
| 五层原理 | 物理层、数据链路层、网络层、运输层、应用层 | 适用于教学 |

五层职责一句话：物理层解决<strong>用什么信号传比特</strong>；数据链路层解决<strong>分组在一个网络/一段链路上传输</strong>；网络层解决<strong>分组在多个网络间传输（路由）</strong>；运输层解决<strong>进程之间基于网络的通信</strong>；应用层解决<strong>应用进程交互完成特定应用</strong>。

体系结构的四部分内容：常见体系结构、分层的必要性、分层思想举例、专用术语（实体/协议/服务，PDU/SDU，SAP，服务原语）。协议是水平的、服务是垂直的，下层协议对上层实体透明——这三句话是理解整个体系结构的钥匙。

## 第2章 物理层

物理层要解决的核心问题只有一个：<strong>怎样在连接各种计算机的传输媒体上传输比特 0 和 1</strong>。它向上为数据链路层提供<strong>透明</strong>的比特流传输服务——数据链路层看不见、也无需看见物理层究竟用什么方法传输比特，只享受比特流传输服务即可。

### 2.1 物理层的基本概念

<figure class="cpd-figure not-content" id="cn-w2-fig-1">
  <svg viewBox="0 0 720 324" role="img" aria-label="物理层在体系结构中的位置与传输媒体接口四大特性">
    <defs>
      <marker id="cn-w2-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="208" height="268" rx="6"/>
    <rect class="fig-box-subtle" x="236" y="16" width="468" height="268" rx="6"/>
    <line class="fig-line-accent" x1="120" y1="196" x2="120" y2="234" stroke-width="1.6" marker-start="url(#cn-w2-arr-1)" marker-end="url(#cn-w2-arr-1)"/>
    <line class="fig-line-dashed" x1="204" y1="215" x2="250" y2="215"/>
    <rect class="fig-box" x="32" y="44" width="176" height="26" rx="4"/>
    <rect class="fig-box" x="32" y="74" width="176" height="26" rx="4"/>
    <rect class="fig-box" x="32" y="104" width="176" height="26" rx="4"/>
    <rect class="fig-box" x="32" y="134" width="176" height="26" rx="4"/>
    <rect class="fig-box-blue" x="32" y="164" width="176" height="30" rx="4"/>
    <rect class="fig-box-dashed" x="32" y="236" width="176" height="34" rx="4"/>
    <rect class="fig-box-accent" x="130" y="205" width="76" height="20" rx="3"/>
    <rect class="fig-box" x="250" y="46" width="440" height="52" rx="5"/>
    <rect class="fig-box-blue" x="250" y="46" width="102" height="52" rx="5"/>
    <rect class="fig-box" x="250" y="104" width="440" height="52" rx="5"/>
    <rect class="fig-box-green" x="250" y="104" width="102" height="52" rx="5"/>
    <rect class="fig-box" x="250" y="162" width="440" height="52" rx="5"/>
    <rect class="fig-box-orange" x="250" y="162" width="102" height="52" rx="5"/>
    <rect class="fig-box" x="250" y="220" width="440" height="52" rx="5"/>
    <rect class="fig-box-purple" x="250" y="220" width="102" height="52" rx="5"/>
    <rect class="fig-box" x="16" y="290" width="688" height="22" rx="4"/>
    <text class="fig-title" x="120" y="34" font-size="12" text-anchor="middle">五层体系结构定位</text>
    <text class="fig-label" x="120" y="61" font-size="12" text-anchor="middle">⑤ 应用层 (Message)</text>
    <text class="fig-label" x="120" y="91" font-size="12" text-anchor="middle">④ 运输层 (Segment)</text>
    <text class="fig-label" x="120" y="121" font-size="12" text-anchor="middle">③ 网络层 (Datagram)</text>
    <text class="fig-label" x="120" y="151" font-size="12" text-anchor="middle">② 数据链路层 (Frame)</text>
    <text class="fig-blue" x="120" y="183" font-size="12" font-weight="bold" text-anchor="middle">① 物理层 (Bit 0/1)</text>
    <text class="fig-accent" x="168" y="219" font-size="11" text-anchor="middle">媒体接口</text>
    <text class="fig-muted" x="120" y="252" font-size="11" text-anchor="middle">传输媒体（第 0 层）</text>
    <text class="fig-muted" x="120" y="266" font-size="10" text-anchor="middle">不含在体系结构内部</text>
    <text class="fig-title" x="470" y="34" font-size="12" text-anchor="middle">物理层与传输媒体接口的四大特性（高频辨析）</text>
    <text class="fig-blue" x="301" y="69" font-size="12" font-weight="bold" text-anchor="middle">① 机械特性</text>
    <text class="fig-muted" x="301" y="86" font-size="10" text-anchor="middle">看得见摸得着</text>
    <text class="fig-label" x="364" y="68" font-size="11">接线器形状与尺寸、引脚数目与排列、固定和锁定装置</text>
    <text class="fig-muted" x="364" y="86" font-size="10">例：RJ-45 水晶头 8P8C 规格、DB-25 插座 25 针排列</text>
    <text class="fig-green" x="301" y="127" font-size="12" font-weight="bold" text-anchor="middle">② 电气特性</text>
    <text class="fig-muted" x="301" y="144" font-size="10" text-anchor="middle">电压与阻抗范围</text>
    <text class="fig-label" x="364" y="126" font-size="11">指明接口电缆各条线上出现的电压范围、负载阻抗与距离限制</text>
    <text class="fig-muted" x="364" y="144" font-size="10">例：RS-232C 规定负电平 -15V～-3V、正电平 +3V～+15V</text>
    <text class="fig-orange" x="301" y="185" font-size="12" font-weight="bold" text-anchor="middle">③ 功能特性</text>
    <text class="fig-muted" x="301" y="202" font-size="10" text-anchor="middle">某电平表示何义</text>
    <text class="fig-label" x="364" y="184" font-size="11">指明某条线上出现某一电平的电压表示何种逻辑或控制意义</text>
    <text class="fig-muted" x="364" y="202" font-size="10">例：规定数据线上 -5V 表示逻辑“1”，+5V 表示逻辑“0”</text>
    <text class="fig-purple" x="301" y="243" font-size="12" font-weight="bold" text-anchor="middle">④ 过程特性</text>
    <text class="fig-muted" x="301" y="260" font-size="10" text-anchor="middle">事件先后顺序</text>
    <text class="fig-label" x="364" y="242" font-size="11">指明对于不同功能的各种可能事件的出现顺序（工作规程时序）</text>
    <text class="fig-muted" x="364" y="260" font-size="10">例：RTS 请求发送 → CTS 允许发送 → 传输比特流 → 释放</text>
    <text class="fig-label" x="360" y="305" font-size="11" text-anchor="middle">核心职责：屏蔽传输媒体与通信手段差异，向上层提供透明比特流服务（避坑：MAC 物理地址属于数据链路层！）</text>
  </svg>
  <figcaption>图 2-1：物理层在协议栈中的位置及传输媒体接口四大特性辨析</figcaption>
</figure>

传输媒体分为两类：<strong>导引型</strong>（双绞线、同轴电缆、光纤）与<strong>非导引型</strong>（微波，例如 2.4 GHz / 5.8 GHz 频段的 Wi-Fi）。

<figure class="cpd-figure not-content" id="cn-w2-fig-2">
  <svg viewBox="0 0 720 332" role="img" aria-label="导引型与非导引型传输媒体物理结构及光传播原理对比">
    <defs>
      <marker id="cn-w2-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="300" rx="6"/>
    <rect class="fig-box" x="28" y="42" width="324" height="124" rx="5"/>
    <rect class="fig-box" x="368" y="42" width="324" height="124" rx="5"/>
    <rect class="fig-box" x="28" y="178" width="324" height="126" rx="5"/>
    <rect class="fig-box" x="368" y="178" width="324" height="126" rx="5"/>
    <rect class="fig-box-dashed" x="44" y="76" width="212" height="36" rx="4"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" d="M 54 84 C 74 84, 74 104, 94 104 C 114 104, 114 84, 134 84 C 154 84, 154 104, 174 104 C 194 104, 194 84, 214 84 C 230 84, 234 100, 246 102"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2.2" d="M 54 104 C 74 104, 74 84, 94 84 C 114 84, 114 104, 134 104 C 154 104, 154 84, 174 84 C 194 84, 194 104, 214 104 C 230 104, 234 88, 246 86"/>
    <rect class="fig-box-purple" x="262" y="76" width="76" height="36" rx="4"/>
    <rect class="fig-box-subtle" x="384" y="74" width="210" height="40" rx="4"/>
    <rect class="fig-box-dashed" x="404" y="80" width="190" height="28" rx="3"/>
    <rect class="fig-box" x="426" y="85" width="168" height="18" rx="2"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="3.5" x1="446" y1="94" x2="594" y2="94"/>
    <rect class="fig-box-blue" x="602" y="74" width="76" height="40" rx="4"/>
    <rect class="fig-box-subtle" x="44" y="206" width="216" height="36" rx="3"/>
    <line class="fig-divider" x1="44" y1="213" x2="260" y2="213"/>
    <line class="fig-divider" x1="44" y1="235" x2="260" y2="235"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" points="48,224 82,214 122,234 162,214 202,234 242,214 256,222" marker-end="url(#cn-w2-arr-2)"/>
    <rect class="fig-box-orange" x="266" y="206" width="74" height="36" rx="4"/>
    <rect class="fig-box-subtle" x="44" y="250" width="216" height="28" rx="3"/>
    <rect class="fig-box-accent" x="44" y="260" width="216" height="8" rx="1"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" x1="48" y1="264" x2="254" y2="264" marker-end="url(#cn-w2-arr-2)"/>
    <rect class="fig-box-green" x="266" y="250" width="74" height="28" rx="4"/>
    <rect class="fig-box-blue" x="384" y="208" width="92" height="42" rx="4"/>
    <rect class="fig-box-green" x="484" y="208" width="104" height="42" rx="4"/>
    <rect class="fig-box-purple" x="596" y="208" width="84" height="42" rx="4"/>
    <line class="fig-line" x1="384" y1="262" x2="678" y2="262" marker-end="url(#cn-w2-arr-2)"/>
    <text class="fig-title" x="360" y="33" font-size="12" text-anchor="middle">物理层下方的传输媒体分类：导引型（固体线路导引）vs 非导引型（自由空间辐射）</text>
    <text class="fig-blue" x="44" y="62" font-size="12" font-weight="bold">① 双绞线（导引型：UTP 无屏蔽 / STP 屏蔽）</text>
    <text class="fig-purple" x="300" y="92" font-size="10" text-anchor="middle">STP 铝箔</text>
    <text class="fig-purple" x="300" y="105" font-size="10" text-anchor="middle">金属屏蔽层</text>
    <text class="fig-muted" x="44" y="132" font-size="11">两根绝缘铜线规则绞合：抵消外部电磁干扰与相邻线对串音</text>
    <text class="fig-muted" x="44" y="150" font-size="10">绞合度越高抗干扰越强（Cat5e / Cat6 / Cat6A 局域网布线主力）</text>
    <text class="fig-blue" x="384" y="62" font-size="12" font-weight="bold">② 同轴电缆（导引型：基带 50Ω / 宽带 75Ω）</text>
    <text class="fig-muted" x="392" y="128" font-size="10">由外向内：护套 → 屏蔽铜网 → 绝缘层 → 内导体铜芯</text>
    <text class="fig-blue" x="640" y="91" font-size="10" text-anchor="middle">网状外导体</text>
    <text class="fig-blue" x="640" y="105" font-size="10" text-anchor="middle">抗高频干扰</text>
    <text class="fig-muted" x="392" y="148" font-size="10">75Ω 用于有线电视 HFC 模拟宽带；早期总线以太网曾用 50Ω</text>
    <text class="fig-green" x="44" y="196" font-size="12" font-weight="bold">③ 光纤（导引型：纤芯折射率 n1 &gt; 包层折射率 n2）</text>
    <text class="fig-orange" x="303" y="221" font-size="10" text-anchor="middle">多模 MMF</text>
    <text class="fig-muted" x="303" y="235" font-size="9.5" text-anchor="middle">全反射·易色散</text>
    <text class="fig-green" x="303" y="262" font-size="10" text-anchor="middle">单模 SMF</text>
    <text class="fig-muted" x="303" y="274" font-size="9.5" text-anchor="middle">激光直线·远距</text>
    <text class="fig-muted" x="44" y="294" font-size="10">多模用发光二极管（近距建筑内）；单模纤芯极细用激光器（长途干线）</text>
    <text class="fig-purple" x="384" y="196" font-size="12" font-weight="bold">④ 非导引型媒体（自由空间无线电磁波）</text>
    <text class="fig-blue" x="430" y="225" font-size="10.5" text-anchor="middle">无线电波</text>
    <text class="fig-muted" x="430" y="241" font-size="9.5" text-anchor="middle">电离层反射/绕射</text>
    <text class="fig-green" x="536" y="225" font-size="10.5" text-anchor="middle">微波 (Wi-Fi/卫星)</text>
    <text class="fig-muted" x="536" y="241" font-size="9.5" text-anchor="middle">2.4 / 5.8 GHz 视距</text>
    <text class="fig-purple" x="638" y="225" font-size="10.5" text-anchor="middle">红外 / 激光</text>
    <text class="fig-muted" x="638" y="241" font-size="9.5" text-anchor="middle">高定向·难穿墙</text>
    <text class="fig-muted" x="530" y="276" font-size="10" text-anchor="middle">频率升高 → 方向性增强、带宽增大、绕射穿障能力减弱</text>
    <text class="fig-muted" x="384" y="294" font-size="10">微波沿直线传播：地面接力站中继或同步卫星转发（传播时延大）</text>
  </svg>
  <figcaption>图 2-2：导引型媒体（双绞线、同轴电缆、多模/单模光纤）与非导引型无线频段对比</figcaption>
</figure>

物理层协议的任务，是规定与<strong>传输媒体接口</strong>有关的四个特性：

| 特性 | 规定的内容 | 题干关键词 |
|---|---|---|
| <strong>机械特性</strong> | 接线器的形状和尺寸、引脚数目和排列顺序、固定和锁定装置 | 形状、尺寸、引脚 |
| <strong>电气特性</strong> | 接口电缆各条线上出现的<strong>电压范围</strong> | 电压范围 |
| <strong>功能特性</strong> | 某条线上出现某一电平的电压<strong>表示何种意义</strong> | 某电平表示什么 |
| <strong>过程特性</strong> | 对于不同功能的各种可能<strong>事件的出现顺序</strong> | 事件顺序 |

> <strong>★考点</strong>：四个特性的辨析是选择题常客，“完成每种功能的事件发生顺序”只对应<strong>过程特性</strong>。

> <strong>注意</strong>：物理层协议种类多，因为传输媒体多、连接方式也多（点对点、广播等），所以学习重点是<strong>基本概念</strong>而非某个具体协议。

> <strong>注意</strong>：<strong>物理地址就是 MAC 地址（硬件地址），属于数据链路层</strong>，不属于物理层接口规范。它名字里带“物理”二字，是最常见的概念陷阱。

### 2.2 数据通信基础：消息、数据、信号与码元

| 概念 | 含义 | 例子 |
|---|---|---|
| <strong>消息</strong> | 需要处理和传输的内容 | 文字、图片、音频、视频 |
| <strong>数据</strong> | 运送消息的<strong>实体</strong> | 人类熟悉的十进制数、计算机处理的二进制比特 |
| <strong>信号</strong> | 数据的<strong>电（磁）表现</strong> | 网卡把比特 0/1 变换成电信号送上网线 |
| <strong>基带信号</strong> | 信源发出的<strong>原始</strong>电信号 | 数字基带：CPU 与内存之间；模拟基带：麦克风拾音后的音频 |

信号在<strong>信道</strong>中传输，信道分<strong>数字信道</strong>与<strong>模拟信道</strong>。对信号的处理只有两种：

| 处理 | 定义 | 输出信号 | 走哪种信道 | 典型例子 |
|---|---|---|---|---|
| <strong>编码</strong> | 不改变信号性质，只对数字基带信号的<strong>波形</strong>进行变换 | 数字信号 | 数字信道 | 以太网的曼彻斯特编码、4B/5B、8B/10B |
| <strong>调制</strong> | 把数字基带信号的<strong>频率范围搬移到较高频段</strong>，并转换为模拟信号 | 模拟信号 | 模拟信道 | Wi-Fi 的 CCK（补码键控）、DSSS（直接序列扩频）、OFDM |

模拟基带信号同样可以编码或调制：

- <strong>编码</strong>：PCM（脉冲编码调制）把模拟音频经<strong>采样 → 量化 → 编码</strong>三步数字化；
- <strong>调制</strong>：把语音加载到模拟载波上传输（传统电话）；<strong>FDM 频分复用</strong>把多路信号搬到不同频段，充分利用带宽资源。

<strong>码元</strong>：用时间域波形表示数字信号时，代表不同离散数值的<strong>基本波形</strong>。一段码元波形可以表示比特 0，也可以表示比特 1；一个信号若由两种基本波形构成，就由两种码元构成。

<figure class="cpd-figure not-content" id="cn-w2-fig-3">
  <svg viewBox="0 0 720 330" role="img" aria-label="数据通信系统模型、编码与调制路径对比及多进制码元波形示意图">
    <defs>
      <marker id="cn-w2-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="92" rx="6"/>
    <rect class="fig-box-subtle" x="16" y="118" width="336" height="90" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="118" width="336" height="90" rx="6"/>
    <rect class="fig-box-subtle" x="16" y="218" width="688" height="96" rx="6"/>
    <line class="fig-line" x1="112" y1="68" x2="140" y2="68" marker-end="url(#cn-w2-arr-3)"/>
    <line class="fig-line-accent" x1="276" y1="68" x2="304" y2="68" marker-end="url(#cn-w2-arr-3)"/>
    <line class="fig-line-accent" x1="436" y1="68" x2="464" y2="68" marker-end="url(#cn-w2-arr-3)"/>
    <line class="fig-line" x1="600" y1="68" x2="626" y2="68" marker-end="url(#cn-w2-arr-3)"/>
    <line class="fig-line-dashed" x1="120" y1="242" x2="120" y2="294"/>
    <line class="fig-line-dashed" x1="204" y1="242" x2="204" y2="294"/>
    <line class="fig-line-dashed" x1="288" y1="242" x2="288" y2="294"/>
    <line class="fig-line-dashed" x1="372" y1="242" x2="372" y2="294"/>
    <line class="fig-line-dashed" x1="456" y1="242" x2="456" y2="294"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" points="120,278 120,248 204,248 204,284 288,284 288,248 372,248 372,260 456,260"/>
    <rect class="fig-box" x="28" y="44" width="84" height="48" rx="4"/>
    <rect class="fig-box-blue" x="142" y="44" width="134" height="48" rx="4"/>
    <rect class="fig-box-orange" x="306" y="44" width="130" height="48" rx="4"/>
    <rect class="fig-box-green" x="466" y="44" width="134" height="48" rx="4"/>
    <rect class="fig-box" x="628" y="44" width="64" height="48" rx="4"/>
    <rect class="fig-box-blue" x="28" y="142" width="68" height="24" rx="4"/>
    <rect class="fig-box" x="104" y="142" width="236" height="24" rx="4"/>
    <rect class="fig-box" x="28" y="172" width="312" height="26" rx="4"/>
    <rect class="fig-box-orange" x="380" y="142" width="68" height="24" rx="4"/>
    <rect class="fig-box" x="456" y="142" width="236" height="24" rx="4"/>
    <rect class="fig-box" x="380" y="172" width="312" height="26" rx="4"/>
    <rect class="fig-box-purple" x="474" y="244" width="218" height="58" rx="4"/>
    <text class="fig-title" x="360" y="34" font-size="12" text-anchor="middle">数据通信系统模型（消息 → 数据 → 信号 → 信道传输 → 还原）</text>
    <text class="fig-label" x="70" y="65" font-size="12" text-anchor="middle">信源</text>
    <text class="fig-muted" x="70" y="81" font-size="10" text-anchor="middle">消息 → 基带</text>
    <text class="fig-blue" x="209" y="64" font-size="12" font-weight="bold" text-anchor="middle">发送设备</text>
    <text class="fig-muted" x="209" y="81" font-size="10" text-anchor="middle">编码器 / 调制器</text>
    <text class="fig-orange" x="371" y="64" font-size="12" font-weight="bold" text-anchor="middle">传输信道 + 噪声</text>
    <text class="fig-muted" x="371" y="81" font-size="10" text-anchor="middle">数字信道 / 模拟信道</text>
    <text class="fig-green" x="533" y="64" font-size="12" font-weight="bold" text-anchor="middle">接收设备</text>
    <text class="fig-muted" x="533" y="81" font-size="10" text-anchor="middle">解码器 / 解调器</text>
    <text class="fig-label" x="660" y="65" font-size="12" text-anchor="middle">信宿</text>
    <text class="fig-muted" x="660" y="81" font-size="10" text-anchor="middle">终点设备</text>
    <text class="fig-title" x="184" y="135" font-size="11.5" text-anchor="middle">路径 A：编码（输出数字信号 → 走数字信道）</text>
    <text class="fig-blue" x="62" y="158" font-size="11" font-weight="bold" text-anchor="middle">数字基带</text>
    <text class="fig-label" x="222" y="158" font-size="10.5" text-anchor="middle">波形变换：曼彻斯特 / 4B5B / 8B10B</text>
    <text class="fig-muted" x="184" y="189" font-size="10.5" text-anchor="middle">模拟基带编码（PCM）：采样(f_s≥2f_max) → 量化 → 编码</text>
    <text class="fig-title" x="536" y="135" font-size="11.5" text-anchor="middle">路径 B：调制（输出模拟信号 → 走模拟信道）</text>
    <text class="fig-orange" x="414" y="158" font-size="11" font-weight="bold" text-anchor="middle">数字基带</text>
    <text class="fig-label" x="574" y="158" font-size="10.5" text-anchor="middle">频谱搬移：ASK / FSK / PSK / QAM / OFDM</text>
    <text class="fig-muted" x="536" y="189" font-size="10.5" text-anchor="middle">模拟基带调制：搬移至高频载波（如 FDM 频分复用电话语音）</text>
    <text class="fig-title" x="44" y="236" font-size="11.5">码元（Symbol）：时间域上代表不同离散数值的基本波形</text>
    <text class="fig-muted" x="68" y="254" font-size="10">电平 +3V</text>
    <text class="fig-muted" x="68" y="266" font-size="10">电平 +1V</text>
    <text class="fig-muted" x="68" y="286" font-size="10">电平 -3V</text>
    <text class="fig-label" x="162" y="307" font-size="10.5" text-anchor="middle">码元 1 (T_s)</text>
    <text class="fig-label" x="246" y="307" font-size="10.5" text-anchor="middle">码元 2 (T_s)</text>
    <text class="fig-label" x="330" y="307" font-size="10.5" text-anchor="middle">码元 3 (T_s)</text>
    <text class="fig-label" x="414" y="307" font-size="10.5" text-anchor="middle">码元 4 (T_s)</text>
    <text class="fig-purple" x="583" y="264" font-size="11" font-weight="bold" text-anchor="middle">M 进制码元携带比特数</text>
    <text class="fig-label" x="583" y="282" font-size="11" text-anchor="middle">1 码元 = log2(M) bit</text>
    <text class="fig-muted" x="583" y="296" font-size="10" text-anchor="middle">例：4 种波形 → 每码元带 2 bit</text>
  </svg>
  <figcaption>图 2-3：数据通信系统模型、编码（含 PCM）与调制对比及码元定义</figcaption>
</figure>

<strong>传输媒体 ≠ 信道</strong>：

- <strong>单工</strong>传输：传输媒体中只包含 <strong>1 个</strong>信道（发送或接收）；
- <strong>半双工/全双工</strong>传输：传输媒体中包含 <strong>2 个</strong>信道（一个发送、一个接收）；
- 采用<strong>信道复用</strong>技术时，一条传输媒体还可包含<strong>多个</strong>信道。

<figure class="cpd-figure not-content" id="cn-w2-fig-4">
  <svg viewBox="0 0 720 318" role="img" aria-label="串行与并行传输、同步与异步传输、单工半双工全双工对比">
    <defs>
      <marker id="cn-w2-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="218" height="286" rx="6"/>
    <rect class="fig-box-subtle" x="246" y="16" width="248" height="286" rx="6"/>
    <rect class="fig-box-subtle" x="506" y="16" width="198" height="286" rx="6"/>
    <line class="fig-line-accent" x1="76" y1="84" x2="174" y2="84" stroke-width="1.8" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" x1="76" y1="176" x2="174" y2="176" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" x1="76" y1="190" x2="174" y2="190" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" x1="76" y1="204" x2="174" y2="204" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" x1="76" y1="218" x2="174" y2="218" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.6" x1="556" y1="84" x2="650" y2="84" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" x1="556" y1="162" x2="650" y2="162" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line-dashed" x1="650" y1="178" x2="556" y2="178" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" x1="556" y1="246" x2="650" y2="246" marker-end="url(#cn-w2-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" x1="650" y1="262" x2="556" y2="262" marker-end="url(#cn-w2-arr-4)"/>
    <rect class="fig-box-blue" x="28" y="66" width="48" height="36" rx="4"/>
    <rect class="fig-box-blue" x="176" y="66" width="46" height="36" rx="4"/>
    <rect class="fig-box" x="92" y="62" width="66" height="18" rx="3"/>
    <rect class="fig-box" x="28" y="170" width="48" height="56" rx="4"/>
    <rect class="fig-box" x="176" y="170" width="46" height="56" rx="4"/>
    <rect class="fig-box-green" x="258" y="70" width="54" height="34" rx="3"/>
    <rect class="fig-box-blue" x="314" y="70" width="110" height="34" rx="3"/>
    <rect class="fig-box-green" x="426" y="70" width="56" height="34" rx="3"/>
    <rect class="fig-box-orange" x="258" y="190" width="44" height="34" rx="3"/>
    <rect class="fig-box" x="304" y="190" width="96" height="34" rx="3"/>
    <rect class="fig-box-purple" x="402" y="190" width="44" height="34" rx="3"/>
    <rect class="fig-box-dashed" x="448" y="190" width="34" height="34" rx="3"/>
    <rect class="fig-box" x="518" y="68" width="36" height="32" rx="4"/>
    <rect class="fig-box" x="652" y="68" width="38" height="32" rx="4"/>
    <rect class="fig-box" x="518" y="154" width="36" height="32" rx="4"/>
    <rect class="fig-box" x="652" y="154" width="38" height="32" rx="4"/>
    <rect class="fig-box-green" x="518" y="238" width="36" height="32" rx="4"/>
    <rect class="fig-box-green" x="652" y="238" width="38" height="32" rx="4"/>
    <text class="fig-title" x="125" y="36" font-size="12" text-anchor="middle">串行 vs 并行传输</text>
    <text class="fig-blue" x="28" y="56" font-size="11" font-weight="bold">串行传输（远距离网络通信）</text>
    <text class="fig-blue" x="52" y="88" font-size="11" text-anchor="middle">发送</text>
    <text class="fig-blue" x="199" y="88" font-size="11" text-anchor="middle">接收</text>
    <text class="fig-label" x="125" y="75" font-size="10.5" text-anchor="middle">1 0 1 1 0</text>
    <text class="fig-muted" x="125" y="118" font-size="10" text-anchor="middle">1 条信道逐比特依次传输</text>
    <text class="fig-muted" x="125" y="133" font-size="10" text-anchor="middle">成本低、无多线时延偏斜</text>
    <text class="fig-label" x="28" y="158" font-size="11" font-weight="bold">并行传输（计算机内部总线）</text>
    <text class="fig-label" x="52" y="202" font-size="11" text-anchor="middle">CPU</text>
    <text class="fig-label" x="199" y="202" font-size="11" text-anchor="middle">内存</text>
    <text class="fig-muted" x="125" y="246" font-size="10" text-anchor="middle">n 条线同时传 n bit（速度快）</text>
    <text class="fig-muted" x="125" y="261" font-size="10" text-anchor="middle">远距线间串扰大、造价高</text>
    <text class="fig-title" x="370" y="36" font-size="12" text-anchor="middle">同步 vs 异步传输</text>
    <text class="fig-green" x="258" y="58" font-size="11" font-weight="bold">同步传输：以数据块（帧）为单位</text>
    <text class="fig-green" x="285" y="91" font-size="10" text-anchor="middle">同步头</text>
    <text class="fig-blue" x="369" y="91" font-size="10.5" text-anchor="middle">连续比特流数据块</text>
    <text class="fig-green" x="454" y="91" font-size="10" text-anchor="middle">帧尾</text>
    <text class="fig-muted" x="370" y="122" font-size="10" text-anchor="middle">收发双方时钟严格同步，帧间无空隙</text>
    <text class="fig-muted" x="370" y="137" font-size="10" text-anchor="middle">开销小、效率高（高速链路普遍采用）</text>
    <text class="fig-orange" x="258" y="176" font-size="11" font-weight="bold">异步传输：以字符（字节）为单位</text>
    <text class="fig-orange" x="280" y="206" font-size="10" text-anchor="middle">起始</text>
    <text class="fig-muted" x="280" y="218" font-size="9.5" text-anchor="middle">bit 0</text>
    <text class="fig-label" x="352" y="211" font-size="10" text-anchor="middle">5～8 位数据位</text>
    <text class="fig-purple" x="424" y="206" font-size="10" text-anchor="middle">停止</text>
    <text class="fig-muted" x="424" y="218" font-size="9.5" text-anchor="middle">bit 1</text>
    <text class="fig-muted" x="465" y="211" font-size="9.5" text-anchor="middle">空闲</text>
    <text class="fig-muted" x="370" y="246" font-size="10" text-anchor="middle">字符间间隔任意，字符内部比特间隔固定</text>
    <text class="fig-muted" x="370" y="261" font-size="10" text-anchor="middle">靠起始位触发局部时钟采样（如键盘串口）</text>
    <text class="fig-title" x="605" y="36" font-size="12" text-anchor="middle">交互方向与信道数</text>
    <text class="fig-blue" x="518" y="58" font-size="10.5" font-weight="bold">单工（1 条信道）</text>
    <text class="fig-label" x="536" y="88" font-size="11" text-anchor="middle">A</text>
    <text class="fig-label" x="671" y="88" font-size="11" text-anchor="middle">B</text>
    <text class="fig-muted" x="605" y="114" font-size="10" text-anchor="middle">仅单向：无线电广播</text>
    <text class="fig-orange" x="518" y="144" font-size="10.5" font-weight="bold">半双工（2 条信道）</text>
    <text class="fig-label" x="536" y="174" font-size="11" text-anchor="middle">A</text>
    <text class="fig-label" x="671" y="174" font-size="11" text-anchor="middle">B</text>
    <text class="fig-muted" x="605" y="202" font-size="10" text-anchor="middle">双向交替：对讲机 / Wi-Fi</text>
    <text class="fig-green" x="518" y="228" font-size="10.5" font-weight="bold">全双工（2 条信道）</text>
    <text class="fig-green" x="536" y="258" font-size="11" text-anchor="middle">A</text>
    <text class="fig-green" x="671" y="258" font-size="11" text-anchor="middle">B</text>
    <text class="fig-muted" x="605" y="286" font-size="10" text-anchor="middle">双向同时：交换式以太网</text>
  </svg>
  <figcaption>图 2-4：串行与并行传输、同步与异步传输格式、单工/半双工/全双工信道数量对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w2-fig-5">
  <svg viewBox="0 0 720 344" role="img" aria-label="物理层信道复用技术对比与CDMA正交码片规格化内积计算追踪">
    <defs>
      <marker id="cn-w2-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="140" rx="6"/>
    <rect class="fig-box-subtle" x="16" y="166" width="688" height="162" rx="6"/>
    <rect class="fig-box" x="26" y="42" width="158" height="104" rx="4"/>
    <rect class="fig-box" x="194" y="42" width="160" height="104" rx="4"/>
    <rect class="fig-box" x="364" y="42" width="162" height="104" rx="4"/>
    <rect class="fig-box" x="536" y="42" width="158" height="104" rx="4"/>
    <rect class="fig-box-blue" x="38" y="64" width="134" height="14" rx="2"/>
    <rect class="fig-box-green" x="38" y="84" width="134" height="14" rx="2"/>
    <rect class="fig-box-orange" x="38" y="104" width="134" height="14" rx="2"/>
    <rect class="fig-box-blue" x="204" y="70" width="28" height="28" rx="2"/>
    <rect class="fig-box-green" x="234" y="70" width="28" height="28" rx="2"/>
    <rect class="fig-box-orange" x="264" y="70" width="28" height="28" rx="2"/>
    <rect class="fig-box-blue" x="298" y="70" width="24" height="28" rx="2"/>
    <rect class="fig-box-dashed" x="324" y="70" width="22" height="28" rx="2"/>
    <rect class="fig-box-blue" x="374" y="70" width="44" height="28" rx="2"/>
    <rect class="fig-box-orange" x="422" y="70" width="44" height="28" rx="2"/>
    <rect class="fig-box-green" x="470" y="70" width="46" height="28" rx="2"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.6" x1="548" y1="70" x2="578" y2="82"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" x1="548" y1="84" x2="578" y2="84"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" x1="548" y1="98" x2="578" y2="86"/>
    <line class="fig-line-accent" x1="578" y1="84" x2="652" y2="84" stroke-width="2.2" marker-end="url(#cn-w2-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.6" x1="652" y1="82" x2="682" y2="70"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" x1="652" y1="84" x2="682" y2="84"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" x1="652" y1="86" x2="682" y2="98"/>
    <rect class="fig-box-purple" x="596" y="73" width="42" height="22" rx="3"/>
    <line class="fig-line" x1="352" y1="214" x2="386" y2="236" marker-end="url(#cn-w2-arr-5)"/>
    <line class="fig-line" x1="352" y1="258" x2="386" y2="244" marker-end="url(#cn-w2-arr-5)"/>
    <line class="fig-line-accent" x1="504" y1="240" x2="532" y2="218" marker-end="url(#cn-w2-arr-5)"/>
    <line class="fig-line-accent" x1="504" y1="244" x2="532" y2="266" marker-end="url(#cn-w2-arr-5)"/>
    <rect class="fig-box-blue" x="28" y="194" width="324" height="36" rx="4"/>
    <rect class="fig-box-orange" x="28" y="238" width="324" height="36" rx="4"/>
    <rect class="fig-box" x="28" y="282" width="324" height="34" rx="4"/>
    <rect class="fig-box-purple" x="388" y="216" width="116" height="52" rx="5"/>
    <rect class="fig-box-green" x="534" y="194" width="158" height="44" rx="4"/>
    <rect class="fig-box-orange" x="534" y="246" width="158" height="44" rx="4"/>
    <rect class="fig-box" x="388" y="294" width="304" height="22" rx="3"/>
    <text class="fig-title" x="360" y="34" font-size="12" text-anchor="middle">信道复用全景：一条物理传输媒体同时承载多路逻辑信道</text>
    <text class="fig-blue" x="105" y="57" font-size="11" font-weight="bold" text-anchor="middle">FDM 频分复用</text>
    <text class="fig-label" x="105" y="75" font-size="9.5" text-anchor="middle">频带 f3</text>
    <text class="fig-label" x="105" y="95" font-size="9.5" text-anchor="middle">频带 f2（留隔离带）</text>
    <text class="fig-label" x="105" y="115" font-size="9.5" text-anchor="middle">频带 f1</text>
    <text class="fig-muted" x="105" y="136" font-size="9.5" text-anchor="middle">同时间、不同频率并行</text>
    <text class="fig-green" x="274" y="57" font-size="11" font-weight="bold" text-anchor="middle">TDM 同步时分</text>
    <text class="fig-label" x="218" y="88" font-size="10" text-anchor="middle">A</text>
    <text class="fig-label" x="248" y="88" font-size="10" text-anchor="middle">B</text>
    <text class="fig-label" x="278" y="88" font-size="10" text-anchor="middle">C</text>
    <text class="fig-label" x="310" y="88" font-size="10" text-anchor="middle">A</text>
    <text class="fig-muted" x="335" y="88" font-size="9.5" text-anchor="middle">空</text>
    <text class="fig-muted" x="274" y="118" font-size="9.5" text-anchor="middle">固定帧时隙·无数据也空占</text>
    <text class="fig-muted" x="274" y="136" font-size="9.5" text-anchor="middle">不同时间占同一频带</text>
    <text class="fig-orange" x="445" y="57" font-size="11" font-weight="bold" text-anchor="middle">STDM 统计时分</text>
    <text class="fig-label" x="396" y="88" font-size="9.5" text-anchor="middle">A+址</text>
    <text class="fig-label" x="444" y="88" font-size="9.5" text-anchor="middle">C+址</text>
    <text class="fig-label" x="493" y="88" font-size="9.5" text-anchor="middle">B+址</text>
    <text class="fig-muted" x="445" y="118" font-size="9.5" text-anchor="middle">异步按需分配·必带地址</text>
    <text class="fig-muted" x="445" y="136" font-size="9.5" text-anchor="middle">显著提高线路利用率</text>
    <text class="fig-purple" x="615" y="57" font-size="11" font-weight="bold" text-anchor="middle">WDM 波分复用</text>
    <text class="fig-purple" x="617" y="88" font-size="9.5" text-anchor="middle">EDFA</text>
    <text class="fig-muted" x="615" y="118" font-size="9.5" text-anchor="middle">本质是光的频分复用</text>
    <text class="fig-muted" x="615" y="136" font-size="9.5" text-anchor="middle">多路波长共纤传输</text>
    <text class="fig-title" x="360" y="184" font-size="12" text-anchor="middle">CDMA 码分多址：同时间同频带，靠相互正交的 m 位码片序列（S·T = 0）区分用户</text>
    <text class="fig-blue" x="38" y="209" font-size="10.5" font-weight="bold">站 A 码片 S = (-1,-1,-1,+1,+1,-1,+1,+1)</text>
    <text class="fig-muted" x="38" y="224" font-size="10">发比特 1 → 发原码 +S = (-1,-1,-1,+1,+1,-1,+1,+1)</text>
    <text class="fig-orange" x="38" y="253" font-size="10.5" font-weight="bold">站 B 码片 T = (-1,-1,+1,-1,+1,+1,+1,-1)</text>
    <text class="fig-muted" x="38" y="268" font-size="10">发比特 0 → 发反码 -T = (+1,+1,-1,+1,-1,-1,-1,+1)</text>
    <text class="fig-label" x="190" y="303" font-size="10" text-anchor="middle">正交校验：S·T = (+1+1-1-1+1-1+1-1)/8 = 0</text>
    <text class="fig-purple" x="446" y="236" font-size="10.5" font-weight="bold" text-anchor="middle">信道线性叠加 X</text>
    <text class="fig-label" x="446" y="252" font-size="10" text-anchor="middle">(0,0,-2,+2,</text>
    <text class="fig-label" x="446" y="264" font-size="10" text-anchor="middle">0,-2,0,+2)</text>
    <text class="fig-green" x="613" y="212" font-size="10.5" font-weight="bold" text-anchor="middle">S·X = +8/8 = +1</text>
    <text class="fig-label" x="613" y="229" font-size="10" text-anchor="middle">判决：站 A 发比特 1</text>
    <text class="fig-orange" x="613" y="264" font-size="10.5" font-weight="bold" text-anchor="middle">T·X = -8/8 = -1</text>
    <text class="fig-label" x="613" y="281" font-size="10" text-anchor="middle">判决：站 B 发比特 0</text>
    <text class="fig-muted" x="540" y="309" font-size="10" text-anchor="middle">规格化内积判决：+1 → 比特 1；-1 → 比特 0；0 → 未发送</text>
  </svg>
  <figcaption>图 2-5：FDM / TDM / STDM / WDM 信道复用对比与 CDMA 正交码片规格化内积分离演算</figcaption>
</figure>

### 2.3 常用数字基带编码

以比特流 `1 0 1 1 0` 为例（曼彻斯特按课程约定：负跳变 = 1，正跳变 = 0）：

<figure class="cpd-figure not-content" id="cn-w2-fig-6">
  <svg viewBox="0 0 720 356" role="img" aria-label="比特流10110在NRZ、NRZI、RZ、曼彻斯特、差分曼彻斯特下的编码波形对比">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="324" rx="6"/>
    <line class="fig-divider" x1="152" y1="74" x2="692" y2="74"/>
    <line class="fig-divider" x1="152" y1="126" x2="692" y2="126"/>
    <line class="fig-divider" x1="152" y1="178" x2="692" y2="178"/>
    <line class="fig-divider" x1="152" y1="230" x2="692" y2="230"/>
    <line class="fig-divider" x1="152" y1="282" x2="692" y2="282"/>
    <line class="fig-line-dashed" x1="152" y1="48" x2="152" y2="306"/>
    <line class="fig-line-dashed" x1="260" y1="48" x2="260" y2="306"/>
    <line class="fig-line-dashed" x1="368" y1="48" x2="368" y2="306"/>
    <line class="fig-line-dashed" x1="476" y1="48" x2="476" y2="306"/>
    <line class="fig-line-dashed" x1="584" y1="48" x2="584" y2="306"/>
    <line class="fig-line-dashed" x1="692" y1="48" x2="692" y2="306"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" points="152,58 260,58 260,90 368,90 368,58 476,58 584,58 584,90 692,90"/>
    <polyline class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2.2" points="152,142 152,110 260,110 368,110 368,142 476,142 476,110 584,110 692,110"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2.2" points="152,178 152,162 206,162 206,178 260,178 260,194 314,194 314,178 368,178 368,162 422,162 422,178 476,178 476,162 530,162 530,178 584,178 584,194 638,194 638,178 692,178"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" points="152,214 206,214 206,246 260,246 314,246 314,214 368,214 422,214 422,246 476,246 476,214 530,214 530,246 584,246 638,246 638,214 692,214"/>
    <polyline class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2" points="152,266 206,266 206,298 260,298 260,266 314,266 314,298 368,298 422,298 422,266 476,266 530,266 530,298 584,298 584,266 638,266 638,298 692,298"/>
    <rect class="fig-box-blue" x="156" y="24" width="100" height="22" rx="3"/>
    <rect class="fig-box" x="264" y="24" width="100" height="22" rx="3"/>
    <rect class="fig-box-blue" x="372" y="24" width="100" height="22" rx="3"/>
    <rect class="fig-box-blue" x="480" y="24" width="100" height="22" rx="3"/>
    <rect class="fig-box" x="588" y="24" width="100" height="22" rx="3"/>
    <rect class="fig-box" x="26" y="55" width="118" height="38" rx="4"/>
    <rect class="fig-box" x="26" y="107" width="118" height="38" rx="4"/>
    <rect class="fig-box" x="26" y="159" width="118" height="38" rx="4"/>
    <rect class="fig-box-blue" x="26" y="211" width="118" height="38" rx="4"/>
    <rect class="fig-box-green" x="26" y="263" width="118" height="38" rx="4"/>
    <circle class="fig-dot" cx="206" cy="230" r="3.5"/>
    <circle class="fig-dot" cx="314" cy="230" r="3.5"/>
    <circle class="fig-dot" cx="422" cy="230" r="3.5"/>
    <circle class="fig-dot" cx="530" cy="230" r="3.5"/>
    <circle class="fig-dot" cx="638" cy="230" r="3.5"/>
    <circle class="fig-box-green" cx="206" cy="282" r="3.5"/>
    <circle class="fig-box-green" cx="314" cy="282" r="3.5"/>
    <circle class="fig-box-green" cx="422" cy="282" r="3.5"/>
    <circle class="fig-box-green" cx="530" cy="282" r="3.5"/>
    <circle class="fig-box-green" cx="638" cy="282" r="3.5"/>
    <circle class="fig-box-red" cx="260" cy="282" r="4.5"/>
    <circle class="fig-box-red" cx="584" cy="282" r="4.5"/>
    <rect class="fig-box" x="26" y="312" width="666" height="20" rx="3"/>
    <text class="fig-muted" x="85" y="39" font-size="11" text-anchor="middle">输入比特流</text>
    <text class="fig-blue" x="206" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-label" x="314" y="39" font-size="12" font-weight="bold" text-anchor="middle">0</text>
    <text class="fig-blue" x="422" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-blue" x="530" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-label" x="638" y="39" font-size="12" font-weight="bold" text-anchor="middle">0</text>
    <text class="fig-label" x="85" y="72" font-size="11" font-weight="bold" text-anchor="middle">NRZ 不归零</text>
    <text class="fig-muted" x="85" y="86" font-size="9.5" text-anchor="middle">+V=1 / -V=0 · 无同步</text>
    <text class="fig-purple" x="85" y="124" font-size="11" font-weight="bold" text-anchor="middle">NRZI 反向不归零</text>
    <text class="fig-muted" x="85" y="138" font-size="9.5" text-anchor="middle">遇1翻转 / 遇0不变</text>
    <text class="fig-orange" x="85" y="176" font-size="11" font-weight="bold" text-anchor="middle">RZ 归零编码</text>
    <text class="fig-muted" x="85" y="190" font-size="9.5" text-anchor="middle">后半归零带时钟·效率低</text>
    <text class="fig-blue" x="85" y="228" font-size="11" font-weight="bold" text-anchor="middle">曼彻斯特</text>
    <text class="fig-muted" x="85" y="242" font-size="9.5" text-anchor="middle">中点↓=1 / ↑=0 (10BASE-T)</text>
    <text class="fig-green" x="85" y="280" font-size="11" font-weight="bold" text-anchor="middle">差分曼彻斯特</text>
    <text class="fig-muted" x="85" y="294" font-size="9.5" text-anchor="middle">起点变=0 / 不变=1</text>
    <text class="fig-label" x="359" y="326" font-size="10.5" text-anchor="middle">圆点标注：曼彻斯特中点跳变兼表时钟与数据；差分曼彻斯特中点绿点仅表时钟，码元起点红圈跳变表比特“0”</text>
  </svg>
  <figcaption>图 2-6：常用数字基带编码（NRZ、NRZI、RZ、曼彻斯特、差分曼彻斯特）精确时序对比</figcaption>
</figure>

| 编码 | 规则 | 自同步 | 特点 |
|---|---|---|---|
| <strong>NRZ 不归零 | 整个码元时间内保持正电平 = 1、负电平 = 0，不回到零电平 | 否 | 效率最高，但需另加一根时钟线，收发必须严格同步；计算机网络不采用</strong> |
| <strong>RZ 归零</strong> | 每个码元传输结束后信号<strong>必须归零；正电平 = 1，负电平 = 0 | 是 | 用归零把时钟编进数据；但大部分带宽被归零浪费，编码效率低</strong> |
| <strong>曼彻斯特</strong> | 每个码元<strong>中间时刻必跳变</strong>，跳变<strong>既表示时钟又表示数据</strong> | 是 | 传统以太网（10BASE-T）采用 |
| <strong>差分曼彻斯特</strong> | 中间时刻跳变<strong>仅表示时钟</strong>；码元<strong>开始处电平变化 = 0、不变化 = 1</strong> | 是 | 判读数据需参考前一码元；跳变只表时钟，边界是否变化才表数据 |

<strong>为什么不用 NRZ</strong>：接收方看到连续的正电平，无法判断它是 1 个还是 3 个码元，必须额外用一根传输线传时钟信号，接收方按节拍逐个采样。计算机网络宁愿把这根线用来传数据，所以不采用 NRZ。

<strong>RZ 的自同步</strong>：每个码元传输结束后信号必归零，接收方只需在“归零之后”采样，不需要单独的时钟信号——相当于把时钟用归零方式编码在数据之内，称为<strong>自同步信号</strong>。

> <strong>注意</strong>：2015 题出现了课程未展开的 <strong>NRZI（反向不归零）</strong>，它不影响解题——按“码元内电平不变、1 与 0 对应不同电平”认出 NRZ，按“中间时刻跳变”认出曼彻斯特即可。

> <strong>★考点（2013）</strong>：`10BASE-T` 中 <strong>10 = 10 Mb/s</strong>，<strong>BASE = 基带传输</strong>，<strong>T = 双绞线</strong>，该以太网使用<strong>曼彻斯特编码</strong>。解题时按“每码元中间时刻发生跳变”划分码元；正跳变表示 1 还是 0 <strong>可自行假设，若得到的比特串不在选项中，说明假设取反了，逐位取反</strong>即可。

<figure class="cpd-figure not-content" id="cn-w2-fig-7">
  <svg viewBox="0 0 720 276" role="img" aria-label="10BASE-T曼彻斯特波形反推比特串解题步骤与4B5B编码效率对比">
    <defs>
      <marker id="cn-w2-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="424" height="244" rx="6"/>
    <rect class="fig-box-subtle" x="452" y="16" width="252" height="244" rx="6"/>
    <line class="fig-line-dashed" x1="56" y1="62" x2="56" y2="156"/>
    <line class="fig-line-dashed" x1="146" y1="62" x2="146" y2="156"/>
    <line class="fig-line-dashed" x1="236" y1="62" x2="236" y2="156"/>
    <line class="fig-line-dashed" x1="326" y1="62" x2="326" y2="156"/>
    <line class="fig-line-dashed" x1="416" y1="62" x2="416" y2="156"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.4" points="56,126 101,126 101,78 146,78 146,126 191,126 191,78 236,78 281,78 281,126 326,126 371,126 371,78 416,78"/>
    <line class="fig-line-accent" x1="101" y1="112" x2="101" y2="90" marker-end="url(#cn-w2-arr-7)"/>
    <line class="fig-line-accent" x1="191" y1="112" x2="191" y2="90" marker-end="url(#cn-w2-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" x1="281" y1="90" x2="281" y2="112" marker-end="url(#cn-w2-arr-7)"/>
    <line class="fig-line-accent" x1="371" y1="112" x2="371" y2="90" marker-end="url(#cn-w2-arr-7)"/>
    <rect class="fig-box" x="30" y="164" width="396" height="40" rx="4"/>
    <rect class="fig-box-blue" x="30" y="212" width="396" height="36" rx="4"/>
    <rect class="fig-box-orange" x="466" y="54" width="224" height="84" rx="5"/>
    <rect class="fig-box-green" x="466" y="150" width="224" height="98" rx="5"/>
    <text class="fig-title" x="228" y="36" font-size="12" text-anchor="middle">2013 真题模型：由 10BASE-T 波形反推比特串</text>
    <text class="fig-muted" x="101" y="56" font-size="10.5" text-anchor="middle">码元 1 (↑)</text>
    <text class="fig-muted" x="191" y="56" font-size="10.5" text-anchor="middle">码元 2 (↑)</text>
    <text class="fig-muted" x="281" y="56" font-size="10.5" text-anchor="middle">码元 3 (↓)</text>
    <text class="fig-muted" x="371" y="56" font-size="10.5" text-anchor="middle">码元 4 (↑)</text>
    <text class="fig-label" x="228" y="181" font-size="10.5" text-anchor="middle">① 先找等间隔的“码元中点必有跳变”定出边界虚线</text>
    <text class="fig-muted" x="228" y="197" font-size="10" text-anchor="middle">② 假设 ↑=0、↓=1 得 0010；若选项无此串则逐位取反得 1101</text>
    <text class="fig-blue" x="228" y="228" font-size="10.5" font-weight="bold" text-anchor="middle">10BASE-T 波特率陷阱：数据率 10 Mb/s → 调制速率 20 MBaud</text>
    <text class="fig-muted" x="228" y="242" font-size="10" text-anchor="middle">每传 1 bit 需电平跳变 2 次（脉冲宽度减半，占用双倍频带）</text>
    <text class="fig-title" x="578" y="36" font-size="12" text-anchor="middle">编码效率对比（408 拓展）</text>
    <text class="fig-orange" x="578" y="75" font-size="11" font-weight="bold" text-anchor="middle">曼彻斯特 / 差分曼彻斯特</text>
    <text class="fig-label" x="578" y="95" font-size="11" text-anchor="middle">编码效率 = 50%</text>
    <text class="fig-muted" x="578" y="113" font-size="10" text-anchor="middle">适用：10 Mb/s 传统以太网</text>
    <text class="fig-muted" x="578" y="128" font-size="10" text-anchor="middle">R_b = 10 Mb/s 时 R_B = 20 MBaud</text>
    <text class="fig-green" x="578" y="171" font-size="11" font-weight="bold" text-anchor="middle">4B/5B 块编码（百兆 100BASE-TX）</text>
    <text class="fig-label" x="578" y="191" font-size="11" text-anchor="middle">4 bit 数据映射为 5 bit 码组</text>
    <text class="fig-green" x="578" y="209" font-size="11" font-weight="bold" text-anchor="middle">编码效率 = 4/5 = 80%</text>
    <text class="fig-muted" x="578" y="226" font-size="10" text-anchor="middle">消除长连 0，配合 NRZI 恢复时钟</text>
    <text class="fig-muted" x="578" y="240" font-size="10" text-anchor="middle">100 Mb/s 仅需 125 MBaud</text>
  </svg>
  <figcaption>图 2-7：10BASE-T 曼彻斯特波形判读套路与曼彻斯特（50%）/ 4B5B（80%）编码效率对比</figcaption>
</figure>

### 2.4 基本调制与 QAM 混合调制

把数字基带信号搬移到较高频段并转换为模拟信号，有三种基本方法：

<figure class="cpd-figure not-content" id="cn-w2-fig-8">
  <svg viewBox="0 0 720 318" role="img" aria-label="比特流1011在ASK幅移键控、FSK频移键控、PSK相移键控下的二元调制波形对比">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="286" rx="6"/>
    <line class="fig-divider" x1="164" y1="88" x2="676" y2="88"/>
    <line class="fig-divider" x1="164" y1="162" x2="676" y2="162"/>
    <line class="fig-divider" x1="164" y1="236" x2="676" y2="236"/>
    <line class="fig-line-dashed" x1="164" y1="50" x2="164" y2="266"/>
    <line class="fig-line-dashed" x1="292" y1="50" x2="292" y2="266"/>
    <line class="fig-line-dashed" x1="420" y1="50" x2="420" y2="266"/>
    <line class="fig-line-dashed" x1="548" y1="50" x2="548" y2="266"/>
    <line class="fig-line-dashed" x1="676" y1="50" x2="676" y2="266"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2" d="M 164 88 q 8 -32 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 L 420 88 q 8 -32 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" d="M 164 162 q 8 -32 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 M 292 162 q 16 -32 32 0 t 32 0 t 32 0 t 32 0 M 420 162 q 8 -32 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0 t 16 0"/>
    <path class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" d="M 164 236 q 16 32 32 0 t 32 0 t 32 0 t 32 0 M 292 236 q 16 -32 32 0 t 32 0 t 32 0 t 32 0 M 420 236 q 16 32 32 0 t 32 0 t 32 0 t 32 0 M 548 236 q 16 32 32 0 t 32 0 t 32 0 t 32 0"/>
    <rect class="fig-box-blue" x="168" y="24" width="120" height="22" rx="3"/>
    <rect class="fig-box" x="296" y="24" width="120" height="22" rx="3"/>
    <rect class="fig-box-blue" x="424" y="24" width="120" height="22" rx="3"/>
    <rect class="fig-box-blue" x="552" y="24" width="120" height="22" rx="3"/>
    <rect class="fig-box-blue" x="28" y="66" width="124" height="44" rx="4"/>
    <rect class="fig-box-green" x="28" y="140" width="124" height="44" rx="4"/>
    <rect class="fig-box-purple" x="28" y="214" width="124" height="44" rx="4"/>
    <circle class="fig-box-red" cx="292" cy="236" r="4.5"/>
    <circle class="fig-box-red" cx="420" cy="236" r="4.5"/>
    <rect class="fig-box" x="28" y="272" width="648" height="22" rx="3"/>
    <text class="fig-muted" x="90" y="39" font-size="11" text-anchor="middle">基带比特序列</text>
    <text class="fig-blue" x="228" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-label" x="356" y="39" font-size="12" font-weight="bold" text-anchor="middle">0</text>
    <text class="fig-blue" x="484" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-blue" x="612" y="39" font-size="12" font-weight="bold" text-anchor="middle">1</text>
    <text class="fig-blue" x="90" y="85" font-size="11" font-weight="bold" text-anchor="middle">2ASK 幅移键控</text>
    <text class="fig-muted" x="90" y="101" font-size="9.5" text-anchor="middle">有载波=1 / 无载波=0</text>
    <text class="fig-green" x="90" y="159" font-size="11" font-weight="bold" text-anchor="middle">2FSK 频移键控</text>
    <text class="fig-muted" x="90" y="175" font-size="9.5" text-anchor="middle">高频 f2=1 / 低频 f1=0</text>
    <text class="fig-purple" x="90" y="233" font-size="11" font-weight="bold" text-anchor="middle">2PSK 相移键控</text>
    <text class="fig-muted" x="90" y="249" font-size="9.5" text-anchor="middle">初相 180°=1 / 0°=0</text>
    <text class="fig-label" x="352" y="287" font-size="10.5" text-anchor="middle">二元调制仅产生 N=2 种码元，每码元携带 log2(2)=1 bit（红圈标注 2PSK 在 1↔0 边界发生 180° 相位反转）</text>
  </svg>
  <figcaption>图 2-8：二元基本调制（2ASK、2FSK、2PSK）模拟载波波形对比（抗干扰能力：PSK &gt; FSK &gt; ASK）</figcaption>
</figure>

- <strong>ASK 幅移键控</strong>：控制载波<strong>振幅</strong>；无载波输出 = 0，有载波输出 = 1。
- <strong>FSK 频移键控</strong>：控制载波<strong>频率</strong>；频率 $f_1$ = 0，频率 $f_2$ = 1。
- <strong>PSK 相移键控</strong>：控制载波<strong>初相位</strong>；初相 0° = 0，初相 180° = 1。

三种基本调制都只能产生<strong>两种</strong>码元（二元调制），一个码元只携带 <strong>1 bit</strong>。

<strong>怎样让一个码元带更多比特</strong>：采用<strong>混合调制</strong>（多元调制）。频率是相位随时间的变化率，二者相关，因此<strong>一次只能调制频率和相位中的一个</strong>；通常把<strong>相位与振幅</strong>结合起来调制，称为<strong>正交振幅调制 QAM</strong>。

<strong>QAM-16</strong>：可以调制出 <strong>12 种相位</strong>，每种相位有 1 或 2 种振幅可选，共 <strong>16 种码元</strong>，每个码元携带 <strong>4 bit</strong>。

<figure class="cpd-figure not-content" id="cn-w2-fig-9">
  <svg viewBox="0 0 720 330" role="img" aria-label="QAM-16星座图16个码元分布与格雷码相邻仅差1比特抗误码原理">
    <defs>
      <marker id="cn-w2-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="336" height="298" rx="6"/>
    <rect class="fig-box-subtle" x="364" y="16" width="340" height="298" rx="6"/>
    <circle class="fig-box-dashed" cx="184" cy="172" r="100"/>
    <circle class="fig-box-dashed" cx="184" cy="172" r="54"/>
    <line class="fig-line" x1="60" y1="172" x2="310" y2="172" marker-end="url(#cn-w2-arr-9)"/>
    <line class="fig-line" x1="184" y1="292" x2="184" y2="48" marker-end="url(#cn-w2-arr-9)"/>
    <line class="fig-line-accent" x1="184" y1="172" x2="270.6" y2="122" stroke-width="1.8"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" d="M 248 172 A 64 64 0 0 0 239.4 140"/>
    <circle class="fig-dot-muted" cx="184" cy="172" r="3"/>
    <circle class="fig-dot" cx="284" cy="172" r="4.5"/>
    <circle class="fig-dot" cx="270.6" cy="122" r="4.5"/>
    <circle class="fig-dot" cx="234" cy="85.4" r="4.5"/>
    <circle class="fig-dot" cx="184" cy="72" r="4.5"/>
    <circle class="fig-dot" cx="134" cy="85.4" r="4.5"/>
    <circle class="fig-dot" cx="97.4" cy="122" r="4.5"/>
    <circle class="fig-dot" cx="84" cy="172" r="4.5"/>
    <circle class="fig-dot" cx="97.4" cy="222" r="4.5"/>
    <circle class="fig-dot" cx="134" cy="258.6" r="4.5"/>
    <circle class="fig-dot" cx="184" cy="272" r="4.5"/>
    <circle class="fig-dot" cx="234" cy="258.6" r="4.5"/>
    <circle class="fig-dot" cx="270.6" cy="222" r="4.5"/>
    <circle class="fig-box-green" cx="238" cy="172" r="4.5"/>
    <circle class="fig-box-green" cx="184" cy="118" r="4.5"/>
    <circle class="fig-box-green" cx="130" cy="172" r="4.5"/>
    <circle class="fig-box-green" cx="184" cy="226" r="4.5"/>
    <rect class="fig-box" x="198" y="128" width="46" height="18" rx="3"/>
    <rect class="fig-box-blue" x="378" y="46" width="312" height="98" rx="5"/>
    <rect class="fig-box-green" x="378" y="156" width="312" height="70" rx="5"/>
    <rect class="fig-box-red" x="378" y="236" width="312" height="66" rx="5"/>
    <text class="fig-title" x="184" y="36" font-size="12" text-anchor="middle">QAM-16 星座图（12 种相位 · 2 种振幅）</text>
    <text class="fig-muted" x="316" y="176" font-size="11">I</text>
    <text class="fig-muted" x="194" y="56" font-size="11">Q</text>
    <text class="fig-accent" x="221" y="141" font-size="10" font-weight="bold" text-anchor="middle">振幅 A</text>
    <text class="fig-orange" x="266" y="158" font-size="10" font-weight="bold">相位 φ</text>
    <text class="fig-muted" x="184" y="304" font-size="10" text-anchor="middle">点到原点距离 = 振幅；与横轴夹角 = 相位</text>
    <text class="fig-title" x="534" y="36" font-size="12" text-anchor="middle">多元调制容量换算与格雷码映射</text>
    <text class="fig-blue" x="534" y="68" font-size="11.5" font-weight="bold" text-anchor="middle">12 种相位 × (1 或 2 种振幅) = 16 个码元</text>
    <text class="fig-label" x="534" y="90" font-size="11" text-anchor="middle">每码元携带比特数 k = log2(16) = 4 bit</text>
    <text class="fig-muted" x="534" y="110" font-size="10" text-anchor="middle">对比：QPSK (4 相位) → 2 bit/码元；QAM-64 → 6 bit/码元</text>
    <text class="fig-muted" x="534" y="128" font-size="10" text-anchor="middle">注意：频率是相位对时间的变化率，二者不可同时独立调制</text>
    <text class="fig-green" x="534" y="177" font-size="11" font-weight="bold" text-anchor="middle">✓ 格雷码映射（相邻星座点仅差 1 bit）</text>
    <text class="fig-label" x="534" y="197" font-size="10.5" text-anchor="middle">例：0000 ↔ 0001 ↔ 0011 ↔ 0010</text>
    <text class="fig-muted" x="534" y="215" font-size="10" text-anchor="middle">噪声致码元轻微漂移落入邻点时，最多仅错 1 bit，易纠错</text>
    <text class="fig-red" x="534" y="256" font-size="11" font-weight="bold" text-anchor="middle">✗ 随意自然二进制映射（反例）</text>
    <text class="fig-muted" x="534" y="275" font-size="10" text-anchor="middle">若 0000 与 1111 相邻，一次边界误判即导致 4 bit 全错！</text>
    <text class="fig-muted" x="534" y="291" font-size="10" text-anchor="middle">高阶 QAM 点距更密，虽速率高但要求更高信噪比 S/N</text>
  </svg>
  <figcaption>图 2-9：QAM-16 正交振幅调制星座图（16 码元 = 4 bit/Baud）与格雷码抗误码映射原理</figcaption>
</figure>

星座图中，一个点就是一个码元：点到圆心的<strong>距离是振幅</strong>，与横坐标的<strong>夹角是相位</strong>。码元数与每码元比特数的换算：

$$
k=\log_{2}N
$$

<strong>码元与比特的映射必须用格雷码</strong>：任意两个<strong>相邻</strong>码元只有 <strong>1 个比特</strong>不同。若随意映射，噪声使码元落到相邻位置时可能多位同时出错（课程例子中五个原本都表示 `0000` 的码元失真后，最坏被解调为 `1111`，4 位全错）；格雷码把这种误判限制为<strong>1 位</strong>，才便于上层纠错码处理。

> <strong>注意</strong>：题目给出的调制技术再陌生也不影响解题——只关心它<strong>能调制出多少种不同的基本波形（码元数 $N$）</strong>。

### 2.5 奈氏准则：理想信道的码元速率上限

信号通过实际信道后波形会失真。失真的原因主要有：<strong>码元传输速率、信号传输距离、噪声干扰、传输媒体质量</strong>。失真不严重时接收端还能还原码元；失真严重时波形失去码元之间的清晰界限，无法判断何处是 1、何处是 0，这种现象称为<strong>码间串扰</strong>。

<figure class="cpd-figure not-content" id="cn-w2-fig-10">
  <svg viewBox="0 0 720 326" role="img" aria-label="信号经实际受限信道失真产生码间串扰及奈氏准则低通与带通上限对比">
    <defs>
      <marker id="cn-w2-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="444" height="294" rx="6"/>
    <rect class="fig-box-subtle" x="472" y="16" width="232" height="294" rx="6"/>
    <line class="fig-line-dashed" x1="56" y1="48" x2="56" y2="262"/>
    <line class="fig-line-dashed" x1="146" y1="48" x2="146" y2="262"/>
    <line class="fig-line-dashed" x1="236" y1="48" x2="236" y2="262"/>
    <line class="fig-line-dashed" x1="326" y1="48" x2="326" y2="262"/>
    <line class="fig-line-dashed" x1="416" y1="48" x2="416" y2="262"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" points="56,98 56,60 146,60 146,98 236,98 236,60 326,60 326,98 416,98"/>
    <line class="fig-line-accent" x1="236" y1="108" x2="236" y2="142" stroke-width="1.6" marker-end="url(#cn-w2-arr-10)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" d="M 56 188 C 66 158, 134 158, 146 180 C 158 196, 224 196, 236 180 C 248 158, 314 158, 326 180 C 338 196, 404 196, 416 192"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="2.2" d="M 56 250 C 84 222, 136 226, 174 242 C 204 254, 240 224, 286 228 C 330 232, 368 252, 416 248"/>
    <rect class="fig-box" x="250" y="113" width="192" height="22" rx="3"/>
    <rect class="fig-box-red" x="30" y="270" width="416" height="28" rx="4"/>
    <rect class="fig-box-blue" x="486" y="50" width="204" height="74" rx="5"/>
    <rect class="fig-box-purple" x="486" y="136" width="204" height="74" rx="5"/>
    <rect class="fig-box-orange" x="486" y="222" width="204" height="76" rx="5"/>
    <rect class="fig-box" x="104" y="144" width="128" height="16" rx="3"/>
    <rect class="fig-box" x="104" y="206" width="272" height="16" rx="3"/>
    <text class="fig-title" x="238" y="36" font-size="12" text-anchor="middle">实际信道波形失真与码间串扰（ISI）演化</text>
    <text class="fig-blue" x="101" y="54" font-size="10.5" text-anchor="middle">发送 1</text>
    <text class="fig-muted" x="191" y="54" font-size="10.5" text-anchor="middle">发送 0</text>
    <text class="fig-blue" x="281" y="54" font-size="10.5" text-anchor="middle">发送 1</text>
    <text class="fig-muted" x="371" y="54" font-size="10.5" text-anchor="middle">发送 0</text>
    <text class="fig-muted" x="346" y="128" font-size="10" text-anchor="middle">高频分量衰减 · 脉冲边沿展宽</text>
    <text class="fig-green" x="108" y="156" font-size="10">轻度失真：界限仍可识别</text>
    <text class="fig-red" x="108" y="218" font-size="10" font-weight="bold">严重失真（码元过密）：前后脉冲重叠 → 码间串扰！</text>
    <text class="fig-red" x="238" y="288" font-size="10.5" text-anchor="middle">成因：码元速率过高 · 传输距离过远 · 噪声干扰 · 媒体质量差</text>
    <text class="fig-title" x="588" y="36" font-size="12" text-anchor="middle">奈氏准则速率上限</text>
    <text class="fig-blue" x="588" y="70" font-size="11" font-weight="bold" text-anchor="middle">理想低通信道（默认）</text>
    <text class="fig-label" x="588" y="90" font-size="11.5" font-weight="bold" text-anchor="middle">R_Bmax = 2W Baud</text>
    <text class="fig-muted" x="588" y="110" font-size="10" text-anchor="middle">未指明上下限频率时一律用 2W</text>
    <text class="fig-purple" x="588" y="156" font-size="11" font-weight="bold" text-anchor="middle">理想带通信道（调制载波）</text>
    <text class="fig-label" x="588" y="176" font-size="11.5" font-weight="bold" text-anchor="middle">R_Bmax = W Baud</text>
    <text class="fig-muted" x="588" y="196" font-size="10" text-anchor="middle">给频率范围 [f1, f2] 时 W = f2 - f1</text>
    <text class="fig-orange" x="588" y="242" font-size="11" font-weight="bold" text-anchor="middle">极限比特率换算</text>
    <text class="fig-label" x="588" y="262" font-size="11" text-anchor="middle">R_b = R_Bmax × log2(N)</text>
    <text class="fig-muted" x="588" y="282" font-size="10" text-anchor="middle">奈氏只限波特率，不限比特率！</text>
  </svg>
  <figcaption>图 2-10：信道高频衰减导致码间串扰（ISI）及奈氏准则低通（2W）/ 带通（W）码元速率上限</figcaption>
</figure>

1924 年奈奎斯特推导出<strong>理想条件下</strong>避免码间串扰的码元传输速率上限——<strong>奈氏准则</strong>：

$$
R_{B\max}=2W\ \text{Baud}\quad(\text{ideal low-pass})
$$

$$
R_{B\max}=W\ \text{Baud}\quad(\text{ideal band-pass})
$$

$W$ 为信道带宽（Hz），Baud（波特）= <strong>码元每秒</strong>。码元传输速率又称<strong>波特率、调制速率、波形速率、符号速率</strong>。

比特率与波特率的关系：

$$
R_b=R_B\cdot \log_{2}N
$$

$N$ 为可调制出的码元数。<strong>一个码元只携带 1 bit 时，波特率与比特率在数值上相等</strong>；一个码元携带 $n$ bit 时，比特率是波特率的 $n$ 倍。

> <strong>注意</strong>：实际信道能传输的最高码元速率<strong>明显低于</strong> $2W$，因为奈氏准则是在理想条件下推导的，未考虑传输距离、噪声干扰、传输媒体质量。

> <strong>注意</strong>：题目<strong>未特别指明带通信道</strong>（即没有给出频率的上下限）时，一律按<strong>低通信道</strong>处理，用 $2W$。

### 2.6 香农公式：噪声信道的极限信息速率

只靠多元制不能无限提高速率：信道中的噪声会干扰接收端对码元的识别，噪声功率相对信号功率越大，影响越大。1948 年香农给出<strong>带宽受限、有高斯白噪声干扰</strong>的信道的极限信息传输速率：

$$
C=W\log_{2}\left(1+\frac{S}{N}\right)
$$

$C$ 的单位是 bit/s，$W$ 为带宽（Hz），$S$ 为信道内所传信号的平均功率，$N$ 为高斯噪声功率，$S/N$ 为<strong>信噪比</strong>（无量纲比值）。

信噪比常用分贝表示，换算关系为：

$$
\left(\frac{S}{N}\right)_{dB}=10\lg\frac{S}{N}
$$

即 30 dB → $S/N=1000$，20 dB → 100，10 dB → 10。

<strong>结论</strong>：带宽越大、信噪比越高，信道的极限信息传输速率越大。实际可达速率比该极限<strong>低不少</strong>，因为信道中还有脉冲干扰、信号衰减与失真等香农公式未考虑的因素。

两个公式的分工：

| 公式 | 前提 | 限制的对象 | 提高速率的着力点 |
|---|---|---|---|
| <strong>奈氏准则</strong> | 理想（无噪声）信道 | <strong>码元传输速率</strong> | 多元制，让每码元携带更多比特 |
| <strong>香农公式</strong> | 带宽受限 + 高斯白噪声 | <strong>信息传输速率</strong> | 提高带宽与信噪比 |

<figure class="cpd-figure not-content" id="cn-w2-fig-11">
  <svg viewBox="0 0 720 336" role="img" aria-label="奈氏准则与香农公式双极限约束关系及信噪比dB换算速查图">
    <defs>
      <marker id="cn-w2-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="334" height="206" rx="6"/>
    <rect class="fig-box-subtle" x="370" y="16" width="334" height="206" rx="6"/>
    <rect class="fig-box-subtle" x="16" y="232" width="688" height="88" rx="6"/>
    <line class="fig-line" x1="183" y1="80" x2="183" y2="100" marker-end="url(#cn-w2-arr-11)"/>
    <line class="fig-line" x1="183" y1="142" x2="183" y2="160" marker-end="url(#cn-w2-arr-11)"/>
    <line class="fig-line" x1="537" y1="80" x2="537" y2="100" marker-end="url(#cn-w2-arr-11)"/>
    <line class="fig-line" x1="537" y1="142" x2="537" y2="160" marker-end="url(#cn-w2-arr-11)"/>
    <line class="fig-line-accent" x1="230" y1="212" x2="290" y2="244" stroke-width="1.6" marker-end="url(#cn-w2-arr-11)"/>
    <line class="fig-line-accent" x1="490" y1="212" x2="430" y2="244" stroke-width="1.6" marker-end="url(#cn-w2-arr-11)"/>
    <rect class="fig-box" x="32" y="42" width="302" height="38" rx="4"/>
    <rect class="fig-box-blue" x="32" y="102" width="302" height="40" rx="4"/>
    <rect class="fig-box" x="32" y="162" width="302" height="50" rx="4"/>
    <rect class="fig-box" x="386" y="42" width="302" height="38" rx="4"/>
    <rect class="fig-box-orange" x="386" y="102" width="302" height="40" rx="4"/>
    <rect class="fig-box" x="386" y="162" width="302" height="50" rx="4"/>
    <rect class="fig-box-green" x="144" y="244" width="432" height="44" rx="5"/>
    <rect class="fig-box-purple" x="26" y="244" width="108" height="64" rx="4"/>
    <rect class="fig-box-purple" x="586" y="244" width="108" height="64" rx="4"/>
    <text class="fig-title" x="183" y="34" font-size="12" text-anchor="middle">奈氏准则（防码间串扰上限）</text>
    <text class="fig-label" x="183" y="65" font-size="11" text-anchor="middle">前提：理想无噪声信道（带宽 W Hz）</text>
    <text class="fig-blue" x="183" y="120" font-size="11.5" font-weight="bold" text-anchor="middle">低通 ≤ 2W Baud ｜ 带通 ≤ W Baud</text>
    <text class="fig-muted" x="183" y="135" font-size="10" text-anchor="middle">直接限制：最高码元传输速率 R_B</text>
    <text class="fig-label" x="183" y="182" font-size="11" text-anchor="middle">对应比特率上限 = 2W · log2(N) bit/s</text>
    <text class="fig-muted" x="183" y="200" font-size="10" text-anchor="middle">提速途径：多元制调制（增大码元状态数 N）</text>
    <text class="fig-title" x="537" y="34" font-size="12" text-anchor="middle">香农公式（抗高斯白噪声上限）</text>
    <text class="fig-label" x="537" y="65" font-size="11" text-anchor="middle">前提：带宽受限 W + 高斯白噪声 S/N</text>
    <text class="fig-orange" x="537" y="120" font-size="11.5" font-weight="bold" text-anchor="middle">C = W · log2(1 + S/N) bit/s</text>
    <text class="fig-muted" x="537" y="135" font-size="10" text-anchor="middle">直接限制：极限无差错信息速率 C</text>
    <text class="fig-label" x="537" y="182" font-size="11" text-anchor="middle">N 不能无限增大（噪声使过密星座点不可分辨）</text>
    <text class="fig-muted" x="537" y="200" font-size="10" text-anchor="middle">提速途径：增大带宽 W 或提高信噪比 S/N</text>
    <text class="fig-green" x="360" y="263" font-size="11.5" font-weight="bold" text-anchor="middle">双公式同时给条件时取较小值：R_bmax = min{ 2W·log2(N),  W·log2(1 + S/N) }</text>
    <text class="fig-muted" x="360" y="280" font-size="10" text-anchor="middle">实际信道因脉冲干扰与衰减，可达码元速率远低于 2W，实际信息速率也远低于 C</text>
    <text class="fig-muted" x="360" y="308" font-size="10" text-anchor="middle">2017 真题联立求最少状态数：2W·log2(N) ≥ W·log2(1+1000) ⇒ log2(N) ≥ 4.98 ⇒ N ≥ 32</text>
    <text class="fig-purple" x="80" y="262" font-size="10.5" font-weight="bold" text-anchor="middle">dB 换算公式</text>
    <text class="fig-label" x="80" y="280" font-size="10" text-anchor="middle">dB = 10·lg(S/N)</text>
    <text class="fig-muted" x="80" y="298" font-size="9.5" text-anchor="middle">S/N = 10^(dB/10)</text>
    <text class="fig-purple" x="640" y="262" font-size="10.5" font-weight="bold" text-anchor="middle">常考数值速查</text>
    <text class="fig-label" x="640" y="279" font-size="10" text-anchor="middle">30 dB → 1000</text>
    <text class="fig-muted" x="640" y="294" font-size="9.5" text-anchor="middle">20dB→100 · 10dB→10</text>
    <text class="fig-muted" x="640" y="305" font-size="9" text-anchor="middle">log2(1001) ≈ 10</text>
  </svg>
  <figcaption>图 2-11：奈氏准则与香农公式双极限联合约束关系及信噪比 dB 换算速查</figcaption>
</figure>

在带宽一定的情况下，要提高信息传输速率，必须<strong>采用多元制并努力提高信噪比</strong>。香农公式发表后涌现的各种信号处理与调制方法，目标都是尽量逼近它给出的极限。

### 2.7 真题套路与例题

| 题目 | 考点 | 解法 | 答案 |
|---|---|---|---|
| 描述完成每种功能的事件发生顺序 | 接口四特性 | 事件顺序 → 过程特性 | C |
| 2018 不属于物理层接口规范的是 | 接口四特性 | 物理地址 / MAC 地址属数据链路层 | C |
| 2014 影响数据传输速率的因素 | 两公式综合 | 信噪比、带宽、调制速度都影响；<strong>信号传播速度不影响</strong> | D |
| 2013 由 10BASE-T 波形读比特串 | 曼彻斯特编码 | 按码元中间跳变划分，极性假设不符则逐位取反 | A |
| 2009 无噪声、4 相位 × 4 振幅、带宽 3 kHz | 奈氏准则 | $2\times3\text{k}=6$ kBaud；16 种码元 → 4 bit；$6\text{k}\times4=24$ kb/s | B |
| 2011 四相位调制、数据率 2400 b/s | 比特率↔波特率 | 4 种码元 → 2 bit；$2400=R_B\times2$ → $R_B=1200$ Baud | B |
| 2016 8 kHz、30 dB、实际为理论值 50% | 香农公式 | $8000\times\log_{2}1001\approx80$ kb/s，取 50% ≈ 40 kb/s | C |
| 2017 无噪声极限不低于 30 dB 噪声极限，求最少信号状态数 | 两公式联立 | $2W\log_{2}X\ge W\log_{2}1001$ → $\log_{2}X\ge4.98$ → $X\ge32$ | D |
| 2015 编码波形识别 | 常用编码 | 码元内电平不变、1/0 电平不同 → NRZ；中间时刻跳变 → 曼彻斯特 | A |

<strong>通用解题步骤</strong>：

1. 判断信道类型：看到“无噪声”“理想低通”用 $2W$，看到“带通”用 $W$。
2. 数出调制技术能产生的<strong>不同基本波形数 $N$</strong>，每码元比特数 $=\log_{2}N$。
3. 比特率 = 波特率 × 每码元比特数；题目给比特率反求波特率时做除法。
4. 看到 dB 先换算成比值：$S/N=10^{\,dB/10}$。
5. 两公式同时出现时，令两边的比特率相等或列不等式，解出未知量（通常是 $N$）。

> <strong>★考点</strong>：$2W$ 算出的是<strong>波特率</strong>而不是比特率，忘记乘 $\log_{2}N$ 是最高频的失分点。

> <strong>注意</strong>：“信号在传输媒体上的传播速度”与“数据传输速率”是两个概念：传播速率只影响<strong>传播时延</strong>，不影响数据传输速率，考两公式时不要把它当作影响因素。

<figure class="cpd-figure not-content" id="cn-w2-fig-12">
  <svg viewBox="0 0 720 264" role="img" aria-label="408物理层速率计算五步审题流程与易错陷阱总结">
    <defs>
      <marker id="cn-w2-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="232" rx="6"/>
    <line class="fig-line-accent" x1="158" y1="92" x2="180" y2="92" marker-end="url(#cn-w2-arr-12)"/>
    <line class="fig-line-accent" x1="336" y1="92" x2="358" y2="92" marker-end="url(#cn-w2-arr-12)"/>
    <line class="fig-line-accent" x1="518" y1="92" x2="540" y2="92" marker-end="url(#cn-w2-arr-12)"/>
    <rect class="fig-box-blue" x="28" y="50" width="130" height="84" rx="5"/>
    <rect class="fig-box-purple" x="182" y="50" width="154" height="84" rx="5"/>
    <rect class="fig-box-orange" x="360" y="50" width="158" height="84" rx="5"/>
    <rect class="fig-box-green" x="542" y="50" width="150" height="84" rx="5"/>
    <rect class="fig-box-red" x="28" y="150" width="328" height="84" rx="5"/>
    <rect class="fig-box" x="368" y="150" width="324" height="84" rx="5"/>
    <text class="fig-title" x="360" y="36" font-size="12" text-anchor="middle">408 物理层计算题四步闭环审题法与高频概念陷阱</text>
    <text class="fig-blue" x="93" y="70" font-size="11" font-weight="bold" text-anchor="middle">① 判信道与带宽</text>
    <text class="fig-label" x="93" y="90" font-size="10" text-anchor="middle">无上下限 → 低通 2W</text>
    <text class="fig-muted" x="93" y="108" font-size="9.5" text-anchor="middle">给 [f1,f2] → 带通 W</text>
    <text class="fig-muted" x="93" y="123" font-size="9.5" text-anchor="middle">(W = f2 - f1 Hz)</text>
    <text class="fig-purple" x="259" y="70" font-size="11" font-weight="bold" text-anchor="middle">② 算码元比特数 k</text>
    <text class="fig-label" x="259" y="90" font-size="10" text-anchor="middle">总状态数 N = 相位×振幅</text>
    <text class="fig-muted" x="259" y="108" font-size="9.5" text-anchor="middle">k = log2(N) bit/Baud</text>
    <text class="fig-muted" x="259" y="123" font-size="9.5" text-anchor="middle">二元调制 N=2 → k=1</text>
    <text class="fig-orange" x="439" y="70" font-size="11" font-weight="bold" text-anchor="middle">③ 换算信噪比 S/N</text>
    <text class="fig-label" x="439" y="90" font-size="10" text-anchor="middle">先看单位是否带 dB！</text>
    <text class="fig-muted" x="439" y="108" font-size="9.5" text-anchor="middle">带 dB 必转 S/N=10^(dB/10)</text>
    <text class="fig-muted" x="439" y="123" font-size="9.5" text-anchor="middle">无 dB 则直接代入比值</text>
    <text class="fig-green" x="617" y="70" font-size="11" font-weight="bold" text-anchor="middle">④ 求极值并核比率</text>
    <text class="fig-label" x="617" y="90" font-size="10" text-anchor="middle">R_b = R_B × log2(N)</text>
    <text class="fig-muted" x="617" y="108" font-size="9.5" text-anchor="middle">双条件取 min(奈氏,香农)</text>
    <text class="fig-muted" x="617" y="123" font-size="9.5" text-anchor="middle">注意“实际达理论 50%”</text>
    <text class="fig-red" x="192" y="171" font-size="11" font-weight="bold" text-anchor="middle">高频失分陷阱 A：波特率 ≠ 比特率</text>
    <text class="fig-label" x="192" y="191" font-size="10" text-anchor="middle">奈氏 2W 算出的是波特率（Baud），求比特率必乘 log2(N)！</text>
    <text class="fig-muted" x="192" y="209" font-size="9.5" text-anchor="middle">曼彻斯特编码相反：传 10 Mb/s 数据需 20 MBaud 调制速率</text>
    <text class="fig-muted" x="192" y="224" font-size="9.5" text-anchor="middle">反求波特率时做除法：R_B = R_b / log2(N)（如 2011 真题）</text>
    <text class="fig-blue" x="530" y="171" font-size="11" font-weight="bold" text-anchor="middle">高频失分陷阱 B：信号传播速度不影响数据率</text>
    <text class="fig-label" x="530" y="191" font-size="10" text-anchor="middle">电磁波传播速率 v（约 2×10^8 m/s）仅决定传播时延 d/v</text>
    <text class="fig-muted" x="530" y="209" font-size="9.5" text-anchor="middle">数据传输速率（发送速率）仅取决于 W、S/N 与编码调制阶数 N</text>
    <text class="fig-muted" x="530" y="224" font-size="9.5" text-anchor="middle">2014 真题：影响数据传输速率的因素不包含“信号传播速度”！</text>
  </svg>
  <figcaption>图 2-12：408 物理层速率计算四步审题法与“波特率 vs 比特率”“传播速率 vs 传输速率”辨析</figcaption>
</figure>

## 第3章 数据链路层

### 3.1 数据链路层概述

数据链路层位于网络层之下、物理层之上。在五层原理体系结构中，<strong>主机具有全部五层，路由器只需下面三层</strong>（物理层、数据链路层、网络层）。

主机 H1 向 H2 发送数据，中间经过三个路由器和电话网、局域网、广域网等多种网络。研究数据链路层时，可以只关心数据链路层：<strong>H1 到 H2 的通信可看成是 4 段不同链路上的通信组成的</strong>。

<figure class="cpd-figure not-content" id="cn-w3-fig-1">
  <svg viewBox="0 0 720 304" role="img" aria-label="H1到H2跨异构网络通信在数据链路层等价为4段相邻节点间链路的逐跳帧传输">
    <defs>
      <marker id="cn-w3-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="272" rx="8"/>
    <line class="fig-line" x1="92" y1="152" x2="178" y2="152"/>
    <line class="fig-line" x1="242" y1="152" x2="328" y2="152"/>
    <line class="fig-line" x1="392" y1="152" x2="478" y2="152"/>
    <line class="fig-line" x1="542" y1="152" x2="628" y2="152"/>
    <line class="fig-line-accent" x1="92" y1="132" x2="176" y2="132" marker-end="url(#cn-w3-arr-1)"/>
    <line class="fig-line-accent" x1="242" y1="132" x2="326" y2="132" marker-end="url(#cn-w3-arr-1)"/>
    <line class="fig-line-accent" x1="392" y1="132" x2="476" y2="132" marker-end="url(#cn-w3-arr-1)"/>
    <line class="fig-line-accent" x1="542" y1="132" x2="626" y2="132" marker-end="url(#cn-w3-arr-1)"/>
    <line class="fig-line-dashed" x1="135" y1="152" x2="135" y2="174"/>
    <line class="fig-line-dashed" x1="285" y1="152" x2="285" y2="174"/>
    <line class="fig-line-dashed" x1="435" y1="152" x2="435" y2="174"/>
    <line class="fig-line-dashed" x1="585" y1="152" x2="585" y2="174"/>
    <rect class="fig-box" x="28" y="62" width="64" height="20"/>
    <rect class="fig-box" x="28" y="82" width="64" height="20"/>
    <rect class="fig-box-purple" x="28" y="102" width="64" height="20"/>
    <rect class="fig-box-blue" x="28" y="122" width="64" height="20"/>
    <rect class="fig-box" x="28" y="142" width="64" height="20"/>
    <rect class="fig-box-purple" x="178" y="102" width="64" height="20"/>
    <rect class="fig-box-blue" x="178" y="122" width="64" height="20"/>
    <rect class="fig-box" x="178" y="142" width="64" height="20"/>
    <rect class="fig-box-purple" x="328" y="102" width="64" height="20"/>
    <rect class="fig-box-blue" x="328" y="122" width="64" height="20"/>
    <rect class="fig-box" x="328" y="142" width="64" height="20"/>
    <rect class="fig-box-purple" x="478" y="102" width="64" height="20"/>
    <rect class="fig-box-blue" x="478" y="122" width="64" height="20"/>
    <rect class="fig-box" x="478" y="142" width="64" height="20"/>
    <rect class="fig-box" x="628" y="62" width="64" height="20"/>
    <rect class="fig-box" x="628" y="82" width="64" height="20"/>
    <rect class="fig-box-purple" x="628" y="102" width="64" height="20"/>
    <rect class="fig-box-blue" x="628" y="122" width="64" height="20"/>
    <rect class="fig-box" x="628" y="142" width="64" height="20"/>
    <rect class="fig-box-blue" x="93" y="174" width="84" height="42" rx="5"/>
    <rect class="fig-box-green" x="243" y="174" width="84" height="42" rx="5"/>
    <rect class="fig-box-orange" x="393" y="174" width="84" height="42" rx="5"/>
    <rect class="fig-box-purple" x="543" y="174" width="84" height="42" rx="5"/>
    <rect class="fig-box" x="28" y="226" width="664" height="50" rx="6"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">H1 → H2 跨网络端到端通信 = 4 段相邻节点数据链路上的逐跳帧传输</text>
    <text class="fig-label" x="60" y="55" text-anchor="middle" font-size="11">主机 H1</text>
    <text class="fig-muted" x="60" y="76" text-anchor="middle" font-size="11">应用层</text>
    <text class="fig-muted" x="60" y="96" text-anchor="middle" font-size="11">运输层</text>
    <text class="fig-purple" x="60" y="116" text-anchor="middle" font-size="11">网络层</text>
    <text class="fig-blue" x="60" y="136" text-anchor="middle" font-size="11">链路层</text>
    <text class="fig-muted" x="60" y="156" text-anchor="middle" font-size="11">物理层</text>
    <text class="fig-label" x="210" y="95" text-anchor="middle" font-size="11">路由器 R1</text>
    <text class="fig-purple" x="210" y="116" text-anchor="middle" font-size="11">网络层</text>
    <text class="fig-blue" x="210" y="136" text-anchor="middle" font-size="11">链路层</text>
    <text class="fig-muted" x="210" y="156" text-anchor="middle" font-size="11">物理层</text>
    <text class="fig-label" x="360" y="95" text-anchor="middle" font-size="11">路由器 R2</text>
    <text class="fig-purple" x="360" y="116" text-anchor="middle" font-size="11">网络层</text>
    <text class="fig-blue" x="360" y="136" text-anchor="middle" font-size="11">链路层</text>
    <text class="fig-muted" x="360" y="156" text-anchor="middle" font-size="11">物理层</text>
    <text class="fig-label" x="510" y="95" text-anchor="middle" font-size="11">路由器 R3</text>
    <text class="fig-purple" x="510" y="116" text-anchor="middle" font-size="11">网络层</text>
    <text class="fig-blue" x="510" y="136" text-anchor="middle" font-size="11">链路层</text>
    <text class="fig-muted" x="510" y="156" text-anchor="middle" font-size="11">物理层</text>
    <text class="fig-label" x="660" y="55" text-anchor="middle" font-size="11">主机 H2</text>
    <text class="fig-muted" x="660" y="76" text-anchor="middle" font-size="11">应用层</text>
    <text class="fig-muted" x="660" y="96" text-anchor="middle" font-size="11">运输层</text>
    <text class="fig-purple" x="660" y="116" text-anchor="middle" font-size="11">网络层</text>
    <text class="fig-blue" x="660" y="136" text-anchor="middle" font-size="11">链路层</text>
    <text class="fig-muted" x="660" y="156" text-anchor="middle" font-size="11">物理层</text>
    <text class="fig-accent" x="135" y="125" text-anchor="middle" font-size="11">帧 F1</text>
    <text class="fig-accent" x="285" y="125" text-anchor="middle" font-size="11">帧 F2</text>
    <text class="fig-accent" x="435" y="125" text-anchor="middle" font-size="11">帧 F3</text>
    <text class="fig-accent" x="585" y="125" text-anchor="middle" font-size="11">帧 F4</text>
    <text class="fig-blue" x="135" y="191" text-anchor="middle" font-size="11">数据链路 1</text>
    <text class="fig-muted" x="135" y="208" text-anchor="middle" font-size="10">电话网 PPP</text>
    <text class="fig-green" x="285" y="191" text-anchor="middle" font-size="11">数据链路 2</text>
    <text class="fig-muted" x="285" y="208" text-anchor="middle" font-size="10">以太网帧</text>
    <text class="fig-orange" x="435" y="191" text-anchor="middle" font-size="11">数据链路 3</text>
    <text class="fig-muted" x="435" y="208" text-anchor="middle" font-size="10">广域网专线</text>
    <text class="fig-purple" x="585" y="191" text-anchor="middle" font-size="11">数据链路 4</text>
    <text class="fig-muted" x="585" y="208" text-anchor="middle" font-size="10">802.11 WLAN</text>
    <text class="fig-label" x="42" y="246" font-size="11">• 链路 = 相邻节点间无交换节点的物理线路；数据链路 = 物理链路 + 通信协议软硬件（传输单位：帧）</text>
    <text class="fig-muted" x="42" y="266" font-size="11">• 路由器只含下三层：每跳拆去旧帧首尾 → 查网络层转发表 → 按下一段链路协议重新封装新帧首尾</text>
  </svg>
  <figcaption>图 3-1：H1 到 H2 的通信由 4 段异构数据链路组成，各段可采用不同的帧格式与链路层协议逐跳重封装</figcaption>
</figure>

- <strong>链路</strong>：从一个节点到相邻节点的一段物理线路，中间没有任何交换节点。
- <strong>数据链路</strong>：在链路之上加上实现通信协议的硬件和软件。
- 数据链路层<strong>以帧为单位</strong>传输和处理数据。

<strong>三个基本问题</strong>：

1. <strong>封装成帧</strong>：把网络层交付的协议数据单元加上帧头（首部）和帧尾。加头加尾的目的是<strong>帧定界</strong>，使链路上以帧为单位传送数据。
2. <strong>差错检测</strong>：帧在传输中受干扰会产生<strong>误码</strong>（比特 0 变 1、1 变 0）。发送方根据待发送数据和检错算法算出<strong>检错码</strong>放在帧尾；接收方用同样的检错码和算法判断是否出错。以太网 V2 MAC 帧的帧尾就是 <strong>4 字节的帧检验序列 FCS</strong>。
3. <strong>可靠传输</strong>：检错后若只提供<strong>不可靠服务</strong>，出错的帧直接丢弃；若提供<strong>可靠服务</strong>，还要有重传等措施，使“发送方发送什么，接收方最终都能收到什么”。

<figure class="cpd-figure not-content" id="cn-w3-fig-2">
  <svg viewBox="0 0 720 318" role="img" aria-label="以太网V2 MAC帧格式与48位MAC地址单播多播广播结构解析">
    <defs>
      <marker id="cn-w3-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="286" rx="8"/>
    <path class="fig-line" d="M 28 108 L 28 117 L 140 117 L 140 108"/>
    <path class="fig-line-accent" d="M 140 108 L 140 117 L 396 117 L 396 108"/>
    <path class="fig-line-accent" d="M 608 108 L 608 117 L 692 117 L 692 108"/>
    <path class="fig-line" d="M 140 136 L 140 145 L 692 145 L 692 136"/>
    <line class="fig-line-dashed" x1="186" y1="102" x2="186" y2="172" marker-end="url(#cn-w3-arr-2)"/>
    <line class="fig-line-dashed" x1="360" y1="102" x2="520" y2="172" marker-end="url(#cn-w3-arr-2)"/>
    <rect class="fig-box-dashed" x="28" y="50" width="112" height="52" rx="4"/>
    <rect class="fig-box-blue" x="140" y="50" width="92" height="52"/>
    <rect class="fig-box-blue" x="232" y="50" width="92" height="52"/>
    <rect class="fig-box-purple" x="324" y="50" width="72" height="52"/>
    <rect class="fig-box-green" x="396" y="50" width="212" height="52"/>
    <rect class="fig-box-orange" x="608" y="50" width="84" height="52"/>
    <rect class="fig-box" x="28" y="172" width="416" height="116" rx="6"/>
    <rect class="fig-box-blue" x="42" y="198" width="196" height="36" rx="4"/>
    <rect class="fig-box-subtle" x="238" y="198" width="192" height="36" rx="4"/>
    <rect class="fig-box" x="456" y="172" width="236" height="116" rx="6"/>
    <rect class="fig-box" x="336" y="136" width="160" height="18" rx="3"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">以太网 V2 MAC 帧封装格式（物理层前同步码 + 64～1518 B 有效帧）</text>
    <text class="fig-muted" x="84" y="72" text-anchor="middle" font-size="11">前同步码 + SFD</text>
    <text class="fig-muted" x="84" y="90" text-anchor="middle" font-size="11">7B + 1B（物理层）</text>
    <text class="fig-blue" x="186" y="72" text-anchor="middle" font-size="12">目的地址</text>
    <text class="fig-muted" x="186" y="90" text-anchor="middle" font-size="11">6 字节</text>
    <text class="fig-blue" x="278" y="72" text-anchor="middle" font-size="12">源地址</text>
    <text class="fig-muted" x="278" y="90" text-anchor="middle" font-size="11">6 字节</text>
    <text class="fig-purple" x="360" y="72" text-anchor="middle" font-size="12">类型</text>
    <text class="fig-muted" x="360" y="90" text-anchor="middle" font-size="11">2 字节</text>
    <text class="fig-green" x="502" y="72" text-anchor="middle" font-size="12">数据部分（上层 PDU）</text>
    <text class="fig-muted" x="502" y="90" text-anchor="middle" font-size="11">46～1500 B（不足 46B 填充）</text>
    <text class="fig-orange" x="650" y="72" text-anchor="middle" font-size="12">FCS</text>
    <text class="fig-muted" x="650" y="90" text-anchor="middle" font-size="11">4 字节 CRC</text>
    <text class="fig-muted" x="84" y="131" text-anchor="middle" font-size="11">不计入 MAC 帧长</text>
    <text class="fig-accent" x="268" y="131" text-anchor="middle" font-size="11">帧首部（14 字节）</text>
    <text class="fig-accent" x="650" y="131" text-anchor="middle" font-size="11">帧尾（4 B）</text>
    <text class="fig-label" x="416" y="149" text-anchor="middle" font-size="11">MAC 帧总长 64～1518 B</text>
    <text class="fig-label" x="42" y="190" font-size="11">48 位 MAC 地址（6 字节）结构与单播/多播/广播判别：</text>
    <text class="fig-blue" x="140" y="214" text-anchor="middle" font-size="11">高 24 位：OUI（机构唯一标识符）</text>
    <text class="fig-muted" x="140" y="228" text-anchor="middle" font-size="10">IEEE 分配给网卡厂商</text>
    <text class="fig-label" x="334" y="214" text-anchor="middle" font-size="11">低 24 位：扩展唯一标识符</text>
    <text class="fig-muted" x="334" y="228" text-anchor="middle" font-size="10">厂商自行指派（全球唯一）</text>
    <text class="fig-label" x="42" y="253" font-size="11">• 第 1 字节最低位 I/G 位：0 = 单播地址，1 = 多播（组播）地址</text>
    <text class="fig-orange" x="42" y="273" font-size="11">• 全 1 地址 FF-FF-FF-FF-FF-FF = 广播地址（源地址只能是单播！）</text>
    <text class="fig-purple" x="468" y="192" font-size="11">类型字段（上层协议分用）：</text>
    <text class="fig-label" x="468" y="212" font-size="11">• 0x0800 → IPv4 数据报</text>
    <text class="fig-label" x="468" y="230" font-size="11">• 0x0806 → ARP 请求/应答分组</text>
    <text class="fig-label" x="468" y="248" font-size="11">• 0x8100 → 802.1Q VLAN 标签</text>
    <text class="fig-muted" x="468" y="270" font-size="11">注：帧间最小间隔 9.6 μs 即定界</text>
  </svg>
  <figcaption>图 3-2：以太网 V2 MAC 帧结构、物理层 8 字节前同步码边界与 48 位 MAC 地址“I/G”位单播/多播/广播判据</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w3-fig-3">
  <svg viewBox="0 0 720 316" role="img" aria-label="CRC循环冗余检验模2除法计算实例与海明距离检错纠错几何判据">
    <defs>
      <marker id="cn-w3-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="332" height="240" rx="6"/>
    <rect class="fig-box" x="372" y="48" width="320" height="240" rx="6"/>
    <rect class="fig-box-blue" x="42" y="76" width="304" height="30" rx="4"/>
    <line class="fig-line" x1="112" y1="136" x2="296" y2="136"/>
    <line class="fig-line" x1="112" y1="136" x2="104" y2="154"/>
    <line class="fig-divider" x1="110" y1="170" x2="240" y2="170"/>
    <line class="fig-divider" x1="142" y1="204" x2="288" y2="204"/>
    <rect class="fig-box-orange" x="228" y="210" width="96" height="24" rx="4"/>
    <rect class="fig-box-green" x="42" y="244" width="304" height="32" rx="4"/>
    <line class="fig-line-dashed" x1="436" y1="116" x2="628" y2="116"/>
    <circle class="fig-box-dashed" cx="436" cy="116" r="28"/>
    <circle class="fig-box-blue" cx="436" cy="116" r="13"/>
    <circle class="fig-box-blue" cx="628" cy="116" r="13"/>
    <circle class="fig-box-red" cx="464" cy="116" r="5"/>
    <line class="fig-line-dashed" x1="436" y1="194" x2="628" y2="194"/>
    <circle class="fig-box-green" cx="436" cy="194" r="34"/>
    <circle class="fig-box-green" cx="628" cy="194" r="34"/>
    <circle class="fig-box-blue" cx="436" cy="194" r="12"/>
    <circle class="fig-box-blue" cx="628" cy="194" r="12"/>
    <line class="fig-line-accent" x1="470" y1="194" x2="452" y2="194" marker-end="url(#cn-w3-arr-3)"/>
    <rect class="fig-box-purple" x="386" y="244" width="292" height="32" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">差错控制核心原理：CRC 模 2 除法检验与最小海明距离 d_min 判据</text>
    <text class="fig-blue" x="194" y="67" text-anchor="middle" font-size="12">① CRC 循环冗余检验（异或模 2 除，不进位不借位）</text>
    <text class="fig-label" x="194" y="95" text-anchor="middle" font-size="11">M = 101001，G(x) = x³+x²+1 ⇒ P = 1101（阶数 r=3）</text>
    <text class="fig-muted" x="198" y="129" text-anchor="middle" font-size="11">商 Q = 1 1 0 1 0 1（仅看最高位够不够异或）</text>
    <text class="fig-purple" x="74" y="152" text-anchor="middle" font-size="11">1101</text>
    <text class="fig-label" x="204" y="152" text-anchor="middle" font-size="11">1 0 1 0 0 1 0 0 0（补 r=3 个 0）</text>
    <text class="fig-muted" x="150" y="166" text-anchor="middle" font-size="11">⊕ 1 1 0 1</text>
    <text class="fig-label" x="196" y="184" text-anchor="middle" font-size="11">0 1 1 1 0 → 连续模 2 异或消去高位…</text>
    <text class="fig-muted" x="218" y="199" text-anchor="middle" font-size="11">… 1 1 0 0 ⊕ 1 1 0 1</text>
    <text class="fig-orange" x="276" y="226" text-anchor="middle" font-size="11">余数 R = 001</text>
    <text class="fig-muted" x="128" y="226" text-anchor="middle" font-size="11">（r=3 位 FCS）</text>
    <text class="fig-green" x="194" y="264" text-anchor="middle" font-size="11">发送帧：101001 001；收端模 2 除 P 余 0 则无误码</text>
    <text class="fig-purple" x="532" y="67" text-anchor="middle" font-size="12">② 最小海明码距 d_min 检错与纠错能力</text>
    <text class="fig-label" x="532" y="84" text-anchor="middle" font-size="11">检出 k 位错：d_min ≥ k + 1（错码落入非法空白区）</text>
    <text class="fig-blue" x="436" y="120" text-anchor="middle" font-size="10">C1</text>
    <text class="fig-blue" x="628" y="120" text-anchor="middle" font-size="10">C2</text>
    <text class="fig-red" x="506" y="109" font-size="10">≤k 位错（非法码）</text>
    <text class="fig-muted" x="532" y="134" text-anchor="middle" font-size="10">码距 d_min ≥ k+1（不会误变成 C2）</text>
    <text class="fig-label" x="532" y="156" text-anchor="middle" font-size="11">纠正 t 位错：d_min ≥ 2t + 1（两纠错球互不重叠）</text>
    <text class="fig-blue" x="436" y="198" text-anchor="middle" font-size="10">C1</text>
    <text class="fig-blue" x="628" y="198" text-anchor="middle" font-size="10">C2</text>
    <text class="fig-green" x="436" y="239" text-anchor="middle" font-size="10">半径 t 纠错球</text>
    <text class="fig-green" x="628" y="239" text-anchor="middle" font-size="10">半径 t 纠错球</text>
    <text class="fig-muted" x="532" y="210" text-anchor="middle" font-size="10">最近邻唯一译回原码字</text>
    <text class="fig-purple" x="532" y="264" text-anchor="middle" font-size="11">海明码纠 1 位错（d=3）：满足 2^r ≥ m + r + 1</text>
  </svg>
  <figcaption>图 3-3：CRC 模 2 除法求 FCS 实例与编码最小海明距离检错（d_min ≥ k+1）、纠错（d_min ≥ 2t+1）几何解释</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w3-fig-4">
  <svg viewBox="0 0 720 332" role="img" aria-label="停止等待协议、后退N帧GBN与选择重传SR滑动窗口时序及序号回绕约束对比">
    <defs>
      <marker id="cn-w3-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="300" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="208" height="200" rx="6"/>
    <rect class="fig-box" x="248" y="48" width="216" height="200" rx="6"/>
    <rect class="fig-box" x="476" y="48" width="216" height="200" rx="6"/>
    <line class="fig-line" x1="62" y1="92" x2="62" y2="236"/>
    <line class="fig-line" x1="202" y1="92" x2="202" y2="236"/>
    <line class="fig-line-accent" x1="62" y1="102" x2="200" y2="122" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="202" y1="128" x2="64" y2="148" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" x1="62" y1="158" x2="144" y2="172"/>
    <line class="fig-line-accent" x1="62" y1="196" x2="200" y2="216" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="202" y1="220" x2="64" y2="236" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" x1="282" y1="92" x2="282" y2="236"/>
    <line class="fig-line" x1="430" y1="92" x2="430" y2="236"/>
    <line class="fig-line-accent" x1="282" y1="100" x2="428" y2="118" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" x1="282" y1="116" x2="364" y2="128"/>
    <line class="fig-line-dashed" x1="282" y1="132" x2="428" y2="150" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="430" y1="122" x2="284" y2="144" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line-accent" x1="282" y1="182" x2="428" y2="200" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line-accent" x1="282" y1="198" x2="428" y2="216" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="430" y1="220" x2="284" y2="236" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" x1="510" y1="92" x2="510" y2="236"/>
    <line class="fig-line" x1="658" y1="92" x2="658" y2="236"/>
    <line class="fig-line-accent" x1="510" y1="100" x2="656" y2="118" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" x1="510" y1="118" x2="590" y2="130"/>
    <line class="fig-line-accent" x1="510" y1="136" x2="656" y2="154" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="658" y1="158" x2="512" y2="176" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="510" y1="194" x2="656" y2="212" marker-end="url(#cn-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="658" y1="216" x2="512" y2="234" marker-end="url(#cn-w3-arr-4)"/>
    <rect class="fig-box-orange" x="28" y="256" width="664" height="48" rx="6"/>
    <rect class="fig-box" x="108" y="134" width="48" height="14" rx="3"/>
    <rect class="fig-box" x="120" y="223" width="48" height="14" rx="3"/>
    <rect class="fig-box" x="38" y="173" width="32" height="14" rx="3"/>
    <rect class="fig-box" x="260" y="163" width="32" height="14" rx="3"/>
    <rect class="fig-box" x="374" y="121" width="36" height="14" rx="3"/>
    <rect class="fig-box" x="316" y="132" width="40" height="14" rx="3"/>
    <rect class="fig-box" x="304" y="176" width="104" height="14" rx="3"/>
    <rect class="fig-box" x="330" y="223" width="72" height="14" rx="3"/>
    <rect class="fig-box" x="548" y="164" width="72" height="14" rx="3"/>
    <rect class="fig-box" x="540" y="221" width="108" height="14" rx="3"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">可靠传输三类 ARQ 滑动窗口协议对比：停止-等待、GBN 与 SR</text>
    <text class="fig-blue" x="132" y="66" text-anchor="middle" font-size="12">停止-等待 (S&amp;W)</text>
    <text class="fig-muted" x="132" y="82" text-anchor="middle" font-size="10">W_T = 1，W_R = 1（1 bit 序号）</text>
    <text class="fig-muted" x="46" y="98" text-anchor="middle" font-size="10">发</text>
    <text class="fig-muted" x="218" y="98" text-anchor="middle" font-size="10">收</text>
    <text class="fig-accent" x="132" y="108" text-anchor="middle" font-size="10">F0</text>
    <text class="fig-green" x="132" y="145" text-anchor="middle" font-size="10">ACK 0</text>
    <text class="fig-red" x="154" y="176" font-size="10">F1 丢失 ×</text>
    <text class="fig-orange" x="44" y="184" font-size="9">超时</text>
    <text class="fig-accent" x="132" y="203" text-anchor="middle" font-size="10">重传 F1</text>
    <text class="fig-green" x="144" y="234" text-anchor="middle" font-size="10">ACK 1</text>
    <text class="fig-blue" x="356" y="66" text-anchor="middle" font-size="12">后退 N 帧 (GBN)</text>
    <text class="fig-muted" x="356" y="82" text-anchor="middle" font-size="10">1 &lt; W_T ≤ 2^n − 1，W_R = 1</text>
    <text class="fig-accent" x="356" y="105" text-anchor="middle" font-size="10">F0</text>
    <text class="fig-red" x="380" y="132" font-size="10">F1 ×</text>
    <text class="fig-muted" x="388" y="160" text-anchor="middle" font-size="9">F2 失序丢弃</text>
    <text class="fig-green" x="336" y="143" text-anchor="middle" font-size="9">ACK0</text>
    <text class="fig-orange" x="266" y="174" font-size="9">超时</text>
    <text class="fig-accent" x="356" y="187" text-anchor="middle" font-size="10">回退重传 F1, F2…</text>
    <text class="fig-green" x="366" y="234" text-anchor="middle" font-size="10">累积 ACK 2</text>
    <text class="fig-blue" x="584" y="66" text-anchor="middle" font-size="12">选择重传 (SR)</text>
    <text class="fig-muted" x="584" y="82" text-anchor="middle" font-size="10">W_T + W_R ≤ 2^n（常取 2^(n−1)）</text>
    <text class="fig-accent" x="584" y="105" text-anchor="middle" font-size="10">F0</text>
    <text class="fig-red" x="606" y="134" font-size="10">F1 ×</text>
    <text class="fig-accent" x="594" y="143" text-anchor="middle" font-size="10">F2 缓存</text>
    <text class="fig-green" x="584" y="175" text-anchor="middle" font-size="10">逐帧 ACK 2</text>
    <text class="fig-orange" x="584" y="200" text-anchor="middle" font-size="10">仅重传 F1</text>
    <text class="fig-green" x="594" y="232" text-anchor="middle" font-size="10">ACK 1（齐备交付）</text>
    <text class="fig-label" x="42" y="275" font-size="10">• 防序号回绕歧义：若 n 位序号取满 2^n 窗口且 ACK 全丢，重传的旧 0 号帧会被滑入下一轮的接收窗口误判为新 0 号帧！</text>
    <text class="fig-orange" x="42" y="294" font-size="10">• 408 必背公式：一般滑动窗口 W_T + W_R ≤ 2^n；GBN 因 W_R=1 故 W_T ≤ 2^n−1；SR 取对称最大窗口 W_T = W_R = 2^(n−1)</text>
  </svg>
  <figcaption>图 3-4：停止-等待、GBN（累积确认、失序丢弃回退重传）与 SR（逐帧确认、乱序缓存选择重传）对比及窗口上界约束</figcaption>
</figure>

> <strong>注意</strong>：上面三个问题是用<strong>点对点信道</strong>的数据链路层举例的；广播信道还有额外问题。

### 3.2 广播信道带来的两个问题

多台主机通过一根总线互联时，主机 A 发给 C 的帧会沿总线传到总线上的每一台主机。由此产生两个问题：

1. <strong>编址</strong>：B、D 如何知道收到的帧不是给自己的？C 如何知道帧是给自己的？做法是把<strong>目的地址写进帧头。以太网 V2 MAC 帧首部三个字段中有两个与地址有关：目的地址字段、源地址字段</strong>。
2. <strong>碰撞</strong>：总线上多台主机同时发送，信号在共享媒体上叠加，产生<strong>碰撞（冲突）</strong>，各站发送均失败。这是广播式共享局域网不可避免的，需要<strong>媒体接入控制</strong>来协调。

以太网采用 <strong>CSMA/CD</strong>（载波监听多点接入/碰撞检测）。随着交换技术成熟和成本下降，使用<strong>点对点链路 + 链路层交换机</strong>的交换式局域网在有线领域已完全取代共享式局域网。但无线信道具有广播天性，无线局域网仍使用共享信道，802.11 采用的媒体接入控制协议是 <strong>CSMA/CA</strong>（载波监听多点接入/碰撞避免）。

<figure class="cpd-figure not-content" id="cn-w3-fig-5">
  <svg viewBox="0 0 720 328" role="img" aria-label="CSMA/CD协议争用期2τ时空碰撞几何推导与64字节最短帧长计算">
    <defs>
      <marker id="cn-w3-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="296" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="368" height="248" rx="6"/>
    <rect class="fig-box" x="408" y="48" width="284" height="248" rx="6"/>
    <line class="fig-line" x1="102" y1="86" x2="102" y2="282"/>
    <line class="fig-line" x1="346" y1="86" x2="346" y2="282"/>
    <line class="fig-line-dashed" x1="102" y1="86" x2="346" y2="86"/>
    <line class="fig-line-dashed" x1="102" y1="166" x2="346" y2="166"/>
    <line class="fig-line-dashed" x1="102" y1="226" x2="346" y2="226"/>
    <line class="fig-line-accent" x1="102" y1="98" x2="318" y2="160" marker-end="url(#cn-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="346" y1="152" x2="324" y2="159" marker-end="url(#cn-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="5 3" x1="318" y1="160" x2="104" y2="224" marker-end="url(#cn-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="5 3" x1="318" y1="160" x2="344" y2="168" marker-end="url(#cn-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="66" y1="98" x2="66" y2="224" marker-start="url(#cn-w3-arr-5)" marker-end="url(#cn-w3-arr-5)"/>
    <rect class="fig-box-blue" x="94" y="98" width="16" height="128" rx="2"/>
    <rect class="fig-box-red" x="94" y="226" width="16" height="28" rx="2"/>
    <circle class="fig-box-red" cx="318" cy="160" r="8"/>
    <rect class="fig-box" x="40" y="150" width="52" height="22" rx="3"/>
    <rect class="fig-box-blue" x="422" y="76" width="256" height="54" rx="5"/>
    <rect class="fig-box-orange" x="422" y="140" width="256" height="80" rx="5"/>
    <rect class="fig-box-green" x="422" y="230" width="256" height="54" rx="5"/>
    <rect class="fig-box" x="116" y="95" width="122" height="14" rx="3"/>
    <rect class="fig-box" x="190" y="135" width="132" height="14" rx="3"/>
    <rect class="fig-box" x="220" y="168" width="116" height="14" rx="3"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">CSMA/CD 争用期（碰撞窗口 2τ）时空几何图与最短帧长推导</text>
    <text class="fig-blue" x="102" y="74" text-anchor="middle" font-size="11">站 A (x=0)</text>
    <text class="fig-muted" x="224" y="80" text-anchor="middle" font-size="10">单程端到端传播时延 τ</text>
    <text class="fig-orange" x="346" y="74" text-anchor="middle" font-size="11">站 B (x=d)</text>
    <text class="fig-muted" x="120" y="106" font-size="10">t = 0：A 监听空闲发帧</text>
    <text class="fig-orange" x="256" y="146" text-anchor="middle" font-size="10">t = τ−δ：B 误判空闲发帧</text>
    <text class="fig-red" x="278" y="179" text-anchor="middle" font-size="10">⚡ 碰撞点 t = τ−δ/2</text>
    <text class="fig-red" x="224" y="213" text-anchor="middle" font-size="10">碰撞波传回 A（t → 2τ）</text>
    <text class="fig-purple" x="66" y="165" text-anchor="middle" font-size="10">2τ</text>
    <text class="fig-red" x="118" y="244" font-size="10">停发数据 + 发强化碰撞信号 Jam</text>
    <text class="fig-muted" x="118" y="272" font-size="10">时间轴 ↓（δ→0 时最晚 2τ 检出碰撞）</text>
    <text class="fig-blue" x="550" y="96" text-anchor="middle" font-size="11">16 字口诀：先听后发、边发边听</text>
    <text class="fig-label" x="550" y="116" text-anchor="middle" font-size="11">冲突停发（半双工！）、随机延迟后重发</text>
    <text class="fig-orange" x="550" y="159" text-anchor="middle" font-size="11">最短帧长条件：帧发送时延 ≥ 争用期 2τ</text>
    <text class="fig-label" x="550" y="181" text-anchor="middle" font-size="11">L_min / R ≥ 2τ  ⇒  L_min = 2τ · R</text>
    <text class="fig-muted" x="550" y="203" text-anchor="middle" font-size="10">10 Mb/s 以太网 2τ=51.2 μs ⇒ L_min = 512 b = 64 B</text>
    <text class="fig-green" x="550" y="250" text-anchor="middle" font-size="11">• 长度 &lt; 64 B 必为碰撞残片，收端直接丢弃</text>
    <text class="fig-muted" x="550" y="270" text-anchor="middle" font-size="10">• 发送满 64 B 未冲突，后续必占稳总线不再冲突</text>
  </svg>
  <figcaption>图 3-5：共享总线最远两端站点碰撞时空图——最坏经端到端往返时延 2τ 检出冲突，由此决定以太网 64 字节最短帧长</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w3-fig-6">
  <svg viewBox="0 0 720 296" role="img" aria-label="CSMA/CD截断二进制指数退避算法流程与共享总线对比交换式以太网">
    <defs>
      <marker id="cn-w3-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="264" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="356" height="218" rx="6"/>
    <rect class="fig-box" x="396" y="48" width="296" height="218" rx="6"/>
    <line class="fig-line-accent" x1="142" y1="104" x2="142" y2="122" marker-end="url(#cn-w3-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="224" y1="90" x2="262" y2="90" marker-end="url(#cn-w3-arr-6)"/>
    <line class="fig-line-accent" x1="142" y1="158" x2="142" y2="176" marker-end="url(#cn-w3-arr-6)"/>
    <rect class="fig-box-orange" x="44" y="74" width="180" height="30" rx="4"/>
    <rect class="fig-box-red" x="264" y="74" width="106" height="30" rx="4"/>
    <rect class="fig-box-blue" x="44" y="124" width="326" height="34" rx="4"/>
    <rect class="fig-box-purple" x="44" y="178" width="326" height="74" rx="4"/>
    <rect class="fig-box-red" x="410" y="76" width="268" height="82" rx="5"/>
    <rect class="fig-box-green" x="410" y="170" width="268" height="82" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">截断二进制指数退避算法与有线局域网演进对比</text>
    <text class="fig-orange" x="206" y="66" text-anchor="middle" font-size="12">① CSMA/CD 截断二进制指数退避</text>
    <text class="fig-label" x="134" y="93" text-anchor="middle" font-size="11">重传次数 n ← n + 1</text>
    <text class="fig-red" x="317" y="93" text-anchor="middle" font-size="10">n=16 丢弃报错</text>
    <text class="fig-blue" x="207" y="145" text-anchor="middle" font-size="11">取 k = min(n, 10)，随机抽 r ∈ {0, 1, …, 2^k − 1}</text>
    <text class="fig-purple" x="207" y="198" text-anchor="middle" font-size="11">退避时长 = r × 争用期 2τ（r × 51.2 μs）</text>
    <text class="fig-muted" x="207" y="218" text-anchor="middle" font-size="10">例：n=1 抽 {0,1}；n=3 抽 {0..7}；n≥10 封顶 {0..1023}</text>
    <text class="fig-muted" x="207" y="238" text-anchor="middle" font-size="10">动态适应负载：冲突越多窗口越宽，成功发完一帧重置 n=0</text>
    <text class="fig-label" x="544" y="66" text-anchor="middle" font-size="12">② 共享式 vs 交换式以太网</text>
    <text class="fig-red" x="544" y="97" text-anchor="middle" font-size="11">同轴总线 / 集线器 Hub（共享信道）</text>
    <text class="fig-muted" x="544" y="117" text-anchor="middle" font-size="10">逻辑总线广播、只能半双工通信</text>
    <text class="fig-label" x="544" y="137" text-anchor="middle" font-size="10">全网处同一冲突域 → 必须运行 CSMA/CD</text>
    <text class="fig-green" x="544" y="191" text-anchor="middle" font-size="11">以太网交换机 Switch（点对点专享）</text>
    <text class="fig-muted" x="544" y="211" text-anchor="middle" font-size="10">每端口隔离独立冲突域、支持全双工同时收发</text>
    <text class="fig-label" x="544" y="231" text-anchor="middle" font-size="10">无碰撞问题 → 全双工模式下停用 CSMA/CD</text>
  </svg>
  <figcaption>图 3-6：CSMA/CD 截断二进制指数退避窗口计算规则（k = min(n,10)，16 次上限）及共享式与交换式以太网对比</figcaption>
</figure>

> <strong>★考点</strong>：有线共享式以太网用 CSMA/CD，无线局域网用 CSMA/CA；交换式以太网用交换机转发帧，不再有碰撞问题。

### 3.3 点对点协议 PPP 的组成

PPP 是目前使用最广泛的点对点数据链路层协议。

- <strong>应用场景：一般用户通过 ISP（如中国电信、中国联通、中国移动）接入因特网。ISP 从因特网管理机构申请到一批 IP 地址，用户计算机获得 ISP 分配的合法 IP 地址后才成为因特网上的主机；用户计算机与 ISP 通信所用的数据链路层协议通常就是 PPP</strong>。
- <strong>PPPoE：1999 年公布的“在以太网上运行的 PPP”（PPP over Ethernet），使 ISP 能通过 DSL、调制解调器、以太网等宽带接入技术，以以太网接口的形式</strong>为用户提供接入服务。
- PPP 也广泛用于<strong>广域网路由器之间的专用线路</strong>。
- 由 IETF 于 1992 年制定，1993、1994 年修订，现为因特网正式标准（见相应 RFC 文档）。

<strong>PPP 由三部分构成</strong>：

| 组成 | 作用 |
|---|---|
| 封装方法 | 把各种协议数据报封装成帧（封装成帧） |
| 链路控制协议 LCP | 建立、配置、测试数据链路的连接 |
| 网络控制协议 NCP（一套） | 每个 NCP 支持一种不同的网络层协议（IP、IPX、AppleTalk 等） |

PPP 能在多种类型的点对点链路上运行：<strong>面向字节的异步链路</strong>、<strong>面向比特的同步链路</strong>。

<figure class="cpd-figure not-content" id="cn-w3-fig-7">
  <svg viewBox="0 0 720 276" role="img" aria-label="点对点协议PPP三大组成部分LCP、NCP与封装方法架构及PPPoE宽带接入封装">
    <defs>
      <marker id="cn-w3-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="244" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="406" height="196" rx="6"/>
    <rect class="fig-box" x="446" y="48" width="246" height="196" rx="6"/>
    <line class="fig-line-accent" x1="142" y1="100" x2="142" y2="116" marker-end="url(#cn-w3-arr-7)"/>
    <line class="fig-line-accent" x1="142" y1="154" x2="142" y2="170" marker-end="url(#cn-w3-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="334" y1="154" x2="334" y2="170" marker-end="url(#cn-w3-arr-7)"/>
    <line class="fig-line" x1="231" y1="204" x2="231" y2="216" marker-end="url(#cn-w3-arr-7)"/>
    <rect class="fig-box-subtle" x="42" y="72" width="200" height="28" rx="4"/>
    <rect class="fig-box-purple" x="42" y="118" width="200" height="36" rx="4"/>
    <rect class="fig-box-orange" x="252" y="96" width="168" height="58" rx="4"/>
    <rect class="fig-box-blue" x="42" y="172" width="378" height="32" rx="4"/>
    <rect class="fig-box" x="42" y="216" width="378" height="20" rx="3"/>
    <rect class="fig-box-blue" x="460" y="90" width="56" height="36" rx="3"/>
    <rect class="fig-box-purple" x="516" y="90" width="54" height="36" rx="3"/>
    <rect class="fig-box-green" x="570" y="90" width="72" height="36" rx="3"/>
    <rect class="fig-box-orange" x="642" y="90" width="36" height="36" rx="3"/>
    <rect class="fig-box-subtle" x="460" y="140" width="218" height="90" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">PPP 协议三大组成部分架构与宽带接入 PPPoE 嵌套封装</text>
    <text class="fig-label" x="231" y="64" text-anchor="middle" font-size="11">PPP 三大核心组件（数据链路层）</text>
    <text class="fig-muted" x="142" y="90" text-anchor="middle" font-size="10">多种网络层分组（IP / IPX 等）</text>
    <text class="fig-purple" x="142" y="134" text-anchor="middle" font-size="11">③ 网络控制协议族 NCP</text>
    <text class="fig-muted" x="142" y="148" text-anchor="middle" font-size="9">如 IPCP：协商并动态分配 IP 地址</text>
    <text class="fig-orange" x="336" y="116" text-anchor="middle" font-size="11">② 链路控制协议 LCP</text>
    <text class="fig-muted" x="336" y="133" text-anchor="middle" font-size="9">建立/配置/测试链路</text>
    <text class="fig-muted" x="336" y="146" text-anchor="middle" font-size="9">协商最大帧长与 PAP/CHAP</text>
    <text class="fig-blue" x="231" y="192" text-anchor="middle" font-size="11">① 封装成帧方法（支持异步面向字节 / 同步面向比特链路）</text>
    <text class="fig-muted" x="231" y="230" text-anchor="middle" font-size="10">物理层点对点全双工链路（拨号 / 光纤 / 广域网路由器专线）</text>
    <text class="fig-label" x="569" y="68" text-anchor="middle" font-size="11">PPPoE（以太网承载 PPP）</text>
    <text class="fig-blue" x="488" y="106" text-anchor="middle" font-size="10">MAC头</text>
    <text class="fig-muted" x="488" y="120" text-anchor="middle" font-size="9">14 B</text>
    <text class="fig-purple" x="543" y="106" text-anchor="middle" font-size="10">PPPoE</text>
    <text class="fig-muted" x="543" y="120" text-anchor="middle" font-size="9">6 B</text>
    <text class="fig-green" x="606" y="106" text-anchor="middle" font-size="10">PPP 帧</text>
    <text class="fig-muted" x="606" y="120" text-anchor="middle" font-size="9">含 IP 包</text>
    <text class="fig-orange" x="660" y="106" text-anchor="middle" font-size="10">FCS</text>
    <text class="fig-muted" x="660" y="120" text-anchor="middle" font-size="9">4 B</text>
    <text class="fig-label" x="569" y="160" text-anchor="middle" font-size="10">为什么以太网还要套 PPP？</text>
    <text class="fig-muted" x="569" y="180" text-anchor="middle" font-size="10">传统以太网无用户名口令鉴别；</text>
    <text class="fig-muted" x="569" y="198" text-anchor="middle" font-size="10">借助 PPP 的 LCP 认证计费 +</text>
    <text class="fig-muted" x="569" y="216" text-anchor="middle" font-size="10">NCP 给家庭宽带分配公网 IP</text>
  </svg>
  <figcaption>图 3-7：PPP 协议三大部分（封装成帧、LCP、NCP 族）协作模型与家庭宽带 PPPoE 二次封装结构</figcaption>
</figure>

> <strong>注意</strong>：PPP 是数据链路层协议，它把上层交付的 PDU 封装成 PPP 帧；为支持不同网络层协议，才需要一族 NCP。

### 3.4 PPP 帧格式与透明传输

帧首部 4 个字段，帧尾部 2 个字段：

<figure class="cpd-figure not-content" id="cn-w3-fig-8">
  <svg viewBox="0 0 720 268" role="img" aria-label="PPP帧首部4字段、可变长信息字段与帧尾部2字段格式详解">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="236" rx="8"/>
    <path class="fig-line-accent" d="M 28 110 L 28 120 L 324 120 L 324 110"/>
    <path class="fig-line-accent" d="M 548 110 L 548 120 L 692 120 L 692 110"/>
    <rect class="fig-box-orange" x="28" y="52" width="68" height="52"/>
    <rect class="fig-box" x="96" y="52" width="68" height="52"/>
    <rect class="fig-box" x="164" y="52" width="68" height="52"/>
    <rect class="fig-box-purple" x="232" y="52" width="92" height="52"/>
    <rect class="fig-box-green" x="324" y="52" width="224" height="52"/>
    <rect class="fig-box-blue" x="548" y="52" width="76" height="52"/>
    <rect class="fig-box-orange" x="624" y="52" width="68" height="52"/>
    <rect class="fig-box" x="28" y="150" width="330" height="88" rx="6"/>
    <rect class="fig-box" x="370" y="150" width="322" height="88" rx="6"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">PPP 帧格式（首部 4 字段 + 信息部分 + 尾部 2 字段，面向字节对齐）</text>
    <text class="fig-orange" x="62" y="74" text-anchor="middle" font-size="12">标志 F</text>
    <text class="fig-muted" x="62" y="92" text-anchor="middle" font-size="10">0x7E (1B)</text>
    <text class="fig-label" x="130" y="74" text-anchor="middle" font-size="12">地址 A</text>
    <text class="fig-muted" x="130" y="92" text-anchor="middle" font-size="10">0xFF (1B)</text>
    <text class="fig-label" x="198" y="74" text-anchor="middle" font-size="12">控制 C</text>
    <text class="fig-muted" x="198" y="92" text-anchor="middle" font-size="10">0x03 (1B)</text>
    <text class="fig-purple" x="278" y="74" text-anchor="middle" font-size="12">协议</text>
    <text class="fig-muted" x="278" y="92" text-anchor="middle" font-size="10">2 字节</text>
    <text class="fig-green" x="436" y="74" text-anchor="middle" font-size="12">信息部分（上层数据载荷）</text>
    <text class="fig-muted" x="436" y="92" text-anchor="middle" font-size="10">长度可变，默认最大 ≤ 1500 字节</text>
    <text class="fig-blue" x="586" y="74" text-anchor="middle" font-size="12">FCS</text>
    <text class="fig-muted" x="586" y="92" text-anchor="middle" font-size="10">2B CRC</text>
    <text class="fig-orange" x="658" y="74" text-anchor="middle" font-size="12">标志 F</text>
    <text class="fig-muted" x="658" y="92" text-anchor="middle" font-size="10">0x7E (1B)</text>
    <text class="fig-accent" x="176" y="136" text-anchor="middle" font-size="11">帧首部 4 个字段（5 字节）</text>
    <text class="fig-accent" x="620" y="136" text-anchor="middle" font-size="11">帧尾部 2 个字段（3 字节）</text>
    <text class="fig-orange" x="42" y="171" font-size="11">• 标志 F = 0x7E（0111 1110）：PPP 帧定界符</text>
    <text class="fig-muted" x="42" y="191" font-size="11">• 地址 0xFF、控制 0x03 固定不变（点对点无需寻址）</text>
    <text class="fig-muted" x="42" y="211" font-size="11">• FCS 仅 2 字节（CRC-16），只检错不纠错、无序号无确认</text>
    <text class="fig-purple" x="384" y="171" font-size="11">• 协议字段（2 B）指明信息字段承载内容：</text>
    <text class="fig-label" x="396" y="192" font-size="11">0x0021 → IP 数据报　｜　0xC021 → LCP 数据</text>
    <text class="fig-label" x="396" y="212" font-size="11">0x8021 → NCP 网络控制数据（如 IPCP）</text>
  </svg>
  <figcaption>图 3-8：PPP 帧首尾各字段字节长度、定界符 0x7E（01111110）与 2 字节协议字段分用编码</figcaption>
</figure>

| 字段 | 长度 | 说明 |
|---|---|---|
| 标志 | 1 字节 | 0x7E，帧的定界符 |
| 地址 | 1 字节 | 0xFF，无实际信息 |
| 控制 | 1 字节 | 0x03，无实际信息 |
| 协议 | 2 字节 | 指明数据部分交给哪个协议：0x0021→IP，0xC021→LCP，0x8021→NCP |
| 数据 | 可变 | 上层交付的 PDU；最大长度由 LCP 协商 |
| FCS | 2 字节 | CRC 算出的帧检验序列 |
| 标志 | 1 字节 | 0x7E |

<strong>透明传输问题</strong>：数据部分若出现 0x7E，接收方会误判为帧结束。PPP 的做法取决于链路类型：

- <strong>字节填充</strong>（面向字节的异步链路）：插入转义字符 0x7D。
- <strong>比特填充</strong>（面向比特的同步链路）：插入比特 0。

<figure class="cpd-figure not-content" id="cn-w3-fig-9">
  <svg viewBox="0 0 720 316" role="img" aria-label="PPP透明传输机制对比：面向字节异步链路的0x7D转义字节填充与面向比特同步链路的5个连续1插0比特填充">
    <defs>
      <marker id="cn-w3-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="664" height="122" rx="6"/>
    <rect class="fig-box" x="28" y="180" width="664" height="108" rx="6"/>
    <line class="fig-line-accent" x1="142" y1="104" x2="142" y2="124" marker-end="url(#cn-w3-arr-9)"/>
    <line class="fig-line-accent" x1="258" y1="104" x2="258" y2="124" marker-end="url(#cn-w3-arr-9)"/>
    <line class="fig-line-accent" x1="374" y1="104" x2="374" y2="124" marker-end="url(#cn-w3-arr-9)"/>
    <line class="fig-line" x1="478" y1="104" x2="478" y2="124" marker-end="url(#cn-w3-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="274" y1="234" x2="300" y2="250" marker-end="url(#cn-w3-arr-9)"/>
    <rect class="fig-box-red" x="116" y="76" width="52" height="28" rx="3"/>
    <rect class="fig-box-orange" x="232" y="76" width="52" height="28" rx="3"/>
    <rect class="fig-box-purple" x="348" y="76" width="52" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="452" y="76" width="52" height="28" rx="3"/>
    <rect class="fig-box-blue" x="92" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-blue" x="144" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-blue" x="208" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-blue" x="260" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-blue" x="324" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-blue" x="376" y="126" width="48" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="452" y="126" width="52" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="518" y="68" width="162" height="90" rx="5"/>
    <rect class="fig-box-subtle" x="116" y="208" width="28" height="26" rx="2"/>
    <rect class="fig-box-orange" x="146" y="208" width="150" height="26" rx="2"/>
    <rect class="fig-box-red" x="298" y="208" width="28" height="26" rx="2"/>
    <rect class="fig-box-subtle" x="328" y="208" width="28" height="26" rx="2"/>
    <rect class="fig-box-subtle" x="116" y="250" width="28" height="26" rx="2"/>
    <rect class="fig-box-orange" x="146" y="250" width="150" height="26" rx="2"/>
    <rect class="fig-box-green" x="298" y="250" width="28" height="26" rx="2"/>
    <rect class="fig-box-subtle" x="328" y="250" width="28" height="26" rx="2"/>
    <rect class="fig-box-subtle" x="358" y="250" width="28" height="26" rx="2"/>
    <rect class="fig-box-subtle" x="404" y="202" width="276" height="74" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">PPP 透明传输双方案：异步链路字节填充 vs 同步链路零比特填充</text>
    <text class="fig-blue" x="42" y="66" font-size="12">① 字节填充（面向字节异步链路）</text>
    <text class="fig-muted" x="42" y="94" font-size="11">原数据：</text>
    <text class="fig-red" x="142" y="94" text-anchor="middle" font-size="11">0x7E</text>
    <text class="fig-orange" x="258" y="94" text-anchor="middle" font-size="11">0x7D</text>
    <text class="fig-purple" x="374" y="94" text-anchor="middle" font-size="11">0x03</text>
    <text class="fig-label" x="478" y="94" text-anchor="middle" font-size="11">0x41</text>
    <text class="fig-muted" x="42" y="144" font-size="11">填充后：</text>
    <text class="fig-blue" x="116" y="144" text-anchor="middle" font-size="11">7D</text>
    <text class="fig-blue" x="168" y="144" text-anchor="middle" font-size="11">5E</text>
    <text class="fig-blue" x="232" y="144" text-anchor="middle" font-size="11">7D</text>
    <text class="fig-blue" x="284" y="144" text-anchor="middle" font-size="11">5D</text>
    <text class="fig-blue" x="348" y="144" text-anchor="middle" font-size="11">7D</text>
    <text class="fig-blue" x="400" y="144" text-anchor="middle" font-size="11">23</text>
    <text class="fig-label" x="478" y="144" text-anchor="middle" font-size="11">0x41</text>
    <text class="fig-label" x="599" y="87" text-anchor="middle" font-size="10">转义符 0x7D + 异或 0x20</text>
    <text class="fig-muted" x="599" y="106" text-anchor="middle" font-size="10">7E → 7D 5E（防假边界）</text>
    <text class="fig-muted" x="599" y="124" text-anchor="middle" font-size="10">7D → 7D 5D（防假转义）</text>
    <text class="fig-muted" x="599" y="142" text-anchor="middle" font-size="10">&lt;0x20 控制符 → 7D + 0x20</text>
    <text class="fig-green" x="42" y="198" font-size="12">② 零比特填充（面向比特同步链路，硬件扫描）</text>
    <text class="fig-muted" x="42" y="225" font-size="11">原比特：</text>
    <text class="fig-label" x="130" y="225" text-anchor="middle" font-size="11">0</text>
    <text class="fig-orange" x="221" y="225" text-anchor="middle" font-size="11">1　1　1　1　1</text>
    <text class="fig-red" x="312" y="225" text-anchor="middle" font-size="11">1</text>
    <text class="fig-label" x="342" y="225" text-anchor="middle" font-size="11">0</text>
    <text class="fig-muted" x="42" y="267" font-size="11">填充后：</text>
    <text class="fig-label" x="130" y="267" text-anchor="middle" font-size="11">0</text>
    <text class="fig-orange" x="221" y="267" text-anchor="middle" font-size="11">1　1　1　1　1</text>
    <text class="fig-green" x="312" y="267" text-anchor="middle" font-size="11">0</text>
    <text class="fig-label" x="342" y="267" text-anchor="middle" font-size="11">1</text>
    <text class="fig-label" x="372" y="267" text-anchor="middle" font-size="11">0</text>
    <text class="fig-label" x="542" y="222" text-anchor="middle" font-size="10">发端：每遇连续 5 个 1 立即插 1 个 0</text>
    <text class="fig-muted" x="542" y="242" text-anchor="middle" font-size="10">收端：每遇连续 5 个 1 删去紧跟的 0</text>
    <text class="fig-green" x="542" y="262" text-anchor="middle" font-size="10">保证载荷永不出现 01111110 (0x7E)！</text>
  </svg>
  <figcaption>图 3-9：PPP 透明传输机制对比——异步字节填充（0x7D 转义）与同步零比特填充（5 个连续 1 后插 0）</figcaption>
</figure>

- 比特填充：发送方扫描数据部分，<strong>每发现 5 个连续比特 1 就立即填入一个 0</strong>；接收方每发现 5 个连续 1，就把其后的一个 0 删除。

> <strong>★考点</strong>：字节填充的关键是“转义 + 异或 0x20”；比特填充的关键是“5 个 1 插 0”，因此数据中不会出现 6 个连续 1，定界符 01111110 不会被误认。

### 3.5 PPP 的差错检测与工作状态

<strong>差错检测</strong>：帧尾 FCS 为 2 字节，用 CRC 计算，生成多项式为

$$
G(x)=x^{16}+x^{12}+x^{5}+1
$$

为减少 CPU 占用，FCS 常采用<strong>查表法。接收方每收到一个 PPP 帧就做 CRC 检验：正确则收下，错误则丢弃。使用 PPP 的数据链路层向上不提供可靠传输服务</strong>（只检错、不纠错）。

<figure class="cpd-figure not-content" id="cn-w3-fig-10">
  <svg viewBox="0 0 720 302" role="img" aria-label="PPP协议链路生命周期6状态转移图：静止、建立、鉴别、网络、打开、终止">
    <defs>
      <marker id="cn-w3-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="270" rx="8"/>
    <line class="fig-line-accent" x1="140" y1="86" x2="202" y2="86" marker-end="url(#cn-w3-arr-10)"/>
    <line class="fig-line-accent" x1="308" y1="86" x2="370" y2="86" marker-end="url(#cn-w3-arr-10)"/>
    <line class="fig-line-accent" x1="476" y1="86" x2="538" y2="86" marker-end="url(#cn-w3-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="592" y1="108" x2="592" y2="178" marker-end="url(#cn-w3-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="540" y1="202" x2="402" y2="202" marker-end="url(#cn-w3-arr-10)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" d="M 424 108 L 424 146 L 348 146 L 348 178" marker-end="url(#cn-w3-arr-10)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" d="M 256 108 L 256 140 L 108 140 L 108 110" marker-end="url(#cn-w3-arr-10)"/>
    <path class="fig-line" d="M 296 202 L 68 202 L 68 110" marker-end="url(#cn-w3-arr-10)"/>
    <rect class="fig-box" x="36" y="64" width="104" height="44" rx="6"/>
    <rect class="fig-box-blue" x="204" y="64" width="104" height="44" rx="6"/>
    <rect class="fig-box-orange" x="372" y="64" width="104" height="44" rx="6"/>
    <rect class="fig-box-purple" x="540" y="64" width="104" height="44" rx="6"/>
    <rect class="fig-box-green" x="540" y="180" width="104" height="44" rx="6"/>
    <rect class="fig-box-red" x="296" y="180" width="104" height="44" rx="6"/>
    <rect class="fig-box" x="36" y="238" width="648" height="34" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">PPP 协议工作状态转移全流程（物理载波 → LCP → PAP/CHAP → NCP）</text>
    <text class="fig-label" x="88" y="84" text-anchor="middle" font-size="12">静止</text>
    <text class="fig-muted" x="88" y="100" text-anchor="middle" font-size="10">无物理连接</text>
    <text class="fig-blue" x="256" y="84" text-anchor="middle" font-size="12">建立</text>
    <text class="fig-muted" x="256" y="100" text-anchor="middle" font-size="10">LCP 协商参数</text>
    <text class="fig-orange" x="424" y="84" text-anchor="middle" font-size="12">鉴别</text>
    <text class="fig-muted" x="424" y="100" text-anchor="middle" font-size="10">PAP / CHAP</text>
    <text class="fig-purple" x="592" y="84" text-anchor="middle" font-size="12">网络</text>
    <text class="fig-muted" x="592" y="100" text-anchor="middle" font-size="10">NCP 配 IP</text>
    <text class="fig-green" x="592" y="200" text-anchor="middle" font-size="12">打开</text>
    <text class="fig-muted" x="592" y="216" text-anchor="middle" font-size="10">双向传输数据</text>
    <text class="fig-red" x="348" y="200" text-anchor="middle" font-size="12">终止</text>
    <text class="fig-muted" x="348" y="216" text-anchor="middle" font-size="10">LCP 拆除链路</text>
    <text class="fig-accent" x="172" y="78" text-anchor="middle" font-size="10">检出载波</text>
    <text class="fig-accent" x="340" y="78" text-anchor="middle" font-size="10">LCP 成功</text>
    <text class="fig-accent" x="508" y="78" text-anchor="middle" font-size="10">鉴别通过</text>
    <text class="fig-green" x="644" y="148" text-anchor="middle" font-size="10">IPCP 完成</text>
    <text class="fig-orange" x="470" y="194" text-anchor="middle" font-size="10">故障/终止请求</text>
    <text class="fig-red" x="434" y="138" font-size="10">鉴别失败</text>
    <text class="fig-red" x="182" y="154" text-anchor="middle" font-size="10">LCP 协商失败</text>
    <text class="fig-muted" x="182" y="194" text-anchor="middle" font-size="10">载波停止 → 回静止</text>
    <text class="fig-muted" x="360" y="259" text-anchor="middle" font-size="11">考点：PPP 只检错（CRC-16）不纠错、无序号无确认，向上提供不可靠点对点数据链路传输服务</text>
  </svg>
  <figcaption>图 3-10：PPP 协议从物理层载波建立、LCP 链路配置、PAP/CHAP 鉴别、NCP 网络配置到打开通信的完整状态转换</figcaption>
</figure>

1. <strong>静止</strong>：不存在物理层连接。检测到调制解调器的载波信号并建立物理层连接后 → <strong>建立</strong>。
2. <strong>建立</strong>：LCP 开始协商配置选项（最大帧长、鉴别协议等）。协商成功 → 鉴别；失败 → 回到静止。
3. <strong>鉴别</strong>：可以不用鉴别，也可用口令鉴别协议 PAP 或挑战握手鉴别协议 CHAP。无需鉴别或鉴别成功 → 网络；鉴别失败 → 终止。
4. <strong>网络</strong>：进行 NCP 配置。运行 IP 时用 IP 控制协议 IPCP 为链路两端配置 IP 模块（如分配 IP 地址）。配置完成 → 打开。
5. <strong>打开</strong>：可进行数据通信。
6. <strong>终止</strong>：出现故障或链路一端发出终止请求时进入；载波停止后回到静止。

### 3.6 媒体接入控制的基本概念

多台主机共享同一传输媒体（如同轴电缆总线型局域网），各站竞争使用总线、随机在信道上发送数据。若两个或更多站点同一时刻发送，信号在共享媒体上叠加产生<strong>碰撞，这些站的发送都失败。如何协调多个发送和接收站点对一个共享传输媒体的占用，就是媒体接入控制（MAC，介质访问控制）</strong>。

<figure class="cpd-figure not-content" id="cn-w3-fig-11">
  <svg viewBox="0 0 720 302" role="img" aria-label="媒体接入控制MAC分类体系：静态划分信道FDMA、TDMA、WDMA、CDMA与动态接入受控接入、随机接入CSMA/CD和CSMA/CA">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="270" rx="8"/>
    <line class="fig-line" x1="360" y1="78" x2="360" y2="88"/>
    <line class="fig-line" x1="168" y1="88" x2="508" y2="88"/>
    <line class="fig-line" x1="168" y1="88" x2="168" y2="98"/>
    <line class="fig-line" x1="508" y1="88" x2="508" y2="98"/>
    <line class="fig-line-dashed" x1="168" y1="128" x2="168" y2="140"/>
    <line class="fig-line" x1="508" y1="128" x2="508" y2="135"/>
    <line class="fig-line" x1="413" y1="135" x2="603" y2="135"/>
    <line class="fig-line" x1="413" y1="135" x2="413" y2="142"/>
    <line class="fig-line" x1="603" y1="135" x2="603" y2="142"/>
    <rect class="fig-box-accent" x="240" y="46" width="240" height="32" rx="6"/>
    <rect class="fig-box-blue" x="40" y="98" width="256" height="30" rx="5"/>
    <rect class="fig-box-green" x="324" y="98" width="368" height="30" rx="5"/>
    <rect class="fig-box" x="40" y="142" width="122" height="28" rx="4"/>
    <rect class="fig-box" x="174" y="142" width="122" height="28" rx="4"/>
    <rect class="fig-box" x="40" y="178" width="122" height="28" rx="4"/>
    <rect class="fig-box" x="174" y="178" width="122" height="28" rx="4"/>
    <rect class="fig-box-purple" x="324" y="142" width="178" height="64" rx="5"/>
    <rect class="fig-box-orange" x="514" y="142" width="178" height="64" rx="5"/>
    <rect class="fig-box" x="40" y="216" width="256" height="54" rx="5"/>
    <rect class="fig-box" x="324" y="216" width="368" height="54" rx="5"/>
    <text class="fig-title" x="360" y="36" text-anchor="middle" font-size="13">媒体接入控制（MAC）方法分类全景图</text>
    <text class="fig-accent" x="360" y="67" text-anchor="middle" font-size="12">媒体接入控制 MAC</text>
    <text class="fig-blue" x="168" y="118" text-anchor="middle" font-size="12">① 静态划分信道（物理层复用）</text>
    <text class="fig-green" x="508" y="118" text-anchor="middle" font-size="12">② 动态媒体接入控制（链路层按需共享）</text>
    <text class="fig-label" x="101" y="160" text-anchor="middle" font-size="11">频分多址 FDMA</text>
    <text class="fig-label" x="235" y="160" text-anchor="middle" font-size="11">时分多址 TDMA</text>
    <text class="fig-label" x="101" y="196" text-anchor="middle" font-size="11">波分多址 WDMA</text>
    <text class="fig-label" x="235" y="196" text-anchor="middle" font-size="11">码分多址 CDMA</text>
    <text class="fig-purple" x="413" y="160" text-anchor="middle" font-size="11">受控接入（无碰撞）</text>
    <text class="fig-muted" x="413" y="178" text-anchor="middle" font-size="10">集中：多点轮询（主站单点故障）</text>
    <text class="fig-muted" x="413" y="195" text-anchor="middle" font-size="10">分散：令牌传递（802.5/FDDI）</text>
    <text class="fig-orange" x="603" y="160" text-anchor="middle" font-size="11">随机接入（竞争碰撞）</text>
    <text class="fig-label" x="603" y="178" text-anchor="middle" font-size="10">有线总线：以太网 CSMA/CD</text>
    <text class="fig-label" x="603" y="195" text-anchor="middle" font-size="10">无线信道：802.11 CSMA/CA</text>
    <text class="fig-muted" x="168" y="237" text-anchor="middle" font-size="10">预先固定分配，代价高、不灵活；</text>
    <text class="fig-muted" x="168" y="256" text-anchor="middle" font-size="10">电脑突发性通信时信道大量空闲浪费</text>
    <text class="fig-muted" x="508" y="237" text-anchor="middle" font-size="10">随机接入最灵活：站点随时竞争发送，核心在于</text>
    <text class="fig-muted" x="508" y="256" text-anchor="middle" font-size="10">降低冲突概率（载波监听）与冲突后快速退避恢复</text>
  </svg>
  <figcaption>图 3-11：媒体接入控制（MAC）两大分支——物理层静态信道划分与数据链路层受控/随机动态接入对比</figcaption>
</figure>

<strong>两类方法</strong>：

| 类别 | 子类 | 特点 |
|---|---|---|
| 静态划分信道 | 频分、时分、波分、码分 | 预先固定分配，不灵活，突发性数据传输时信道利用率低；通常在无线网络物理层使用 |
| 动态接入·受控 | 集中控制（多点轮询） | 一个主站循环轮询各站，被轮询到的站才能发送；最大缺点是存在<strong>单点故障</strong> |
| 动态接入·受控 | 分散控制（令牌传递） | 各站平等连成环形，令牌（特殊控制帧）逐站传递，持令牌者才有权发送，发完传给下一站 |
| 动态接入·随机 | 竞争发送 | 所有站随机竞争信道，可能碰撞；要尽量避免冲突并在冲突后尽快恢复 |

- 令牌传递的典型网络：<strong>802.5 令牌环网、802.4 令牌总线网、FDDI</strong>，均已退出历史舞台。
- 随机接入的典型：<strong>以太网</strong>。
- 注意：交换式局域网已在有线领域完全取代共享式局域网，但无线信道具有广播天性，无线局域网仍使用共享媒体技术。

### 3.7 静态划分信道：频分、时分与波分复用

<strong>复用</strong>：通过一条物理线路同时传输多路用户的信号。当传输媒体的容量大于多条单一信道总通信量时，用复用技术在一条物理线路上建立多条通信信道，充分利用带宽。发送端用<strong>复用器</strong>、接收端用<strong>分用器</strong>。

<figure class="cpd-figure not-content" id="cn-w3-fig-12">
  <svg viewBox="0 0 720 316" role="img" aria-label="频分复用FDM、时分复用TDM与光波分复用WDM原理对比图">
    <defs>
      <marker id="cn-w3-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="326" height="144" rx="6"/>
    <rect class="fig-box" x="366" y="48" width="326" height="144" rx="6"/>
    <rect class="fig-box" x="28" y="202" width="664" height="84" rx="6"/>
    <line class="fig-line" x1="58" y1="168" x2="334" y2="168" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line" x1="58" y1="168" x2="58" y2="74" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line" x1="388" y1="168" x2="674" y2="168" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line" x1="388" y1="168" x2="388" y2="74" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line-accent" x1="162" y1="250" x2="218" y2="250" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line-accent" x1="292" y1="250" x2="354" y2="250" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line-accent" x1="444" y1="250" x2="506" y2="250" marker-end="url(#cn-w3-arr-12)"/>
    <line class="fig-line-accent" x1="580" y1="250" x2="626" y2="250" marker-end="url(#cn-w3-arr-12)"/>
    <rect class="fig-box-blue" x="68" y="80" width="204" height="20" rx="3"/>
    <rect class="fig-box-green" x="68" y="108" width="204" height="20" rx="3"/>
    <rect class="fig-box-orange" x="68" y="136" width="204" height="20" rx="3"/>
    <rect class="fig-box-blue" x="398" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-green" x="440" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-orange" x="482" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-blue" x="536" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-green" x="578" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-orange" x="620" y="96" width="42" height="58" rx="2"/>
    <rect class="fig-box-subtle" x="42" y="232" width="120" height="36" rx="4"/>
    <rect class="fig-box-purple" x="220" y="232" width="72" height="36" rx="4"/>
    <rect class="fig-box-orange" x="356" y="232" width="88" height="36" rx="4"/>
    <rect class="fig-box-purple" x="508" y="232" width="72" height="36" rx="4"/>
    <rect class="fig-box-subtle" x="628" y="232" width="52" height="36" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">静态信道划分复用技术对比：FDM、TDM 与光波分复用 WDM</text>
    <text class="fig-blue" x="191" y="66" text-anchor="middle" font-size="12">频分复用 FDM：同时占用不同频带</text>
    <text class="fig-muted" x="46" y="84" text-anchor="middle" font-size="10">f</text>
    <text class="fig-muted" x="326" y="182" text-anchor="end" font-size="10">时间 t →</text>
    <text class="fig-blue" x="170" y="94" text-anchor="middle" font-size="10">子信道 3（用户 C）</text>
    <text class="fig-green" x="170" y="122" text-anchor="middle" font-size="10">子信道 2（用户 B）</text>
    <text class="fig-orange" x="170" y="150" text-anchor="middle" font-size="10">子信道 1（用户 A）</text>
    <text class="fig-muted" x="308" y="108" text-anchor="middle" font-size="9">隔离频带</text>
    <text class="fig-muted" x="308" y="136" text-anchor="middle" font-size="9">防邻道干扰</text>
    <text class="fig-green" x="529" y="66" text-anchor="middle" font-size="12">时分复用 TDM：分时占用全部频带</text>
    <text class="fig-muted" x="378" y="84" text-anchor="middle" font-size="10">f</text>
    <text class="fig-muted" x="666" y="182" text-anchor="end" font-size="10">时间 t →</text>
    <text class="fig-muted" x="461" y="88" text-anchor="middle" font-size="10">TDM 帧 #1</text>
    <text class="fig-muted" x="599" y="88" text-anchor="middle" font-size="10">TDM 帧 #2（等长周期）</text>
    <text class="fig-blue" x="419" y="129" text-anchor="middle" font-size="11">A1</text>
    <text class="fig-green" x="461" y="129" text-anchor="middle" font-size="11">B1</text>
    <text class="fig-orange" x="503" y="129" text-anchor="middle" font-size="11">C1</text>
    <text class="fig-blue" x="557" y="129" text-anchor="middle" font-size="11">A2</text>
    <text class="fig-green" x="599" y="129" text-anchor="middle" font-size="11">B2</text>
    <text class="fig-orange" x="641" y="129" text-anchor="middle" font-size="11">C2</text>
    <text class="fig-purple" x="360" y="221" text-anchor="middle" font-size="11">波分复用 WDM（光的频分复用）：单根光纤复传多路不同波长激光（1550～1561.2 nm）</text>
    <text class="fig-label" x="102" y="248" text-anchor="middle" font-size="10">8 路光载波 λ1..λ8</text>
    <text class="fig-muted" x="102" y="262" text-anchor="middle" font-size="9">间隔 1.6 nm</text>
    <text class="fig-purple" x="256" y="254" text-anchor="middle" font-size="10">光复用器</text>
    <text class="fig-accent" x="323" y="244" text-anchor="middle" font-size="9">20 Gb/s</text>
    <text class="fig-orange" x="400" y="248" text-anchor="middle" font-size="10">EDFA 放大</text>
    <text class="fig-muted" x="400" y="262" text-anchor="middle" font-size="9">跨距 120 km</text>
    <text class="fig-purple" x="544" y="254" text-anchor="middle" font-size="10">光分用器</text>
    <text class="fig-label" x="654" y="254" text-anchor="middle" font-size="10">8 路还原</text>
  </svg>
  <figcaption>图 3-12：频分复用（FDM 隔离频带并行）、时分复用（TDM 固定时隙轮转）与光波分复用（WDM + EDFA）对比</figcaption>
</figure>

- <strong>频分复用 FDM：把传输线路的频谱资源划分成多个子频带，形成多个子信道，子信道间留隔离频带。各路信号调制到不同频率的载波上，接收端用滤波器分开。所有用户同时占用不同频带</strong>。
- <strong>时分复用 TDM</strong>：把时间划分成一段段等长的 <strong>TDM 帧</strong>，每帧分成若干时隙；每个用户在每个 TDM 帧中占用<strong>固定序号的时隙，周期性出现，周期就是 TDM 帧的长度。所有用户在不同时间占用同样的频带宽度</strong>。
- <strong>波分复用 WDM</strong>：本质是<strong>光的频分复用</strong>。8 路波长均为 1310 nm 的激光经光调制后变换到 <strong>1550～1561.2 nm</strong>，相邻光载波间隔 <strong>1.6 nm</strong>，同一根光纤中传输，总速率 $8\times2.5\ \text{Gb/s}=20\ \text{Gb/s}$。光信号传输一段距离后衰减，需用<strong>掺铒光纤放大器 EDFA</strong> 放大，两个放大器之间的光缆线路长度可达 <strong>120 km</strong>。

### 3.8 码分复用与码分多址

码分复用（主要用于多址接入，故称<strong>码分多址 CDMA</strong>）中，各用户在<strong>同样的时间、同样的频带</strong>上通信，靠各自经过特殊挑选的<strong>不同码型</strong>区分，因此互不干扰。它最初用于军事通信（抗干扰强、频谱似白噪声），现广泛用于民用移动通信。

<strong>码片</strong>：每个比特时间再划分为 $m$ 个短间隔，每个间隔称为一个<strong>码片</strong>，$m$ 通常取 64 或 128（举例取 8）。每个站被指派一个唯一的 $m$ 比特<strong>码片序列</strong>：

- 发送比特 <strong>1</strong> → 发送自己的码片序列；
- 发送比特 <strong>0</strong> → 发送自己码片序列的<strong>二进制反码</strong>。

按惯例把码片中的 0 写为 $-1$、1 写为 $+1$。例如码片序列 00011011 记为 $(-1,-1,-1,+1,+1,-1,+1,+1)$。

<strong>码片挑选原则</strong>：① 各站码片序列各不相同（实际常用伪随机序列）；② 各站码片序列<strong>相互正交</strong>，即规格化内积为 0：

$$
S \cdot T=\frac{1}{m}\sum_{i=1}^{m} S_i T_i=0
$$

常用性质：

| 内积 | 结果 | 含义 |
|---|---|---|
| $S\cdot S$ | $1$ | 与自身 |
| $S\cdot T$ | $0$ | 与其他站码片正交 |
| $S\cdot \bar{T}$ | $0$ | 与其他站反码正交 |
| $S\cdot \bar{S}$ | $-1$ | 与自身反码 |

<strong>接收判决</strong>：把收到的叠加信号分别与各站码片求规格化内积——结果为 $1$ 表示该站发了比特 1，为 $-1$ 表示发了比特 0，为 $0$ 表示该站未发送数据。

<figure class="cpd-figure not-content" id="cn-w3-fig-13">
  <svg viewBox="0 0 720 316" role="img" aria-label="CDMA码分多址8位正交码片序列向量叠加与接收端规格化内积分离实例">
    <defs>
      <marker id="cn-w3-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <line class="fig-line-accent" x1="252" y1="84" x2="288" y2="124" marker-end="url(#cn-w3-arr-13)"/>
    <line class="fig-line-accent" x1="252" y1="142" x2="288" y2="142" marker-end="url(#cn-w3-arr-13)"/>
    <line class="fig-line-dashed" x1="252" y1="200" x2="288" y2="160" marker-end="url(#cn-w3-arr-13)"/>
    <line class="fig-line-accent" x1="456" y1="124" x2="490" y2="84" marker-end="url(#cn-w3-arr-13)"/>
    <line class="fig-line-accent" x1="456" y1="142" x2="490" y2="142" marker-end="url(#cn-w3-arr-13)"/>
    <line class="fig-line-accent" x1="456" y1="160" x2="490" y2="200" marker-end="url(#cn-w3-arr-13)"/>
    <rect class="fig-box-blue" x="28" y="60" width="224" height="48" rx="5"/>
    <rect class="fig-box-orange" x="28" y="118" width="224" height="48" rx="5"/>
    <rect class="fig-box" x="28" y="176" width="224" height="48" rx="5"/>
    <rect class="fig-box-purple" x="290" y="96" width="166" height="92" rx="6"/>
    <rect class="fig-box-green" x="492" y="60" width="200" height="48" rx="5"/>
    <rect class="fig-box-green" x="492" y="118" width="200" height="48" rx="5"/>
    <rect class="fig-box" x="492" y="176" width="200" height="48" rx="5"/>
    <rect class="fig-box" x="28" y="238" width="664" height="48" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">CDMA 正交码片叠加与规格化内积分离实例（m = 8 位码片）</text>
    <text class="fig-blue" x="140" y="79" text-anchor="middle" font-size="11">站 A 发比特 1 → +A</text>
    <text class="fig-muted" x="140" y="97" text-anchor="middle" font-size="10">(−1,−1,−1,+1,+1,−1,+1,+1)</text>
    <text class="fig-orange" x="140" y="137" text-anchor="middle" font-size="11">站 B 发比特 0 → −B（反码）</text>
    <text class="fig-muted" x="140" y="155" text-anchor="middle" font-size="10">(+1,+1,−1,+1,−1,−1,−1,+1)</text>
    <text class="fig-label" x="140" y="195" text-anchor="middle" font-size="11">站 C 未发送 → 0</text>
    <text class="fig-muted" x="140" y="213" text-anchor="middle" font-size="10">C=(−1,+1,−1,+1,+1,+1,−1,−1)</text>
    <text class="fig-purple" x="373" y="122" text-anchor="middle" font-size="12">信道线性叠加 S</text>
    <text class="fig-label" x="373" y="144" text-anchor="middle" font-size="11">S = +A − B</text>
    <text class="fig-muted" x="373" y="166" text-anchor="middle" font-size="10">(0, 0,−2,+2, 0,−2, 0,+2)</text>
    <text class="fig-green" x="592" y="79" text-anchor="middle" font-size="11">S · A = +8 / 8 = +1</text>
    <text class="fig-label" x="592" y="97" text-anchor="middle" font-size="10">判出：站 A 发送了比特 1</text>
    <text class="fig-green" x="592" y="137" text-anchor="middle" font-size="11">S · B = −8 / 8 = −1</text>
    <text class="fig-label" x="592" y="155" text-anchor="middle" font-size="10">判出：站 B 发送了比特 0</text>
    <text class="fig-label" x="592" y="195" text-anchor="middle" font-size="11">S · C = 0 / 8 = 0</text>
    <text class="fig-muted" x="592" y="213" text-anchor="middle" font-size="10">判出：站 C 未发送数据</text>
    <text class="fig-label" x="42" y="258" font-size="11">• 正交条件：任意两不同站码片 S·T = 0（如 s=01011101 与 t=10111000 内积 = −2/8 ≠ 0，不可同网混用）</text>
    <text class="fig-muted" x="42" y="276" font-size="11">• 扩频增益：每比特拆成 m 个码片，信号带宽扩展 m 倍，抗窄带干扰强且所有用户可同频同时通信</text>
  </svg>
  <figcaption>图 3-13：CDMA 8 位正交码片序列线性叠加（S = A − B）与接收端规格化内积（+1 / −1 / 0）解调过程</figcaption>
</figure>

> <strong>★考点</strong>：给定“收到的码片序列”和某站码片序列，按 $m$ 位一组切分，逐组求内积，即可逐比特还原该站数据（2014 年真题）。

### 3.9 CSMA/CA 协议

<strong>为什么无线局域网不能用 CSMA/CD</strong>：

1. 无线信道的信号强度动态范围非常大，网卡收到的信号强度往往远小于发送信号强度（可能相差百万倍），<strong>在硬件上实现碰撞检测要求极高</strong>；
2. 即使能实现，由于电波传播的特殊性（如<strong>隐蔽站问题</strong>），碰撞检测也意义不大——A 的信号能覆盖 B 但覆盖不到 C，C 能覆盖 B 但覆盖不到 A，A 与 C 同时给 B 发帧必然碰撞，而双方都检测不到。有线总线不存在此问题：碰撞信号最多经过一个端到端往返传播时延就传遍总线。

因此 802.11 使用 <strong>CSMA/CA</strong>：在 CSMA 基础上增加<strong>碰撞避免（CA）</strong>，不再实现碰撞检测（CD）。由于不可能避免所有碰撞且无线信道误码率较高，802.11 还使用<strong>数据链路层确认机制（停止等待协议）</strong>：每发完一个数据帧，要收到对方 ACK 才能发下一帧。

<figure class="cpd-figure not-content" id="cn-w3-fig-14">
  <svg viewBox="0 0 720 296" role="img" aria-label="802.11无线局域网隐蔽站问题与暴露站问题几何覆盖范围对比">
    <defs>
      <marker id="cn-w3-arr-14" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="264" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="332" height="218" rx="6"/>
    <rect class="fig-box" x="372" y="48" width="320" height="218" rx="6"/>
    <circle class="fig-box-dashed" cx="142" cy="136" r="62"/>
    <circle class="fig-box-dashed" cx="246" cy="136" r="62"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="126" y1="136" x2="174" y2="136" marker-end="url(#cn-w3-arr-14)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="262" y1="136" x2="214" y2="136" marker-end="url(#cn-w3-arr-14)"/>
    <circle class="fig-box-dashed" cx="496" cy="128" r="56"/>
    <line class="fig-line-accent" x1="478" y1="128" x2="438" y2="128" marker-end="url(#cn-w3-arr-14)"/>
    <line class="fig-line-dashed" x1="564" y1="128" x2="626" y2="128" marker-end="url(#cn-w3-arr-14)"/>
    <circle class="fig-box-blue" cx="108" cy="136" r="18"/>
    <circle class="fig-box-red" cx="194" cy="136" r="18"/>
    <circle class="fig-box-blue" cx="280" cy="136" r="18"/>
    <circle class="fig-box-green" cx="418" cy="128" r="16"/>
    <circle class="fig-box-blue" cx="496" cy="128" r="16"/>
    <circle class="fig-box-orange" cx="548" cy="128" r="16"/>
    <circle class="fig-box" cx="644" cy="128" r="16"/>
    <rect class="fig-box-subtle" x="42" y="206" width="304" height="48" rx="4"/>
    <rect class="fig-box-subtle" x="386" y="196" width="292" height="58" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">无线信道特殊传播问题：隐蔽站碰撞与暴露站误抑制</text>
    <text class="fig-red" x="194" y="66" text-anchor="middle" font-size="12">① 隐蔽站问题（A、C 互不可见）</text>
    <text class="fig-blue" x="108" y="140" text-anchor="middle" font-size="11">A</text>
    <text class="fig-red" x="194" y="140" text-anchor="middle" font-size="11">B</text>
    <text class="fig-blue" x="280" y="140" text-anchor="middle" font-size="11">C</text>
    <text class="fig-muted" x="116" y="88" text-anchor="middle" font-size="9">A 覆盖区</text>
    <text class="fig-muted" x="272" y="88" text-anchor="middle" font-size="9">C 覆盖区</text>
    <text class="fig-red" x="194" y="110" text-anchor="middle" font-size="10">⚡ 同时发往 B 碰撞</text>
    <text class="fig-label" x="194" y="224" text-anchor="middle" font-size="10">A 与 C 均监听不到对方载波，误以为信道空闲；</text>
    <text class="fig-muted" x="194" y="242" text-anchor="middle" font-size="10">同时向公共接收站 B 发送导致 B 处信号冲突失败</text>
    <text class="fig-orange" x="532" y="66" text-anchor="middle" font-size="12">② 暴露站问题与 RTS/CTS 化解</text>
    <text class="fig-green" x="418" y="132" text-anchor="middle" font-size="11">A</text>
    <text class="fig-blue" x="496" y="132" text-anchor="middle" font-size="11">B</text>
    <text class="fig-orange" x="548" y="132" text-anchor="middle" font-size="11">C</text>
    <text class="fig-label" x="644" y="132" text-anchor="middle" font-size="11">D</text>
    <text class="fig-muted" x="496" y="86" text-anchor="middle" font-size="9">B 覆盖区</text>
    <text class="fig-muted" x="596" y="116" text-anchor="middle" font-size="9">C→D 实际不扰 A</text>
    <text class="fig-label" x="532" y="214" text-anchor="middle" font-size="10">• 暴露站：B→A 时 C 听到 B 载波而不敢发往 D</text>
    <text class="fig-green" x="532" y="232" text-anchor="middle" font-size="10">• 解法：目的站广播 CTS 预约时长，隐蔽站据</text>
    <text class="fig-green" x="532" y="247" text-anchor="middle" font-size="10">CTS 设置 NAV 静默，彻底避免数据帧期碰撞</text>
  </svg>
  <figcaption>图 3-14：802.11 无线信道无法使用 CSMA/CD 的空间根源——隐蔽站冲突与暴露站抑制及 RTS/CTS 预约解法</figcaption>
</figure>

<strong>两种接入方式</strong>：

- <strong>DCF（分布式协调功能）</strong>：无中心控制站点，各站用 CSMA/CA 竞争获取发送权。<strong>802.11 默认方式</strong>。
- <strong>PCF（点协调功能）：集中控制接入算法，一般在接入点 AP 实现，可选方式</strong>，实际较少使用。

<strong>帧间间隔 IFS</strong>：所有站点必须在持续检测到信道空闲一段指定时间后才能发送帧。高优先级帧等待时间短，可优先获得发送权。

| 类型 | 长度 | 用途 |
|---|---|---|
| SIFS（短帧间间隔） | 28 μs | 最短；分隔属于一次对话的各帧；用于 ACK、CTS、过长的 MAC 帧分片后的数据帧、回答 AP 探寻的帧、PCF 中 AP 发送的帧 |
| DIFS（DCF 帧间间隔） | 128 μs | 比 SIFS 长得多；DCF 中用于发送数据帧和管理帧 |

<figure class="cpd-figure not-content" id="cn-w3-fig-15">
  <svg viewBox="0 0 720 332" role="img" aria-label="802.11 CSMA/CA基本接入时序DIFS退避SIFS确认与RTS/CTS虚拟载波监听NAV网络分配向量时序">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="300" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="664" height="106" rx="6"/>
    <rect class="fig-box" x="28" y="164" width="664" height="138" rx="6"/>
    <line class="fig-line" x1="98" y1="98" x2="674" y2="98"/>
    <line class="fig-line" x1="98" y1="138" x2="674" y2="138"/>
    <line class="fig-line-dashed" x1="484" y1="98" x2="484" y2="138"/>
    <line class="fig-line" x1="98" y1="214" x2="674" y2="214"/>
    <line class="fig-line" x1="98" y1="248" x2="674" y2="248"/>
    <line class="fig-line" x1="98" y1="284" x2="674" y2="284"/>
    <line class="fig-line-dashed" x1="208" y1="214" x2="208" y2="284"/>
    <line class="fig-line-dashed" x1="292" y1="248" x2="292" y2="284"/>
    <line class="fig-line-dashed" x1="496" y1="214" x2="496" y2="248"/>
    <line class="fig-line-dashed" x1="596" y1="248" x2="596" y2="284"/>
    <rect class="fig-box-blue" x="108" y="74" width="76" height="24" rx="2"/>
    <rect class="fig-box-orange" x="184" y="74" width="120" height="24" rx="2"/>
    <rect class="fig-box-accent" x="304" y="74" width="180" height="24" rx="2"/>
    <rect class="fig-box-purple" x="484" y="114" width="64" height="24" rx="2"/>
    <rect class="fig-box-green" x="548" y="114" width="68" height="24" rx="2"/>
    <rect class="fig-box-blue" x="108" y="192" width="52" height="22" rx="2"/>
    <rect class="fig-box-orange" x="160" y="192" width="48" height="22" rx="2"/>
    <rect class="fig-box-purple" x="292" y="192" width="42" height="22" rx="2"/>
    <rect class="fig-box-accent" x="334" y="192" width="162" height="22" rx="2"/>
    <rect class="fig-box-purple" x="208" y="226" width="42" height="22" rx="2"/>
    <rect class="fig-box-green" x="250" y="226" width="42" height="22" rx="2"/>
    <rect class="fig-box-purple" x="496" y="226" width="42" height="22" rx="2"/>
    <rect class="fig-box-green" x="538" y="226" width="58" height="22" rx="2"/>
    <rect class="fig-box-red" x="208" y="262" width="388" height="22" rx="2"/>
    <rect class="fig-box-blue" x="596" y="262" width="56" height="22" rx="2"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">802.11 CSMA/CA 帧间间隔优先级与 RTS/CTS 虚拟载波监听（NAV）时序</text>
    <text class="fig-blue" x="42" y="64" font-size="11">① 基本接入（停止-等待确认 + 忙时退避计时器冻结）</text>
    <text class="fig-label" x="62" y="90" text-anchor="middle" font-size="11">源站</text>
    <text class="fig-label" x="62" y="130" text-anchor="middle" font-size="11">目的站</text>
    <text class="fig-blue" x="146" y="90" text-anchor="middle" font-size="10">DIFS 128μs</text>
    <text class="fig-orange" x="244" y="90" text-anchor="middle" font-size="10">随机退避（忙冻结）</text>
    <text class="fig-accent" x="394" y="90" text-anchor="middle" font-size="10">发送数据帧 DATA</text>
    <text class="fig-purple" x="516" y="130" text-anchor="middle" font-size="10">SIFS 28μs</text>
    <text class="fig-green" x="582" y="130" text-anchor="middle" font-size="10">ACK 确认</text>
    <text class="fig-orange" x="42" y="181" font-size="11">② 信道预约与虚拟载波监听（SIFS &lt; DIFS 保证同一对话控制帧/ACK 抢占最高优先级）</text>
    <text class="fig-label" x="62" y="207" text-anchor="middle" font-size="11">源站</text>
    <text class="fig-label" x="62" y="241" text-anchor="middle" font-size="11">目的站</text>
    <text class="fig-label" x="62" y="277" text-anchor="middle" font-size="11">其他站</text>
    <text class="fig-blue" x="134" y="207" text-anchor="middle" font-size="10">DIFS</text>
    <text class="fig-orange" x="184" y="207" text-anchor="middle" font-size="10">RTS</text>
    <text class="fig-purple" x="229" y="241" text-anchor="middle" font-size="9">SIFS</text>
    <text class="fig-green" x="271" y="241" text-anchor="middle" font-size="10">CTS</text>
    <text class="fig-purple" x="313" y="207" text-anchor="middle" font-size="9">SIFS</text>
    <text class="fig-accent" x="415" y="207" text-anchor="middle" font-size="10">数据帧 DATA</text>
    <text class="fig-purple" x="517" y="241" text-anchor="middle" font-size="9">SIFS</text>
    <text class="fig-green" x="567" y="241" text-anchor="middle" font-size="10">ACK</text>
    <text class="fig-red" x="402" y="277" text-anchor="middle" font-size="10">NAV 网络分配向量（听 RTS/CTS 持续时间静默推迟接入）</text>
    <text class="fig-blue" x="624" y="277" text-anchor="middle" font-size="9">DIFS+退避</text>
  </svg>
  <figcaption>图 3-15：CSMA/CA 基本接入（DIFS 监听 + 退避倒计时 + SIFS 回 ACK）与 RTS/CTS 预约更新 NAV 虚拟载波监听时序</figcaption>
</figure>

<strong>退避算法</strong>：站点为退避计时器设置一个随机退避时间；计时器减到 0 就发送数据；若未到 0 而信道又变为忙，则<strong>冻结</strong>计时器数值，等信道空闲并经过 DIFS 后继续倒计时。

- 第 $i$ 次退避，从整数集合 $\{0,1,\dots,2^{2+i}-1\}$ 中随机取一个数 $k$，退避时间 $=k\times$ 基本退避时间（一个时隙）。这样可降低不同站点选中同一时隙的概率。
- 当 $i=6$ 时窗口达 <strong>255</strong>，之后不再增加。

$$
T_{\text{backoff}}=k\times \text{slot},\quad k\in\{0,1,\dots,2^{2+i}-1\}
$$

<strong>必须使用退避算法的情况</strong>：① 发送数据帧前检测到信道忙；② 每一次重传数据帧；③ 每一次成功发送后要连续发送下一个帧（避免一个站长时间占用信道）。若检测到信道空闲且不是“紧接上一帧连续发送”，则不必退避。

<strong>信道预约与虚拟载波监听</strong>：

- 源站先发 RTS（请求发送）短控制帧，含源地址、目的地址、本次通信（含确认帧）所需持续时间；目的站收到后在 SIFS 后回 <strong>CTS（允许发送）</strong>，同样含持续时间；源站收到 CTS 后等 SIFS 即发数据帧；目的站正确收到后等 SIFS 回 ACK。
- 其他站收到 CTS 或数据帧后，在持续时间之内推迟接入，从而不受干扰。RTS 碰撞则源站收不到 CTS，需执行退避算法重传 RTS。RTS/CTS 很短，碰撞概率与开销都小，用很小的代价预约信道是值得的。
- <strong>虚拟载波监听</strong>：RTS、CTS、数据帧都携带通信持续时间。站点只要监听到其中之一，就知道信道被占用的时长，不必真正监听信号，因此可缓解<strong>隐蔽站</strong>带来的碰撞。
- 802.11 规定三种选择：使用 RTS/CTS；不使用；仅当数据帧长度超过某数值时才使用。

> <strong>★考点</strong>：CSMA 本身不含确认机制；CSMA/CD 用于早期共享总线以太网；CSMA/CA 用于 802.11，并配合停止等待确认；CDMA 是物理层信道复用技术，不属于 MAC 协议（2011、2013 年真题）。

### 3.10 MAC 地址与 IP 地址

<strong>MAC 地址</strong>：属于<strong>数据链路层</strong>的地址，又称硬件地址或物理地址（注意“物理地址”不等于物理层地址）。封装在帧首部——源 MAC 地址、目的 MAC 地址。

<strong>IP 地址</strong>：属于<strong>网络层</strong>，是因特网上的主机和路由器所使用的地址，标识两部分信息：

- <strong>网络编号</strong>：标识因特网上数以百万计的网络；
- <strong>主机编号</strong>：标识同一网络上不同的主机或路由器各接口。

同一网络上的各主机和路由器各接口的 IP 地址，<strong>网络号部分相同、主机号部分互不相同</strong>；不同网络的网络编号必须各不相同。一个 IP 地址中哪部分是网络号、哪部分是主机号并不都相同（详见网络层一章）。

MAC 地址不具备区分不同网络的功能；若只是一个单独的网络、不接入因特网，可以只用 MAC 地址；一旦接入因特网，<strong>IP 地址和 MAC 地址都需要使用</strong>。

<strong>封装位置</strong>：应用层报文 → 运输层加首部 → 网络层加首部（含<strong>源 IP、目的 IP</strong>）→ 数据链路层加首部（含<strong>源 MAC、目的 MAC</strong>）和尾部 → 物理层视为比特流。各层看不懂也无需看懂上层 PDU 的内容，只加自己的首部。

<figure class="cpd-figure not-content" id="cn-w3-fig-16">
  <svg viewBox="0 0 720 296" role="img" aria-label="数据包跨路由器转发过程中源目的IP地址端到端不变与源目的MAC地址逐跳改变对比">
    <defs>
      <marker id="cn-w3-arr-16" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="264" rx="8"/>
    <line class="fig-line-accent" x1="120" y1="88" x2="218" y2="88" marker-end="url(#cn-w3-arr-16)"/>
    <line class="fig-line-accent" x1="326" y1="88" x2="416" y2="88" marker-end="url(#cn-w3-arr-16)"/>
    <line class="fig-line-accent" x1="524" y1="88" x2="602" y2="88" marker-end="url(#cn-w3-arr-16)"/>
    <line class="fig-line-dashed" x1="170" y1="88" x2="142" y2="132"/>
    <line class="fig-line-dashed" x1="372" y1="88" x2="360" y2="132"/>
    <line class="fig-line-dashed" x1="564" y1="88" x2="578" y2="132"/>
    <rect class="fig-box-blue" x="28" y="62" width="92" height="52" rx="6"/>
    <rect class="fig-box-purple" x="220" y="62" width="106" height="52" rx="6"/>
    <rect class="fig-box-purple" x="418" y="62" width="106" height="52" rx="6"/>
    <rect class="fig-box-blue" x="604" y="62" width="88" height="52" rx="6"/>
    <rect class="fig-box" x="44" y="132" width="196" height="68" rx="5"/>
    <rect class="fig-box-orange" x="54" y="140" width="176" height="24" rx="3"/>
    <rect class="fig-box-green" x="54" y="168" width="176" height="24" rx="3"/>
    <rect class="fig-box" x="262" y="132" width="196" height="68" rx="5"/>
    <rect class="fig-box-orange" x="272" y="140" width="176" height="24" rx="3"/>
    <rect class="fig-box-green" x="272" y="168" width="176" height="24" rx="3"/>
    <rect class="fig-box" x="480" y="132" width="196" height="68" rx="5"/>
    <rect class="fig-box-orange" x="490" y="140" width="176" height="24" rx="3"/>
    <rect class="fig-box-green" x="490" y="168" width="176" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="214" width="664" height="52" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">跨网络分组转发核心规律：IP 地址端到端不变，MAC 地址逐跳重封装</text>
    <text class="fig-blue" x="74" y="84" text-anchor="middle" font-size="12">主机 H1</text>
    <text class="fig-muted" x="74" y="102" text-anchor="middle" font-size="10">IP1 / MAC1</text>
    <text class="fig-purple" x="273" y="84" text-anchor="middle" font-size="12">路由器 R1</text>
    <text class="fig-muted" x="273" y="102" text-anchor="middle" font-size="10">MAC3 ｜ MAC4</text>
    <text class="fig-purple" x="471" y="84" text-anchor="middle" font-size="12">路由器 R2</text>
    <text class="fig-muted" x="471" y="102" text-anchor="middle" font-size="10">MAC5 ｜ MAC6</text>
    <text class="fig-blue" x="648" y="84" text-anchor="middle" font-size="12">主机 H2</text>
    <text class="fig-muted" x="648" y="102" text-anchor="middle" font-size="10">IP2 / MAC2</text>
    <text class="fig-muted" x="170" y="78" text-anchor="middle" font-size="10">局域网 1</text>
    <text class="fig-muted" x="372" y="78" text-anchor="middle" font-size="10">链路 2</text>
    <text class="fig-muted" x="564" y="78" text-anchor="middle" font-size="10">局域网 3</text>
    <text class="fig-orange" x="142" y="156" text-anchor="middle" font-size="10">帧头：MAC1 → MAC3</text>
    <text class="fig-green" x="142" y="184" text-anchor="middle" font-size="10">IP头：IP1 → IP2（不变）</text>
    <text class="fig-orange" x="360" y="156" text-anchor="middle" font-size="10">帧头：MAC4 → MAC5</text>
    <text class="fig-green" x="360" y="184" text-anchor="middle" font-size="10">IP头：IP1 → IP2（不变）</text>
    <text class="fig-orange" x="578" y="156" text-anchor="middle" font-size="10">帧头：MAC6 → MAC2</text>
    <text class="fig-green" x="578" y="184" text-anchor="middle" font-size="10">IP头：IP1 → IP2（不变）</text>
    <text class="fig-label" x="42" y="235" font-size="11">• 路由器转发每跳重写源/目的 MAC；二层交换机、网桥或集线器转发帧时绝不修改源/目的 MAC 地址！</text>
    <text class="fig-muted" x="42" y="254" font-size="11">• 已知下一跳 IP（如 R1 接口 IP）未知其 MAC3 时，H1 在局域网 1 内发广播 ARP 请求（FF-FF-FF-FF-FF-FF）解析</text>
  </svg>
  <figcaption>图 3-16：H1 → R1 → R2 → H2 三段链路转发中内层 IP 地址恒定与外层 MAC 帧地址逐跳更新对照</figcaption>
</figure>

| 转发段 | 源 IP | 目的 IP | 源 MAC | 目的 MAC |
|---|---|---|---|---|
| H1 → R1 | IP1（H1） | IP2（H2） | MAC1（H1） | MAC3（R1 接口） |
| R1 → R2 | IP1 | IP2 | MAC4（R1 接口） | MAC5（R2 接口） |
| R2 → H2 | IP1 | IP2 | MAC6（R2 接口） | MAC2（H2） |

> <strong>★考点</strong>：IP 地址端到端不变、MAC 地址逐跳改变（2018 年真题的填表依据）。

### 3.11 以太网交换机的自学习与转发

以太网交换机工作在<strong>数据链路层</strong>（部分功能的交换机称为三层交换机，其部分功能在网络层）。它收到帧后在<strong>帧交换表</strong>中查找该帧目的 MAC 地址对应的接口号，再从该接口转发。交换机是<strong>即插即用</strong>设备：刚上电时帧交换表为空，随着各主机通信自动逐渐建立。

<figure class="cpd-figure not-content" id="cn-w3-fig-17">
  <svg viewBox="0 0 720 338" role="img" aria-label="以太网交换机逆向自学习、泛洪、明确转发、同端口过滤丢弃与帧交换表演化追踪">
    <defs>
      <marker id="cn-w3-arr-17" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="306" rx="8"/>
    <line class="fig-line-accent" x1="82" y1="70" x2="134" y2="70" marker-end="url(#cn-w3-arr-17)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="136" y1="118" x2="84" y2="118" marker-end="url(#cn-w3-arr-17)"/>
    <line class="fig-line-dashed" x1="136" y1="166" x2="84" y2="166" marker-end="url(#cn-w3-arr-17)"/>
    <line class="fig-line-dashed" x1="224" y1="118" x2="292" y2="118" marker-end="url(#cn-w3-arr-17)"/>
    <line class="fig-line" x1="382" y1="70" x2="434" y2="70"/>
    <line class="fig-line" x1="382" y1="118" x2="434" y2="118"/>
    <line class="fig-line" x1="382" y1="166" x2="434" y2="166"/>
    <rect class="fig-box-blue" x="28" y="56" width="54" height="28" rx="4"/>
    <rect class="fig-box-green" x="28" y="104" width="54" height="28" rx="4"/>
    <rect class="fig-box" x="28" y="152" width="54" height="28" rx="4"/>
    <rect class="fig-box-accent" x="136" y="50" width="88" height="136" rx="6"/>
    <rect class="fig-box-purple" x="294" y="50" width="88" height="136" rx="6"/>
    <rect class="fig-box" x="436" y="56" width="54" height="28" rx="4"/>
    <rect class="fig-box" x="436" y="104" width="54" height="28" rx="4"/>
    <rect class="fig-box" x="436" y="152" width="54" height="28" rx="4"/>
    <rect class="fig-box" x="502" y="50" width="190" height="136" rx="6"/>
    <line class="fig-divider" x1="502" y1="74" x2="692" y2="74"/>
    <line class="fig-divider" x1="502" y1="98" x2="692" y2="98"/>
    <rect class="fig-box-blue" x="510" y="103" width="174" height="22" rx="3"/>
    <rect class="fig-box-green" x="510" y="129" width="174" height="22" rx="3"/>
    <rect class="fig-box-purple" x="510" y="155" width="174" height="22" rx="3"/>
    <rect class="fig-box" x="28" y="198" width="664" height="110" rx="6"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">以太网交换机（透明网桥）逆向自学习与查表转发全流程</text>
    <text class="fig-blue" x="55" y="74" text-anchor="middle" font-size="11">主机 A</text>
    <text class="fig-green" x="55" y="122" text-anchor="middle" font-size="11">主机 B</text>
    <text class="fig-label" x="55" y="170" text-anchor="middle" font-size="11">主机 C</text>
    <text class="fig-accent" x="180" y="72" text-anchor="middle" font-size="11">交换机 SW1</text>
    <text class="fig-muted" x="148" y="88" font-size="10">口1</text>
    <text class="fig-muted" x="148" y="122" font-size="10">口2</text>
    <text class="fig-muted" x="148" y="170" font-size="10">口3</text>
    <text class="fig-muted" x="200" y="114" font-size="10">口4</text>
    <text class="fig-muted" x="259" y="110" text-anchor="middle" font-size="10">级联链路</text>
    <text class="fig-purple" x="338" y="72" text-anchor="middle" font-size="11">交换机 SW2</text>
    <text class="fig-muted" x="302" y="114" font-size="10">口4</text>
    <text class="fig-muted" x="360" y="88" font-size="10">口1</text>
    <text class="fig-muted" x="360" y="122" font-size="10">口2</text>
    <text class="fig-muted" x="360" y="170" font-size="10">口3</text>
    <text class="fig-label" x="463" y="74" text-anchor="middle" font-size="11">主机 D</text>
    <text class="fig-label" x="463" y="122" text-anchor="middle" font-size="11">主机 E</text>
    <text class="fig-label" x="463" y="170" text-anchor="middle" font-size="11">主机 F</text>
    <text class="fig-label" x="597" y="67" text-anchor="middle" font-size="11">SW1 帧交换表（动态老化）</text>
    <text class="fig-muted" x="597" y="90" text-anchor="middle" font-size="10">MAC　｜接口｜学习来源</text>
    <text class="fig-blue" x="597" y="118" text-anchor="middle" font-size="10">MAC_A ｜ 1 ｜ ① A→B 登记</text>
    <text class="fig-green" x="597" y="144" text-anchor="middle" font-size="10">MAC_B ｜ 2 ｜ ② B→A 登记</text>
    <text class="fig-purple" x="597" y="170" text-anchor="middle" font-size="10">MAC_E ｜ 4 ｜ ③ E→A 登记</text>
    <text class="fig-label" x="42" y="218" font-size="11">① 逆向学习源地址：收到帧必定先把（源 MAC，入接口，有效时间戳）写入/刷新帧交换表</text>
    <text class="fig-label" x="42" y="240" font-size="11">② 查目的地址转发：未命中（或广播帧）→ 向除入接口外所有接口泛洪；命中接口 k≠入接口 → 仅从 k 明确转发</text>
    <text class="fig-orange" x="42" y="262" font-size="11">③ 同接口过滤丢弃：若查得目的接口 k == 入接口（如同一共享总线段内两主机互发），交换机直接丢弃不转发！</text>
    <text class="fig-muted" x="42" y="284" font-size="11">④ 防兜圈广播风暴：多交换机冗余互联成环时，须运行生成树协议 STP（IEEE 802.1D）阻塞冗余端口破除物理环路</text>
  </svg>
  <figcaption>图 3-17：以太网交换机自学习四原则（登记源 MAC、未命中泛洪、命中单播、同口过滤）与 STP 生成树防环</figcaption>
</figure>

<strong>转发流程</strong>（以主机 A 给 B 发帧为例）：

1. <strong>登记（自学习）</strong>：帧从交换机 1 的接口 1 进入，交换机把<strong>源 MAC 地址 A + 入接口号 1</strong> 记入帧交换表。
2. <strong>查表转发</strong>：目的 MAC 为 B，表中查不到 → <strong>盲目转发（泛洪）</strong>，从除入接口外的所有接口转发出去。主机 D、E、F 收到后据目的 MAC 判断不是给自己的，丢弃。
3. <strong>明确转发</strong>：B 回帧给 A 时，帧从接口 3 进入，交换机先登记 B 与接口 3；再查目的 MAC A，命中接口 1，只从接口 1 转发。
4. <strong>丢弃</strong>：若帧进入的接口与查表得到的出接口相同（如挂在同一总线上的主机 A 与主机 E 互发，帧从接口 1 进出），交换机不再从该接口转发，直接丢弃。

<strong>表项有效期</strong>：帧交换表每条记录都有有效时间，到期自动删除。因为 MAC 地址与接口的对应关系并非永久——接口所连主机被更换、主机网卡损坏后换新网卡，都会改变对应关系（ARP 高速缓存中的 IP—MAC 对应记录同理）。

> <strong>★考点</strong>：交换机转发的是<strong>帧</strong>（PDU 为帧）；MAC 地址又称硬件地址/物理地址，但属于数据链路层（2009、2014 年真题）。

### 3.12 虚拟局域网 VLAN 的实现机制

VLAN 在交换机上实现，需要交换机具备两大功能：① 能处理<strong>带有 VLAN 标记的帧（802.1Q 帧）</strong>；② 各端口可支持<strong>不同的端口类型</strong>，不同类型对帧的处理方式不同。

<strong>802.1Q 帧（dot1q 帧）</strong>：在以太网 V2 MAC 帧格式中<strong>插入 4 字节 VLAN 标记</strong>。

<figure class="cpd-figure not-content" id="cn-w3-fig-18">
  <svg viewBox="0 0 720 312" role="img" aria-label="IEEE 802.1Q VLAN帧在源地址与类型字段间插入4字节标签TPID 0x8100及PCP 3b、DEI 1b、VID 12b详解">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="280" rx="8"/>
    <line class="fig-line-dashed" x1="276" y1="146" x2="40" y2="174"/>
    <line class="fig-line-dashed" x1="380" y1="146" x2="688" y2="174"/>
    <rect class="fig-box-blue" x="92" y="54" width="96" height="34"/>
    <rect class="fig-box-blue" x="188" y="54" width="96" height="34"/>
    <rect class="fig-box-purple" x="284" y="54" width="72" height="34"/>
    <rect class="fig-box-green" x="356" y="54" width="244" height="34"/>
    <rect class="fig-box-orange" x="600" y="54" width="88" height="34"/>
    <rect class="fig-box-blue" x="92" y="110" width="92" height="36"/>
    <rect class="fig-box-blue" x="184" y="110" width="92" height="36"/>
    <rect class="fig-box-red" x="276" y="110" width="104" height="36"/>
    <rect class="fig-box-purple" x="380" y="110" width="68" height="36"/>
    <rect class="fig-box-green" x="448" y="110" width="156" height="36"/>
    <rect class="fig-box-orange" x="604" y="110" width="84" height="36"/>
    <rect class="fig-box-purple" x="40" y="174" width="188" height="54" rx="4"/>
    <rect class="fig-box-blue" x="228" y="174" width="132" height="54" rx="4"/>
    <rect class="fig-box-orange" x="360" y="174" width="124" height="54" rx="4"/>
    <rect class="fig-box-green" x="484" y="174" width="204" height="54" rx="4"/>
    <rect class="fig-box" x="40" y="240" width="648" height="42" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">IEEE 802.1Q VLAN 标记帧格式与 4 字节 Tag 子字段逐位解析</text>
    <text class="fig-muted" x="56" y="75" text-anchor="middle" font-size="11">普通帧</text>
    <text class="fig-blue" x="140" y="75" text-anchor="middle" font-size="11">目的地址 6B</text>
    <text class="fig-blue" x="236" y="75" text-anchor="middle" font-size="11">源地址 6B</text>
    <text class="fig-purple" x="320" y="75" text-anchor="middle" font-size="11">类型 2B</text>
    <text class="fig-green" x="478" y="75" text-anchor="middle" font-size="11">数据载荷（46～1500 B）</text>
    <text class="fig-orange" x="644" y="75" text-anchor="middle" font-size="11">FCS 4B</text>
    <text class="fig-red" x="56" y="132" text-anchor="middle" font-size="11">802.1Q</text>
    <text class="fig-blue" x="138" y="132" text-anchor="middle" font-size="11">目的地址 6B</text>
    <text class="fig-blue" x="230" y="132" text-anchor="middle" font-size="11">源地址 6B</text>
    <text class="fig-red" x="328" y="132" text-anchor="middle" font-size="11">VLAN 标记 4B</text>
    <text class="fig-purple" x="414" y="132" text-anchor="middle" font-size="11">类型 2B</text>
    <text class="fig-green" x="526" y="132" text-anchor="middle" font-size="11">数据（46～1500 B）</text>
    <text class="fig-orange" x="646" y="132" text-anchor="middle" font-size="11">新 FCS 4B</text>
    <text class="fig-purple" x="134" y="196" text-anchor="middle" font-size="12">TPID（16b = 2B）</text>
    <text class="fig-muted" x="134" y="216" text-anchor="middle" font-size="10">固定取 0x8100</text>
    <text class="fig-blue" x="294" y="196" text-anchor="middle" font-size="12">PCP 3b</text>
    <text class="fig-muted" x="294" y="216" text-anchor="middle" font-size="10">优先级 0～7</text>
    <text class="fig-orange" x="422" y="196" text-anchor="middle" font-size="12">DEI 1b</text>
    <text class="fig-muted" x="422" y="216" text-anchor="middle" font-size="10">丢弃合格位</text>
    <text class="fig-green" x="586" y="196" text-anchor="middle" font-size="12">VID 12b（VLAN ID）</text>
    <text class="fig-muted" x="586" y="216" text-anchor="middle" font-size="10">有效值 1～4094（0/4095 保留）</text>
    <text class="fig-label" x="364" y="258" text-anchor="middle" font-size="11">• 插入/剥离 4 B 标记后必须重新计算 4 B FCS；最大帧长由 1518 B 增至 1522 B</text>
    <text class="fig-muted" x="364" y="275" text-anchor="middle" font-size="10">• 802.1Q 帧仅在交换机间流转，终端主机只识别普通以太网帧（收到带标帧会丢弃）</text>
  </svg>
  <figcaption>图 3-18：IEEE 802.1Q 帧在源 MAC 后插入 4 字节标记（2B TPID 0x8100 + 2B TCI：PCP 3b｜DEI 1b｜VID 12b）</figcaption>
</figure>

- 标记最后 <strong>12 比特为 VLAN 标识符 VID，唯一标识帧属于哪个 VLAN；VID 取值 0～4095，其中 0 和 4095 都不用来表示 VLAN，有效范围 1～4094</strong>。
- 802.1Q 帧由<strong>交换机</strong>处理，不是用户主机处理。交换机收到普通以太网帧时插入 4 字节标记——<strong>打标签</strong>；转发 802.1Q 帧时可能删除标记——<strong>去标签</strong>。
- <strong>PVID（端口 VLAN ID）</strong>：Cisco 称<strong>本征 VLAN</strong>，华为称<strong>端口 VLAN ID</strong>。交换机的每个端口<strong>有且仅有一个 PVID</strong>；默认所有端口属于 VLAN 1，即 PVID = 1。

<strong>三种端口类型</strong>：

| 类型 | 用途 | 接收处理 | 发送处理 |
|---|---|---|---|
| access | 连接用户计算机；只能属于一个 VLAN，PVID 等于所属 VLAN ID | 未打标签的普通帧 → 按端口 PVID 打标签 | VID = PVID → 去标签转发；否则不转发 |
| trunk | 交换机之间、交换机与路由器之间；可属于多个 VLAN，PVID 默认 1 | 未打标签 → 按 PVID 打标签；已打标签 802.1Q 帧 → 直接接收 | VID = PVID → 去标签转发；VID ≠ PVID → 带标签直接转发 |
| hybrid | 既可用于交换机互联（同 trunk），也可连用户计算机（同 access）；Cisco 交换机无此类型 | 同 trunk | 查该帧 VID 是否在端口<strong>去标签列表</strong>中：在 → 去标签转发；不在 → 带标签直接转发 |

<figure class="cpd-figure not-content" id="cn-w3-fig-19">
  <svg viewBox="0 0 720 322" role="img" aria-label="两台802.1Q交换机Access与Trunk端口按PVID打标签与去标签跨机转发广播帧流程">
    <defs>
      <marker id="cn-w3-arr-19" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="290" rx="8"/>
    <line class="fig-line-accent" x1="110" y1="76" x2="174" y2="76" marker-end="url(#cn-w3-arr-19)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="110" y1="146" x2="174" y2="146" marker-end="url(#cn-w3-arr-19)"/>
    <line class="fig-line-accent" x1="292" y1="84" x2="426" y2="84" marker-end="url(#cn-w3-arr-19)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="292" y1="138" x2="426" y2="138" marker-end="url(#cn-w3-arr-19)"/>
    <line class="fig-line-accent" x1="544" y1="76" x2="608" y2="76" marker-end="url(#cn-w3-arr-19)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="544" y1="146" x2="608" y2="146" marker-end="url(#cn-w3-arr-19)"/>
    <rect class="fig-box-blue" x="28" y="60" width="82" height="32" rx="4"/>
    <rect class="fig-box-green" x="28" y="130" width="82" height="32" rx="4"/>
    <rect class="fig-box-accent" x="176" y="50" width="116" height="126" rx="6"/>
    <rect class="fig-box-accent" x="428" y="50" width="116" height="126" rx="6"/>
    <rect class="fig-box-blue" x="610" y="60" width="82" height="32" rx="4"/>
    <rect class="fig-box-green" x="610" y="130" width="82" height="32" rx="4"/>
    <rect class="fig-box-orange" x="312" y="144" width="96" height="22" rx="3"/>
    <rect class="fig-box" x="28" y="190" width="664" height="100" rx="6"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">802.1Q 虚拟局域网 Access / Trunk 端口打标签（Tag）与去标签（Untag）流程</text>
    <text class="fig-blue" x="69" y="80" text-anchor="middle" font-size="11">A (VLAN 1)</text>
    <text class="fig-green" x="69" y="150" text-anchor="middle" font-size="11">C (VLAN 2)</text>
    <text class="fig-muted" x="142" y="68" text-anchor="middle" font-size="9">无标帧</text>
    <text class="fig-muted" x="142" y="138" text-anchor="middle" font-size="9">无标帧</text>
    <text class="fig-accent" x="234" y="68" text-anchor="middle" font-size="11">交换机 SW1</text>
    <text class="fig-blue" x="234" y="92" text-anchor="middle" font-size="10">口1 Access: PVID=1</text>
    <text class="fig-purple" x="234" y="116" text-anchor="middle" font-size="10">口5 Trunk: PVID=1</text>
    <text class="fig-green" x="234" y="154" text-anchor="middle" font-size="10">口3 Access: PVID=2</text>
    <text class="fig-blue" x="360" y="76" text-anchor="middle" font-size="10">VID=1=PVID → 去标发</text>
    <text class="fig-green" x="360" y="131" text-anchor="middle" font-size="10">VID=2≠PVID → 带标发</text>
    <text class="fig-orange" x="360" y="159" text-anchor="middle" font-size="10">802.1Q (VID=2)</text>
    <text class="fig-accent" x="486" y="68" text-anchor="middle" font-size="11">交换机 SW2</text>
    <text class="fig-blue" x="486" y="92" text-anchor="middle" font-size="10">口1 Access: PVID=1</text>
    <text class="fig-purple" x="486" y="116" text-anchor="middle" font-size="10">口5 Trunk: PVID=1</text>
    <text class="fig-green" x="486" y="154" text-anchor="middle" font-size="10">口3 Access: PVID=2</text>
    <text class="fig-muted" x="577" y="68" text-anchor="middle" font-size="9">去标发</text>
    <text class="fig-muted" x="577" y="138" text-anchor="middle" font-size="9">去标发</text>
    <text class="fig-blue" x="651" y="80" text-anchor="middle" font-size="11">E (VLAN 1)</text>
    <text class="fig-green" x="651" y="150" text-anchor="middle" font-size="11">G (VLAN 2)</text>
    <text class="fig-label" x="42" y="212" font-size="11">• Access 端口（连主机）：入方向给无标帧打上端口 PVID；出方向仅当 VID == PVID 时剥离标签发出（跨 VLAN 广播被阻断）</text>
    <text class="fig-label" x="42" y="234" font-size="11">• Trunk 端口（交换机级联，默认 PVID=1）：出方向若 VID == PVID 去标发，若 VID ≠ PVID 保留 802.1Q 标签跨机透传</text>
    <text class="fig-muted" x="42" y="256" font-size="11">• Hybrid 端口（混合口）：出方向查“去标签列表”，在列表中则去标发（主机可收），不在列表中则带标发（主机无法识别而丢弃）</text>
    <text class="fig-orange" x="42" y="276" font-size="11">• 核心结论：同一 VLAN 可跨交换机组成一个逻辑广播域；不同 VLAN 之间二层完全隔离，互通必须经路由器或三层交换机！</text>
  </svg>
  <figcaption>图 3-19：Access / Trunk / Hybrid 三类端口依据 PVID 打标与去标转发机制及跨交换机 VLAN 广播隔离</figcaption>
</figure>

<strong>access 端口举例</strong>（要求：主机 A、B 划归 VLAN 2，C、D 划归 VLAN 3）：

1. 交换机上创建 VLAN 2、VLAN 3；端口 1、2 划归 VLAN 2（PVID=2），端口 3、4 划归 VLAN 3（PVID=3）。
2. 主机 A 发广播帧，从端口 1 进入。access 端口对未打标签帧<strong>按 PVID 打标签</strong>，插入的 VID = 2。
3. 该帧 VID 与端口 2 的 PVID 相等（都是 2），于是从端口 2 <strong>去标签转发</strong>；端口 3、4 的 PVID 为 3，不转发。C 发广播帧时同理，只从端口 4 去标签转发。

<strong>trunk 端口举例</strong>（两台交换机互联，A、B、E、F 属 VLAN 1，C、D、G、H 属 VLAN 2）：

- 两交换机的端口 1、2 保持默认（VLAN 1，PVID=1），端口 3、4 划归 VLAN 2（PVID=2）；<strong>互联的端口 5 类型改为 trunk，PVID 保持 1</strong>。
- A 发广播帧：端口 1 打标签 VID=1；端口 2（access）去标签转发；端口 5 是 trunk 且 VID = PVID = 1 → <strong>去标签转发</strong>到另一台交换机，对方端口 5 收到未打标签帧后按 PVID=1 再打标签，从端口 1、2 去标签转发。
- C 发广播帧：端口 3 打标签 VID=2；端口 4（access，PVID=2）去标签转发；端口 5 的 PVID=1，<strong>VID ≠ PVID → 带标签直接转发</strong>；对方端口 5 收到已打标签的 802.1Q 帧直接接收，再按 VID=2 从端口 3、4 去标签转发。

> <strong>注意：由多台交换机互联构成交换式以太网划分 VLAN 时，连接主机的端口设为 access，交换机之间互联的端口设为 trunk</strong>。

<strong>hybrid 端口</strong>：功能绝大部分与 trunk 相同，区别在发送处理——查帧的 VID 是否在端口的<strong>去标签列表</strong>中，存在则去标签后转发，不存在则带标签直接转发。

应用实例：主机 A、B、C 连在同一交换机的三个端口，要求 A、B 都能与 C 通信，但 A 与 B 不能通信。把三个端口划入不同 VLAN（如 VLAN 10、20、30），类型设为 hybrid，PVID 分别为 10、20、30，去标签列表配置为：端口 1 含 VLAN 10、30，端口 2 含 VLAN 20、30，端口 3 含 VLAN 10、20、30。

- A 给 C 发帧：端口 1 按 PVID=10 打标签，从端口 3 转发；VID=10 在端口 3 的去标签列表中 → 去标签转发，C 正常收到。
- B 给 C 发帧、C 给 A/B 发帧同理，均可互通。
- A 给 B 发帧：端口 1 打标签 VID=10，从端口 2 转发；VID=10 <strong>不在</strong>端口 2 的去标签列表中 → 带标签直接转发。B 只能识别普通以太网 MAC 帧，收到 802.1Q 帧后无法识别而丢弃，于是 A 与 B 不能通信。

<figure class="cpd-figure not-content" id="cn-w3-fig-20">
  <svg viewBox="0 0 720 318" role="img" aria-label="集线器Hub、以太网交换机Switch、VLAN与路由器Router隔离冲突域和广播域能力对比拓扑图">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="286" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="408" height="238" rx="6"/>
    <rect class="fig-box" x="448" y="48" width="244" height="238" rx="6"/>
    <rect class="fig-box-dashed" x="40" y="102" width="154" height="148" rx="6"/>
    <rect class="fig-box-dashed" x="206" y="102" width="108" height="148" rx="6"/>
    <rect class="fig-box-dashed" x="320" y="102" width="104" height="148" rx="6"/>
    <line class="fig-line" x1="194" y1="76" x2="116" y2="76"/>
    <line class="fig-line" x1="116" y1="76" x2="116" y2="124"/>
    <line class="fig-line" x1="274" y1="76" x2="314" y2="76"/>
    <line class="fig-line" x1="314" y1="76" x2="314" y2="124"/>
    <line class="fig-line" x1="92" y1="154" x2="76" y2="196"/>
    <line class="fig-line" x1="140" y1="154" x2="156" y2="196"/>
    <line class="fig-line" x1="276" y1="154" x2="260" y2="196"/>
    <line class="fig-line" x1="352" y1="154" x2="372" y2="196"/>
    <rect class="fig-box-purple" x="194" y="58" width="80" height="34" rx="5"/>
    <rect class="fig-box-red" x="62" y="124" width="108" height="30" rx="4"/>
    <rect class="fig-box-blue" x="220" y="124" width="188" height="30" rx="4"/>
    <rect class="fig-box" x="50" y="196" width="52" height="28" rx="4"/>
    <rect class="fig-box" x="130" y="196" width="52" height="28" rx="4"/>
    <rect class="fig-box-blue" x="234" y="196" width="56" height="28" rx="4"/>
    <rect class="fig-box-green" x="344" y="196" width="56" height="28" rx="4"/>
    <rect class="fig-box-red" x="460" y="76" width="220" height="44" rx="4"/>
    <rect class="fig-box-blue" x="460" y="128" width="220" height="44" rx="4"/>
    <rect class="fig-box-green" x="460" y="180" width="220" height="44" rx="4"/>
    <rect class="fig-box-purple" x="460" y="232" width="220" height="44" rx="4"/>
    <rect class="fig-box" x="54" y="105" width="126" height="14" rx="3"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">408 必考综合：集线器、交换机（含 VLAN）与路由器的冲突域/广播域划分</text>
    <text class="fig-purple" x="234" y="79" text-anchor="middle" font-size="11">路由器 R</text>
    <text class="fig-muted" x="117" y="116" text-anchor="middle" font-size="10">广播域 #1（1 个冲突域）</text>
    <text class="fig-red" x="116" y="143" text-anchor="middle" font-size="11">集线器 Hub</text>
    <text class="fig-label" x="76" y="214" text-anchor="middle" font-size="11">H1</text>
    <text class="fig-label" x="156" y="214" text-anchor="middle" font-size="11">H2</text>
    <text class="fig-muted" x="117" y="240" text-anchor="middle" font-size="9">Hub 所有口合并为 1 冲突域</text>
    <text class="fig-blue" x="314" y="143" text-anchor="middle" font-size="10">交换机 SW（划 VLAN2 / VLAN3）</text>
    <text class="fig-blue" x="260" y="116" text-anchor="middle" font-size="9">广播域 #2</text>
    <text class="fig-green" x="372" y="116" text-anchor="middle" font-size="9">广播域 #3</text>
    <text class="fig-blue" x="262" y="214" text-anchor="middle" font-size="10">H3 (V2)</text>
    <text class="fig-green" x="372" y="214" text-anchor="middle" font-size="10">H4 (V3)</text>
    <text class="fig-muted" x="260" y="240" text-anchor="middle" font-size="9">独立冲突域</text>
    <text class="fig-muted" x="372" y="240" text-anchor="middle" font-size="9">独立冲突域</text>
    <text class="fig-label" x="232" y="272" text-anchor="middle" font-size="10">图示共 4 个冲突域（左1 + 右上1 + H3 + H4）、3 个广播域</text>
    <text class="fig-label" x="570" y="66" text-anchor="middle" font-size="11">各层设备隔离能力速查</text>
    <text class="fig-red" x="570" y="94" text-anchor="middle" font-size="11">物理层：中继器 / 集线器 Hub</text>
    <text class="fig-muted" x="570" y="111" text-anchor="middle" font-size="10">隔离冲突域 × ｜ 隔离广播域 ×</text>
    <text class="fig-blue" x="570" y="146" text-anchor="middle" font-size="11">链路层：网桥 / 普通交换机</text>
    <text class="fig-muted" x="570" y="163" text-anchor="middle" font-size="10">隔离冲突域 ✓ ｜ 隔离广播域 ×</text>
    <text class="fig-green" x="570" y="198" text-anchor="middle" font-size="11">链路层：划分 VLAN 的交换机</text>
    <text class="fig-muted" x="570" y="215" text-anchor="middle" font-size="10">隔离冲突域 ✓ ｜ 按 VLAN 隔广播 ✓</text>
    <text class="fig-purple" x="570" y="250" text-anchor="middle" font-size="11">网络层：路由器 Router</text>
    <text class="fig-muted" x="570" y="267" text-anchor="middle" font-size="10">隔离冲突域 ✓ ｜ 隔离广播域 ✓</text>
  </svg>
  <figcaption>图 3-20：物理层（Hub）、数据链路层（交换机与 VLAN）及网络层（路由器）冲突域与广播域计数规则总结</figcaption>
</figure>

## 第4章 网络层

网络层向上提供<strong>无连接、尽最大努力交付</strong>的数据报服务：可靠性由网络边缘的主机（及其运输层）负责，网络核心只做简单的存储转发。本章围绕“网际层如何传送 IP 数据报”展开：地址如何编、分组如何发与转发、路由如何选、首部如何组织。

### 4.1 网络层提供的两种服务

网络层的设计有两条路线：<strong>面向连接的虚电路服务</strong>（可靠通信由网络保证）与<strong>无连接的数据报服务</strong>（可靠通信由用户主机保证）。因特网选择了后者。

| 对比项 | 虚电路服务 | 数据报服务 |
|---|---|---|
| 可靠性由谁保证 | <strong>网络</strong>自身 | <strong>用户主机</strong> |
| 连接建立 | 先建立网络层连接（虚电路），通信结束释放 | 不需要先建立连接 |
| 连接性质 | 逻辑连接，分组仍按<strong>存储转发</strong>方式传送，不是真正的物理连接 | — |
| 分组首部地址 | 连接建立阶段用完整目的地址，之后每个分组只带<strong>虚电路号</strong> | 每个分组都必须带<strong>完整目的地址</strong> |
| 路径 | 所有分组沿同一条虚电路 | 每个分组可走<strong>不同路径</strong> |
| 传输差错 | 配合可靠传输协议时，分组最终正确到达 | 可能<strong>误码、丢失、重复、失序</strong> |
| 路由器 | 复杂、昂贵 | 简单、低廉，只做尽最大努力交付 |
| 典型 | X.25、帧中继、ATM | 因特网的网际层（IP） |

<figure class="cpd-figure not-content" id="cn-w4-fig-1">
  <svg viewBox="0 0 720 324" role="img" aria-label="网络层面向连接的虚电路服务与无连接的数据报服务工作机制对比图">
    <defs>
      <marker id="cn-w4-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="292" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="326" height="248" rx="6"/>
    <rect class="fig-box" x="366" y="48" width="326" height="248" rx="6"/>
    <line class="fig-line-dashed" x1="94" y1="152" x2="164" y2="176"/>
    <line class="fig-line-dashed" x1="218" y1="176" x2="288" y2="152"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="94" y1="136" x2="122" y2="130" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="178" y1="130" x2="202" y2="130" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="258" y1="130" x2="286" y2="136" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="432" y1="134" x2="460" y2="112" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="516" y1="112" x2="540" y2="112" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="596" y1="112" x2="624" y2="134" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="432" y1="152" x2="500" y2="158" marker-end="url(#cn-w4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="556" y1="158" x2="624" y2="152" marker-end="url(#cn-w4-arr-1)"/>
    <rect class="fig-box-purple" x="104" y="80" width="52" height="20" rx="3"/>
    <rect class="fig-box-purple" x="164" y="80" width="52" height="20" rx="3"/>
    <rect class="fig-box-purple" x="224" y="80" width="52" height="20" rx="3"/>
    <rect class="fig-box-blue" x="38" y="122" width="56" height="44" rx="5"/>
    <rect class="fig-box-purple" x="124" y="114" width="54" height="32" rx="5"/>
    <rect class="fig-box-purple" x="204" y="114" width="54" height="32" rx="5"/>
    <rect class="fig-box-subtle" x="164" y="162" width="54" height="28" rx="5"/>
    <rect class="fig-box-blue" x="288" y="122" width="56" height="44" rx="5"/>
    <rect class="fig-box-subtle" x="40" y="202" width="302" height="82" rx="4"/>
    <rect class="fig-box-blue" x="466" y="74" width="126" height="18" rx="3"/>
    <rect class="fig-box-orange" x="466" y="176" width="126" height="18" rx="3"/>
    <rect class="fig-box-blue" x="376" y="122" width="56" height="44" rx="5"/>
    <rect class="fig-box-orange" x="462" y="96" width="54" height="30" rx="5"/>
    <rect class="fig-box-orange" x="542" y="96" width="54" height="30" rx="5"/>
    <rect class="fig-box-orange" x="502" y="142" width="54" height="30" rx="5"/>
    <rect class="fig-box-green" x="626" y="122" width="56" height="44" rx="5"/>
    <rect class="fig-box-subtle" x="378" y="202" width="302" height="82" rx="4"/>
    <text class="fig-title" x="360" y="37" text-anchor="middle" font-size="13">网络层设计路线对比：面向连接的虚电路服务 vs 无连接的数据报服务</text>
    <text class="fig-purple" x="191" y="68" text-anchor="middle" font-size="12">① 虚电路服务（X.25 / 帧中继 / ATM）</text>
    <text class="fig-purple" x="130" y="94" text-anchor="middle" font-size="9.5">VC5|P3</text>
    <text class="fig-purple" x="190" y="94" text-anchor="middle" font-size="9.5">VC5|P2</text>
    <text class="fig-purple" x="250" y="94" text-anchor="middle" font-size="9.5">VC5|P1</text>
    <text class="fig-blue" x="66" y="141" text-anchor="middle" font-size="11">主机 H1</text>
    <text class="fig-muted" x="66" y="156" text-anchor="middle" font-size="9.5">简单终端</text>
    <text class="fig-purple" x="151" y="134" text-anchor="middle" font-size="10.5">R1(VC表)</text>
    <text class="fig-purple" x="231" y="134" text-anchor="middle" font-size="10.5">R2(VC表)</text>
    <text class="fig-muted" x="191" y="180" text-anchor="middle" font-size="10">R3(未选)</text>
    <text class="fig-blue" x="316" y="141" text-anchor="middle" font-size="11">主机 H2</text>
    <text class="fig-muted" x="316" y="156" text-anchor="middle" font-size="9.5">按序收齐</text>
    <text class="fig-label" x="48" y="219" font-size="10.5">• 通信前先建逻辑虚电路，分组仅带短虚电路号</text>
    <text class="fig-label" x="48" y="237" font-size="10.5">• 全部沿同一虚电路存储转发，天然按序到达</text>
    <text class="fig-purple" x="48" y="255" font-size="10.5">• 可靠通信由「网络核心」保证，节点状态复杂昂贵</text>
    <text class="fig-muted" x="48" y="273" font-size="10">• 逻辑连接 ≠ 电路交换物理独占链路（仍分组复用）</text>
    <text class="fig-blue" x="529" y="68" text-anchor="middle" font-size="12">② 数据报服务（因特网 IP 网际层）</text>
    <text class="fig-blue" x="529" y="87" text-anchor="middle" font-size="9.5">目的IP2 | P1, P3 走上路</text>
    <text class="fig-orange" x="529" y="189" text-anchor="middle" font-size="9.5">目的IP2 | P2 独立走下路</text>
    <text class="fig-blue" x="404" y="141" text-anchor="middle" font-size="11">主机 H1</text>
    <text class="fig-muted" x="404" y="156" text-anchor="middle" font-size="9.5">TCP重传</text>
    <text class="fig-orange" x="489" y="115" text-anchor="middle" font-size="10.5">R1(无状态)</text>
    <text class="fig-orange" x="569" y="115" text-anchor="middle" font-size="10.5">R2(无状态)</text>
    <text class="fig-orange" x="529" y="161" text-anchor="middle" font-size="10.5">R3(无状态)</text>
    <text class="fig-green" x="654" y="141" text-anchor="middle" font-size="11">主机 H2</text>
    <text class="fig-muted" x="654" y="156" text-anchor="middle" font-size="9.5">TCP排序</text>
    <text class="fig-label" x="386" y="219" font-size="10.5">• 无需建连，每分组必带完整 32 位目的 IP 地址</text>
    <text class="fig-label" x="386" y="237" font-size="10.5">• 独立查表可走不同路径，可能误码/丢失/重复/失序</text>
    <text class="fig-blue" x="386" y="255" font-size="10.5">• 可靠通信由「边缘用户主机」负责，核心简单廉价</text>
    <text class="fig-muted" x="386" y="273" font-size="10">• 定位：无连接、尽最大努力交付（Best-Effort）</text>
  </svg>
  <figcaption>图 4-1：网络层虚电路服务（网络核心保证可靠、单路径虚电路号转发）与 IP 数据报服务（边缘主机保证可靠、完整目的 IP 独立选路）对比</figcaption>
</figure>

数据报服务的代价是把复杂功能放到网络边缘，收益是造价低、运行方式灵活、能适应多种应用。

> <strong>★考点</strong>：两类服务的区别集中在“可靠性由谁保证”——虚电路靠网络，数据报靠主机；IP 网际层提供的是<strong>无连接、尽最大努力交付</strong>的服务，不保证不丢失、不保证按序。

### 4.2 无分类编址的 IPv4 地址（CIDR）

划分子网只是缓解地址紧张；数量巨大的 C 类网地址块太小、利用不充分，IPv4 地址空间仍面临耗尽。1993 年 IETF 发布<strong>无分类域间路由选择 CIDR</strong>（读作“si-der”）的 RFC，同时成立 IPv6 工作组。

CIDR 消除了传统 A、B、C 类地址和划分子网的概念，采用<strong>斜线记法</strong>：在 IPv4 地址后加 `/n`，`n` 是<strong>网络前缀</strong>占的比特数，剩余 $32-n$ 位为主机号。网络前缀相同的连续地址构成一个 <strong>CIDR 地址块</strong>——只要知道块中任一地址，就能推出该块的全部细节。

<figure class="cpd-figure not-content" id="cn-w4-fig-2">
  <svg viewBox="0 0 720 344" role="img" aria-label="IPv4分类编址A至E类格式、私有与特殊IP地址汇总及VLSM变长子网前缀树分配实例">
    <defs>
      <marker id="cn-w4-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="312" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="356" height="268" rx="6"/>
    <rect class="fig-box" x="396" y="48" width="296" height="268" rx="6"/>
    <line class="fig-line" x1="520" y1="104" x2="474" y2="128" marker-end="url(#cn-w4-arr-2)"/>
    <line class="fig-line" x1="576" y1="104" x2="616" y2="128" marker-end="url(#cn-w4-arr-2)"/>
    <line class="fig-line" x1="598" y1="158" x2="544" y2="188" marker-end="url(#cn-w4-arr-2)"/>
    <line class="fig-line" x1="634" y1="158" x2="646" y2="188" marker-end="url(#cn-w4-arr-2)"/>
    <line class="fig-line" x1="636" y1="218" x2="606" y2="244" marker-end="url(#cn-w4-arr-2)"/>
    <rect class="fig-box-blue" x="40" y="76" width="42" height="20" rx="3"/>
    <rect class="fig-box-accent" x="84" y="76" width="128" height="20" rx="3"/>
    <rect class="fig-box-subtle" x="214" y="76" width="158" height="20" rx="3"/>
    <rect class="fig-box-blue" x="40" y="100" width="42" height="20" rx="3"/>
    <rect class="fig-box-accent" x="84" y="100" width="160" height="20" rx="3"/>
    <rect class="fig-box-subtle" x="246" y="100" width="126" height="20" rx="3"/>
    <rect class="fig-box-blue" x="40" y="124" width="42" height="20" rx="3"/>
    <rect class="fig-box-accent" x="84" y="124" width="192" height="20" rx="3"/>
    <rect class="fig-box-subtle" x="278" y="124" width="94" height="20" rx="3"/>
    <rect class="fig-box-purple" x="40" y="148" width="46" height="20" rx="3"/>
    <rect class="fig-box-purple" x="88" y="148" width="284" height="20" rx="3"/>
    <rect class="fig-box-orange" x="40" y="172" width="46" height="20" rx="3"/>
    <rect class="fig-box-subtle" x="88" y="172" width="284" height="20" rx="3"/>
    <rect class="fig-box-subtle" x="40" y="200" width="332" height="104" rx="4"/>
    <rect class="fig-box-accent" x="478" y="78" width="140" height="26" rx="4"/>
    <rect class="fig-box-green" x="408" y="130" width="128" height="36" rx="4"/>
    <rect class="fig-box-blue" x="556" y="130" width="122" height="28" rx="4"/>
    <rect class="fig-box-green" x="476" y="190" width="126" height="36" rx="4"/>
    <rect class="fig-box-blue" x="612" y="190" width="68" height="28" rx="4"/>
    <rect class="fig-box-orange" x="526" y="246" width="154" height="32" rx="4"/>
    <text class="fig-title" x="360" y="37" text-anchor="middle" font-size="13">IPv4 分类编址、特殊/私有地址速查与 VLSM 变长子网前缀树分配</text>
    <text class="fig-blue" x="206" y="67" text-anchor="middle" font-size="12">① 传统 A–E 类与必背特殊/私有地址</text>
    <text class="fig-blue" x="61" y="90" text-anchor="middle" font-size="9.5">A: 0</text>
    <text class="fig-accent" x="148" y="90" text-anchor="middle" font-size="9.5">7b 网络 (1~126)</text>
    <text class="fig-label" x="293" y="90" text-anchor="middle" font-size="9.5">24b 主机 (2²⁴−2)</text>
    <text class="fig-blue" x="61" y="114" text-anchor="middle" font-size="9.5">B: 10</text>
    <text class="fig-accent" x="164" y="114" text-anchor="middle" font-size="9.5">14b 网络 (128.0~191.255)</text>
    <text class="fig-label" x="309" y="114" text-anchor="middle" font-size="9.5">16b 主机 (65534)</text>
    <text class="fig-blue" x="61" y="138" text-anchor="middle" font-size="9.5">C: 110</text>
    <text class="fig-accent" x="180" y="138" text-anchor="middle" font-size="9.5">21b 网络 (192.0.0~223.255.255)</text>
    <text class="fig-label" x="325" y="138" text-anchor="middle" font-size="9.5">8b (254)</text>
    <text class="fig-purple" x="63" y="162" text-anchor="middle" font-size="9.5">D:1110</text>
    <text class="fig-purple" x="230" y="162" text-anchor="middle" font-size="9.5">多播地址 224.0.0.0 ~ 239.255.255.255</text>
    <text class="fig-orange" x="63" y="186" text-anchor="middle" font-size="9.5">E:1111</text>
    <text class="fig-muted" x="230" y="186" text-anchor="middle" font-size="9.5">保留地址 240.0.0.0 ~ 255.255.255.255</text>
    <text class="fig-blue" x="46" y="216" font-size="9.5">• 私有网段：10.0.0.0/8 ｜ 172.16.0.0/12 ｜ 192.168.0.0/16</text>
    <text class="fig-label" x="46" y="235" font-size="9.5">• 特殊地址：0.0.0.0（本机源）｜ 127.x.y.z（环回测试不入网）</text>
    <text class="fig-label" x="46" y="254" font-size="9.5">• 广播地址：255.255.255.255（受限）｜ 主机全1（直接广播）</text>
    <text class="fig-orange" x="48" y="273" font-size="10">• 子网划分：借主机位作子网号，三级结构 &lt;网络,子网,主机&gt;</text>
    <text class="fig-muted" x="48" y="292" font-size="10">• 可分配主机 IP 数 = 2^(32−n) − 2（扣全0网络与全1广播）</text>
    <text class="fig-green" x="544" y="67" text-anchor="middle" font-size="12">② VLSM 变长子网二叉前缀树</text>
    <text class="fig-accent" x="548" y="95" text-anchor="middle" font-size="10.5">192.168.1.0/24 (256)</text>
    <text class="fig-muted" x="484" y="118" font-size="9.5">0</text>
    <text class="fig-muted" x="606" y="118" font-size="9.5">1</text>
    <text class="fig-green" x="472" y="145" text-anchor="middle" font-size="10">192.168.1.0/25</text>
    <text class="fig-muted" x="472" y="159" text-anchor="middle" font-size="9">子网A：126台主机</text>
    <text class="fig-blue" x="617" y="148" text-anchor="middle" font-size="10">.128/25 继续切分</text>
    <text class="fig-muted" x="556" y="176" font-size="9.5">10</text>
    <text class="fig-muted" x="648" y="176" font-size="9.5">11</text>
    <text class="fig-green" x="539" y="205" text-anchor="middle" font-size="10">192.168.1.128/26</text>
    <text class="fig-muted" x="539" y="219" text-anchor="middle" font-size="9">子网B：62台主机</text>
    <text class="fig-blue" x="646" y="208" text-anchor="middle" font-size="9.5">.192/26</text>
    <text class="fig-muted" x="600" y="236" font-size="9">110000</text>
    <text class="fig-orange" x="603" y="260" text-anchor="middle" font-size="10">.192/30 (点对点链路)</text>
    <text class="fig-muted" x="603" y="272" text-anchor="middle" font-size="8.5">仅 .193/.194 两个可用IP</text>
    <text class="fig-muted" x="544" y="302" text-anchor="middle" font-size="9.5">★ 已分配叶节点严禁再拆分，确保前缀互不重叠</text>
  </svg>
  <figcaption>图 4-2：IPv4 分类编址（A–E 类与私有/特殊地址）及利用 VLSM 二叉前缀树按需无重叠切分 `/25`、`/26`、`/30` 子网</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w4-fig-3">
  <svg viewBox="0 0 720 326" role="img" aria-label="CIDR斜线记法128.14.35.7/20的32位比特拆解、最小最大地址与掩码推导图">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="294" rx="8"/>
    <path class="fig-line-accent" d="M 40 66 L 40 58 L 438 58 L 438 66"/>
    <path class="fig-line-dashed" d="M 442 66 L 442 58 L 680 58 L 680 66"/>
    <rect class="fig-box-blue" x="40" y="70" width="400" height="34" rx="4"/>
    <rect class="fig-box-orange" x="440" y="70" width="240" height="34" rx="4"/>
    <line class="fig-divider" x1="200" y1="70" x2="200" y2="112"/>
    <line class="fig-divider" x1="360" y1="70" x2="360" y2="112"/>
    <line class="fig-divider" x1="520" y1="70" x2="520" y2="112"/>
    <rect class="fig-box-green" x="40" y="138" width="312" height="44" rx="5"/>
    <rect class="fig-box-orange" x="368" y="138" width="312" height="44" rx="5"/>
    <rect class="fig-box-purple" x="40" y="192" width="312" height="44" rx="5"/>
    <rect class="fig-box-accent" x="368" y="192" width="312" height="44" rx="5"/>
    <rect class="fig-box" x="40" y="246" width="640" height="50" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">128.14.35.7/20 地址块拆解：前 20 位网络前缀固定，后 12 位主机号变化</text>
    <text class="fig-accent" x="239" y="53" text-anchor="middle" font-size="10.5">网络前缀 n = 20 bit（固定不变）</text>
    <text class="fig-orange" x="561" y="53" text-anchor="middle" font-size="10.5">主机号 32−n = 12 bit（全0~全1）</text>
    <text class="fig-blue" x="120" y="92" text-anchor="middle" font-size="12.5">1000 0000</text>
    <text class="fig-blue" x="280" y="92" text-anchor="middle" font-size="12.5">0000 1110</text>
    <text class="fig-blue" x="400" y="92" text-anchor="middle" font-size="12.5">0010</text>
    <text class="fig-orange" x="480" y="92" text-anchor="middle" font-size="12.5">0011</text>
    <text class="fig-orange" x="600" y="92" text-anchor="middle" font-size="12.5">0000 0111</text>
    <text class="fig-label" x="120" y="124" text-anchor="middle" font-size="11">第1字节：128</text>
    <text class="fig-label" x="280" y="124" text-anchor="middle" font-size="11">第2字节：14</text>
    <text class="fig-accent" x="440" y="124" text-anchor="middle" font-size="11">第3字节：35（跨界字节）</text>
    <text class="fig-label" x="600" y="124" text-anchor="middle" font-size="11">第4字节：7</text>
    <text class="fig-green" x="52" y="156" font-size="10.5">最小地址（主机12b全0）：128.14.32.0</text>
    <text class="fig-muted" x="52" y="173" font-size="9.5">第3字节 0010 0000₂ = 32（网络地址）</text>
    <text class="fig-orange" x="380" y="156" font-size="10.5">最大地址（主机12b全1）：128.14.47.255</text>
    <text class="fig-muted" x="380" y="173" font-size="9.5">第3字节 0010 1111₂ = 47（直接广播地址）</text>
    <text class="fig-purple" x="52" y="210" font-size="10.5">地址掩码 /20：255.255.240.0</text>
    <text class="fig-muted" x="52" y="227" font-size="9.5">前20位全1 → 第3字节 1111 0000₂ = 240</text>
    <text class="fig-accent" x="380" y="210" font-size="10.5">地址总数：2¹² = 4096（可配主机 4094）</text>
    <text class="fig-muted" x="380" y="227" font-size="9.5">相当于聚合 C 类网数 = 2^(24−20) = 16 个</text>
    <text class="fig-label" x="52" y="266" font-size="10.5">• 解题关键在「跨界第 3 字节」：保持前 4 位 0010 不变，后 4 位填 0000 得下界 32，填 1111 得上界 47</text>
    <text class="fig-muted" x="52" y="285" font-size="10">• 特例警钟：192.168.4.0/30 共 4 个地址，扣除 .0 网络地址与 .3 广播地址，可分配主机与收广播主机数均为 2</text>
  </svg>
  <figcaption>图 4-3：`128.14.35.7/20` 地址块按第 20 位切分推导最小地址 `128.14.32.0`、最大地址 `128.14.47.255` 与掩码 `255.255.240.0`</figcaption>
</figure>

地址块的关键量可以直接算：

$$
N_{\mathrm{addr}} = 2^{\,32-n}
$$

$$
N_{\mathrm{classC}} = \frac{2^{32-n}}{2^{8}} = 2^{\,24-n}
$$

掩码是把前 $n$ 位置 1、后 $32-n$ 位置 0：

`/20` 掩码：前 20 位为 1、后 12 位为 0，即 `11111111.11111111.11110000.00000000` = `255.255.240.0`。

> <strong>★考点</strong>：给出 `a.b.c.d/n` 求最小地址、最大地址、地址数、掩码。做法固定——把前缀位保持不变，主机号<strong>全 0</strong> 得最小地址，<strong>全 1</strong> 得最大地址；地址数只看主机号位数。
>
> <strong>注意</strong>：`192.168.4.0/30` 只有 2 个可分配地址（192.168.4.1、192.168.4.2），192.168.4.3 是<strong>广播地址</strong>；目的地址为广播地址时，该网络内所有主机都收到，可收到分组的最大主机数为 2。

### 4.3 路由聚合与最长前缀匹配

路由器 R1 与 5 个网络直连，并和 R2 互为相邻路由器、周期性通告路由信息。若逐条通告，R2 的路由表要多 5 条记录；把这 5 个目的网络地址的<strong>共同前缀</strong>找出来（前 22 位相同），其余 10 位全 0，就聚合成一条 `/22` 路由——即<strong>构造超网</strong>。

<figure class="cpd-figure not-content" id="cn-w4-fig-4">
  <svg viewBox="0 0 720 316" role="img" aria-label="CIDR路由聚合构造超网：5条直连路由寻找前22位共同前缀聚合为1条/22路由">
    <defs>
      <marker id="cn-w4-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <path class="fig-line-accent" d="M 332 62 L 346 62 L 346 182 L 332 182"/>
    <line class="fig-line-accent" x1="346" y1="122" x2="386" y2="122" marker-end="url(#cn-w4-arr-4)"/>
    <rect class="fig-box" x="32" y="50" width="294" height="24" rx="3"/>
    <rect class="fig-box" x="32" y="80" width="294" height="24" rx="3"/>
    <rect class="fig-box" x="32" y="110" width="294" height="24" rx="3"/>
    <rect class="fig-box" x="32" y="140" width="294" height="24" rx="3"/>
    <rect class="fig-box" x="32" y="170" width="294" height="24" rx="3"/>
    <rect class="fig-box-accent" x="388" y="80" width="300" height="84" rx="6"/>
    <rect class="fig-box-blue" x="32" y="206" width="416" height="28" rx="4"/>
    <rect class="fig-box-orange" x="448" y="206" width="240" height="28" rx="4"/>
    <rect class="fig-box" x="32" y="242" width="656" height="46" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">路由聚合（构造超网）：5 条直连具体路由 → 提取 22 位共同前缀合为 1 条 /22</text>
    <text class="fig-label" x="42" y="66" font-size="10">① 206.0.68.0/24 → 第3字节 010001｜00</text>
    <text class="fig-label" x="42" y="96" font-size="10">② 206.0.69.0/24 → 第3字节 010001｜01</text>
    <text class="fig-label" x="42" y="126" font-size="10">③ 206.0.70.0/24 → 第3字节 010001｜10</text>
    <text class="fig-label" x="42" y="156" font-size="10">④ 206.0.71.0/25 → 第3字节 010001｜11.0…</text>
    <text class="fig-label" x="42" y="186" font-size="10">⑤ 206.0.71.128/25 → 第3字节 010001｜11.1…</text>
    <text class="fig-accent" x="538" y="104" text-anchor="middle" font-size="12">R1 通告 R2：206.0.68.0/22（超网）</text>
    <text class="fig-label" x="538" y="125" text-anchor="middle" font-size="10.5">前 16+6=22 bit 完全一致，第 23 bit 开始分叉</text>
    <text class="fig-muted" x="538" y="146" text-anchor="middle" font-size="10">共同前缀不变 + 剩余 10 bit 全置 0（减少路由表规模）</text>
    <text class="fig-blue" x="240" y="224" text-anchor="middle" font-size="10.5">共同前缀 22 bit（1~22 位：206.0 + 010001）</text>
    <text class="fig-orange" x="568" y="224" text-anchor="middle" font-size="10.5">剩余 10 bit 全取 0（23~32 位）</text>
    <text class="fig-label" x="44" y="261" font-size="10.5">• 聚合口诀：将首个不一致字节展开成 8 位二进制，从左数到第一个不全相同的位为止，后面全部清零</text>
    <text class="fig-muted" x="44" y="279" font-size="10">• 2018 真题：4 个目的网络在第 3 字节仅前 3 位相同 → 共同前缀长 16+3=19 位，聚合路由为 /19</text>
  </svg>
  <figcaption>图 4-4：路由器将 5 条直连明细路由按二进制对齐提取前 22 位共同前缀，聚合成 1 条 `/22` 超网路由通告邻居</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w4-fig-5">
  <svg viewBox="0 0 720 316" role="img" aria-label="最长前缀匹配查表追踪：目的IP同时命中默认路由、/22超网与/25明细路由时优先选择/25">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <rect class="fig-box-accent" x="32" y="50" width="656" height="32" rx="4"/>
    <rect class="fig-box" x="32" y="92" width="656" height="36" rx="4"/>
    <rect class="fig-box-blue" x="32" y="136" width="656" height="36" rx="4"/>
    <rect class="fig-box-green" x="32" y="180" width="656" height="36" rx="4"/>
    <rect class="fig-box" x="32" y="226" width="656" height="60" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">最长前缀匹配查表实例：多条路由同时匹配时选网络前缀 n 最长的一条</text>
    <text class="fig-accent" x="360" y="70" text-anchor="middle" font-size="11">待转发分组目的 IP：D = 206.0.70.130（第3–4字节二进制：01000110 . 10000010）</text>
    <text class="fig-muted" x="44" y="114" font-size="10.5">表项① 0.0.0.0/0（默认路由）｜ D AND 0.0.0.0 = 0.0.0.0 → 匹配（前缀长 n=0，兜底备选：下一跳 R4）</text>
    <text class="fig-blue" x="44" y="158" font-size="10.5">表项② 206.0.68.0/22（聚合超网）｜ D AND 255.255.252.0 = 206.0.68.0 → 匹配（前缀长 n=22：下一跳 R2）</text>
    <text class="fig-green" x="44" y="202" font-size="10">表项③ 206.0.70.128/25（迁出子网）｜ D AND 255.255.255.128 = 206.0.70.128 → ★ 选中（n=25 最长：下一跳 R3）</text>
    <text class="fig-label" x="44" y="247" font-size="10.5">• 规律：网络前缀 n 越长 → 地址块 2^(32−n) 越小 → 路由越具体；支持个别子网从聚合超网中分拆独立选路</text>
    <text class="fig-muted" x="44" y="268" font-size="10">• 两个极端：默认路由 0.0.0.0/0（前缀 0 位最短，无其他匹配时走它）｜ 特定主机路由 a.b.c.d/32（前缀 32 位最长）</text>
  </svg>
  <figcaption>图 4-5：目的地址 `206.0.70.130` 同时匹配 `/0`、`/22`、`/25` 三条表项时，按最长前缀匹配准则选择 `/25` 下一跳 R3</figcaption>
</figure>

> <strong>★考点</strong>：路由聚合题只做一件事——<strong>找共同前缀</strong>。共同前缀取到第一个不同的比特之前，聚合地址 = 共同前缀 + 后面所有位取 0，写成 `前缀地址/前缀长度`。

### 4.4 IP 数据报的发送与转发

同一网络内主机之间可以直接通信，称为<strong>直接交付</strong>；不同网络的主机之间必须由路由器中转，称为<strong>间接交付</strong>。主机判断“目的主机是否与自己同网”的方法固定：

$$
\text{local net address} = \text{local IP}\ \text{AND}\ \text{subnet mask}
$$

$$
\text{dest net address} = \text{dest IP}\ \text{AND}\ \text{same subnet mask}
$$

两者相等 → 直接交付；不等 → 间接交付，把数据报交给<strong>默认网关</strong>（本网络中某个路由器接口的 IP 地址）。

<figure class="cpd-figure not-content" id="cn-w4-fig-6">
  <svg viewBox="0 0 720 348" role="img" aria-label="主机发送IP数据报直接交付与间接交付判据及路由器查表转发完整控制流图">
    <defs>
      <marker id="cn-w4-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="316" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="320" height="270" rx="6"/>
    <rect class="fig-box" x="360" y="48" width="332" height="270" rx="6"/>
    <line class="fig-line" x1="114" y1="120" x2="114" y2="142" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line-accent" x1="262" y1="120" x2="262" y2="142" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line" x1="467" y1="108" x2="467" y2="124" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="558" y1="143" x2="580" y2="143" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line" x1="467" y1="160" x2="467" y2="178" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="463" y1="218" x2="463" y2="238" marker-end="url(#cn-w4-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="619" y1="218" x2="619" y2="238" marker-end="url(#cn-w4-arr-6)"/>
    <rect class="fig-box-blue" x="44" y="80" width="288" height="40" rx="4"/>
    <rect class="fig-box-green" x="44" y="144" width="140" height="58" rx="4"/>
    <rect class="fig-box-orange" x="192" y="144" width="140" height="58" rx="4"/>
    <rect class="fig-box-subtle" x="44" y="212" width="288" height="94" rx="4"/>
    <rect class="fig-box-subtle" x="376" y="80" width="182" height="28" rx="4"/>
    <rect class="fig-box-purple" x="376" y="126" width="182" height="34" rx="4"/>
    <rect class="fig-box-red" x="582" y="122" width="98" height="42" rx="4"/>
    <rect class="fig-box-blue" x="376" y="180" width="304" height="38" rx="4"/>
    <rect class="fig-box-green" x="376" y="240" width="174" height="66" rx="4"/>
    <rect class="fig-box-red" x="558" y="240" width="122" height="66" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">IP 数据报发送与转发控制流：源主机同网判据 vs 路由器查表与差错丢弃</text>
    <text class="fig-blue" x="188" y="68" text-anchor="middle" font-size="11.5">① 源主机：判断目的主机是否同网络</text>
    <text class="fig-blue" x="188" y="96" text-anchor="middle" font-size="10.5">目的 IP AND 本机子网掩码</text>
    <text class="fig-label" x="188" y="112" text-anchor="middle" font-size="10">与「本机 IP AND 本机子网掩码」比较</text>
    <text class="fig-green" x="114" y="162" text-anchor="middle" font-size="10.5">相同 → 直接交付</text>
    <text class="fig-muted" x="114" y="179" text-anchor="middle" font-size="9.5">不经路由器，ARP 查</text>
    <text class="fig-muted" x="114" y="193" text-anchor="middle" font-size="9.5">目的主机 MAC 封装</text>
    <text class="fig-orange" x="262" y="162" text-anchor="middle" font-size="10.5">不同 → 间接交付</text>
    <text class="fig-muted" x="262" y="179" text-anchor="middle" font-size="9.5">交给默认网关，ARP 查</text>
    <text class="fig-muted" x="262" y="193" text-anchor="middle" font-size="9.5">网关接口 MAC 封装</text>
    <text class="fig-label" x="54" y="231" font-size="10">• 默认网关必须是本网路由器接口 IP</text>
    <text class="fig-label" x="54" y="250" font-size="10">• 网关配错时：同网仍能通，跨网不通！</text>
    <text class="fig-muted" x="54" y="269" font-size="9.5">• 路由表四要素：目的网络｜掩码｜下一跳｜接口</text>
    <text class="fig-muted" x="54" y="288" font-size="9.5">• 直连网络表项无需填下一跳（直接交付）</text>
    <text class="fig-purple" x="526" y="68" text-anchor="middle" font-size="11.5">② 路由器：首部校验与逐条掩码相与查表</text>
    <text class="fig-label" x="467" y="98" text-anchor="middle" font-size="10">收到数据报，检查首部检验和</text>
    <text class="fig-purple" x="467" y="147" text-anchor="middle" font-size="10.5">首部无误且 TTL &gt; 1？</text>
    <text class="fig-muted" x="569" y="138" text-anchor="middle" font-size="9">否</text>
    <text class="fig-muted" x="480" y="172" font-size="9">是</text>
    <text class="fig-red" x="631" y="139" text-anchor="middle" font-size="9.5">丢弃分组</text>
    <text class="fig-muted" x="631" y="155" text-anchor="middle" font-size="8.5">回发 ICMP 差错</text>
    <text class="fig-blue" x="528" y="196" text-anchor="middle" font-size="10">目的 IP AND 各表项地址掩码</text>
    <text class="fig-label" x="528" y="211" text-anchor="middle" font-size="9.5">与表项「目的网络地址」比较（最长前缀匹配）</text>
    <text class="fig-green" x="463" y="258" text-anchor="middle" font-size="10.5">匹配成功 → 转发</text>
    <text class="fig-label" x="463" y="275" text-anchor="middle" font-size="9.5">TTL−1，重算首部检验和</text>
    <text class="fig-muted" x="463" y="292" text-anchor="middle" font-size="9">按下一跳/接口封装新帧发出</text>
    <text class="fig-red" x="619" y="258" text-anchor="middle" font-size="10.5">全部不匹配</text>
    <text class="fig-label" x="619" y="275" text-anchor="middle" font-size="9.5">丢弃该数据报</text>
    <text class="fig-muted" x="619" y="292" text-anchor="middle" font-size="9">发 ICMP 目的不可达</text>
  </svg>
  <figcaption>图 4-6：源主机基于本机掩码相与判断直接/间接交付，路由器校验首部后按掩码相与查表转发或回送 ICMP 差错</figcaption>
</figure>

路由器转发分组的完整顺序：

1. 收到 IP 数据报，检查首部是否出错；出错则<strong>丢弃</strong>并通告源主机。
2. 用目的地址与路由表中各条目的<strong>地址掩码</strong>逐条相与，得到目的网络地址；与条目中的目的网络地址相同即为匹配。
3. 命中则按该条目的下一跳（或直连接口）转发；全部不匹配则丢弃并通告源主机。
4. 转发时更新首部中部分字段（如 TTL 减 1），重新计算首部检验和，再交链路层封装发送。

以路由器两个接口为例：接口 0 直连 `192.168.0.0/25`（掩码 255.255.255.128），接口 1 直连 `192.168.0.128/25`，这两条是<strong>直连路由</strong>，无下一跳；其余条目可以是人工配置的<strong>静态路由</strong>，也可以是路由协议自动学到的<strong>动态路由</strong>。实际路由器要设计高效的数据结构与查找算法，以上只是最基本的工作原理。

<figure class="cpd-figure not-content" id="cn-w4-fig-7">
  <svg viewBox="0 0 720 344" role="img" aria-label="ARP地址解析广播请求与单播响应及DHCP动态主机配置四步广播时序对比图">
    <defs>
      <marker id="cn-w4-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="312" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="332" height="268" rx="6"/>
    <rect class="fig-box" x="372" y="48" width="320" height="268" rx="6"/>
    <line class="fig-line-dashed" x1="72" y1="114" x2="72" y2="216"/>
    <line class="fig-line-dashed" x1="306" y1="114" x2="306" y2="216"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="72" y1="146" x2="302" y2="146" marker-end="url(#cn-w4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="306" y1="196" x2="76" y2="196" marker-end="url(#cn-w4-arr-7)"/>
    <line class="fig-line-dashed" x1="410" y1="110" x2="410" y2="230"/>
    <line class="fig-line-dashed" x1="654" y1="110" x2="654" y2="230"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="410" y1="132" x2="650" y2="132" marker-end="url(#cn-w4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="654" y1="162" x2="414" y2="162" marker-end="url(#cn-w4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="410" y1="192" x2="650" y2="192" marker-end="url(#cn-w4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="654" y1="222" x2="414" y2="222" marker-end="url(#cn-w4-arr-7)"/>
    <rect class="fig-box-blue" x="38" y="76" width="96" height="38" rx="4"/>
    <rect class="fig-box-purple" x="234" y="76" width="114" height="38" rx="4"/>
    <rect class="fig-box-subtle" x="40" y="222" width="308" height="82" rx="4"/>
    <rect class="fig-box-blue" x="384" y="76" width="98" height="34" rx="4"/>
    <rect class="fig-box-green" x="582" y="76" width="98" height="34" rx="4"/>
    <rect class="fig-box-subtle" x="384" y="236" width="296" height="68" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">主机接入与下一跳解析配套机制：ARP 广播请求/单播响应与 DHCP 四步交互</text>
    <text class="fig-blue" x="194" y="67" text-anchor="middle" font-size="11.5">① ARP 地址解析（本局域网 IP → MAC）</text>
    <text class="fig-blue" x="86" y="92" text-anchor="middle" font-size="10">主机 H1 (.1)</text>
    <text class="fig-muted" x="86" y="106" text-anchor="middle" font-size="9">硬件地址 MAC1</text>
    <text class="fig-purple" x="291" y="92" text-anchor="middle" font-size="10">默认网关 R (.254)</text>
    <text class="fig-muted" x="291" y="106" text-anchor="middle" font-size="9">硬件地址 MAC_R</text>
    <text class="fig-orange" x="189" y="138" text-anchor="middle" font-size="9.5">① ARP 请求（广播帧 FF-FF-FF-FF-FF-FF）</text>
    <text class="fig-muted" x="189" y="160" text-anchor="middle" font-size="9">问：谁是 .254？我是 .1（顺带写入 .1→MAC1）</text>
    <text class="fig-green" x="189" y="188" text-anchor="middle" font-size="9.5">② ARP 响应（单播帧 → 目的 MAC1）</text>
    <text class="fig-muted" x="189" y="210" text-anchor="middle" font-size="9">答：我是 .254，我的 MAC 是 MAC_R</text>
    <text class="fig-label" x="48" y="240" font-size="9.5">• 高速缓存设有效期（防换网卡失效），类型字段 0x0806</text>
    <text class="fig-purple" x="48" y="258" font-size="9.5">• 跨网通信时 H1 只用 ARP 解析「本网默认网关 MAC_R」</text>
    <text class="fig-muted" x="48" y="276" font-size="9.5">• 绝不跨路由器广播 ARP！下一跳路由器再各自逐段 ARP</text>
    <text class="fig-muted" x="48" y="294" font-size="9.5">• ARP 请求是广播，ARP 响应是单播（高频真题辨析）</text>
    <text class="fig-green" x="532" y="67" text-anchor="middle" font-size="11.5">② DHCP 动态获取配置（UDP 68 ⇄ 67）</text>
    <text class="fig-blue" x="433" y="97" text-anchor="middle" font-size="9.5">客户机 (0.0.0.0:68)</text>
    <text class="fig-green" x="631" y="97" text-anchor="middle" font-size="9.5">DHCP服务器 (:67)</text>
    <text class="fig-orange" x="532" y="126" text-anchor="middle" font-size="9">① DISCOVER 发现（广播：0.0.0.0 → 255.255.255.255）</text>
    <text class="fig-blue" x="532" y="156" text-anchor="middle" font-size="9">② OFFER 提供（广播：服务器IP → 255.255.255.255）</text>
    <text class="fig-orange" x="532" y="186" text-anchor="middle" font-size="9">③ REQUEST 请求（广播：选定某台并通知其余收回）</text>
    <text class="fig-green" x="532" y="216" text-anchor="middle" font-size="9">④ ACK 确认（广播：正式下发 IP/掩码/默认网关/DNS）</text>
    <text class="fig-label" x="392" y="253" font-size="9.5">• 应用层协议，封装在 UDP（客户 68、服务器 67）中</text>
    <text class="fig-label" x="392" y="271" font-size="9.5">• 四步均可用广播（客户未正式启用 IP 且可能多服务器）</text>
    <text class="fig-muted" x="392" y="289" font-size="9.5">• 租用期过半（50%）单播续租；跨网需配 DHCP 中继代理</text>
  </svg>
  <figcaption>图 4-7：局域网内 ARP“广播请求、单播响应”硬件地址解析与 DHCP 四步 UDP 广播自动获取 IP、子网掩码、默认网关流程</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w4-fig-8">
  <svg viewBox="0 0 720 304" role="img" aria-label="NAT与NAPT网络地址端口转换路由器双向重写源目的IP与端口号流程">
    <defs>
      <marker id="cn-w4-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="272" rx="8"/>
    <line class="fig-line-accent" x1="162" y1="80" x2="238" y2="96" marker-end="url(#cn-w4-arr-8)"/>
    <line class="fig-line-accent" x1="162" y1="138" x2="238" y2="122" marker-end="url(#cn-w4-arr-8)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="472" y1="94" x2="552" y2="94" marker-end="url(#cn-w4-arr-8)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="554" y1="126" x2="474" y2="126" marker-end="url(#cn-w4-arr-8)"/>
    <rect class="fig-box-blue" x="28" y="58" width="134" height="44" rx="5"/>
    <rect class="fig-box-blue" x="28" y="116" width="134" height="44" rx="5"/>
    <rect class="fig-box-purple" x="240" y="54" width="232" height="110" rx="6"/>
    <rect class="fig-box" x="252" y="78" width="208" height="76" rx="4"/>
    <line class="fig-divider" x1="252" y1="100" x2="460" y2="100"/>
    <rect class="fig-box-green" x="556" y="76" width="136" height="66" rx="5"/>
    <rect class="fig-box" x="28" y="176" width="324" height="98" rx="5"/>
    <rect class="fig-box" x="364" y="176" width="328" height="98" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">NAPT 网络地址与端口转换：多台专用网主机复用单一全球 IP 访问公网</text>
    <text class="fig-blue" x="95" y="77" text-anchor="middle" font-size="10.5">内网主机 A</text>
    <text class="fig-muted" x="95" y="94" text-anchor="middle" font-size="9.5">192.168.0.2 : 3000</text>
    <text class="fig-blue" x="95" y="135" text-anchor="middle" font-size="10.5">内网主机 B</text>
    <text class="fig-muted" x="95" y="152" text-anchor="middle" font-size="9.5">192.168.0.3 : 3000</text>
    <text class="fig-purple" x="356" y="71" text-anchor="middle" font-size="10.5">NAT 路由器（NAPT 转换表）</text>
    <text class="fig-muted" x="356" y="94" text-anchor="middle" font-size="9">专用网 IP:Port  ⇄  全球公网 IP:Port</text>
    <text class="fig-orange" x="356" y="118" text-anchor="middle" font-size="9.5">192.168.0.2:3000 ⇄ 172.38.1.5:40001</text>
    <text class="fig-green" x="356" y="140" text-anchor="middle" font-size="9.5">192.168.0.3:3000 ⇄ 172.38.1.5:40002</text>
    <text class="fig-orange" x="513" y="86" text-anchor="middle" font-size="9">出站请求</text>
    <text class="fig-green" x="513" y="142" text-anchor="middle" font-size="9">入站应答</text>
    <text class="fig-green" x="624" y="104" text-anchor="middle" font-size="10.5">公网服务器 S</text>
    <text class="fig-muted" x="624" y="123" text-anchor="middle" font-size="9.5">213.18.2.4 : 80</text>
    <text class="fig-orange" x="40" y="195" font-size="10.5">① 出站转换（内网 → 因特网）：改写源 IP 与源端口</text>
    <text class="fig-label" x="40" y="215" font-size="9.5">• 转换前：源 192.168.0.2:3000 → 目的 213.18.2.4:80</text>
    <text class="fig-label" x="40" y="234" font-size="9.5">• 转换后：源 172.38.1.5:40001 → 目的 213.18.2.4:80</text>
    <text class="fig-muted" x="40" y="254" font-size="9.5">• 须同时重算 IP 首部检验和及运输层 TCP/UDP 检验和</text>
    <text class="fig-green" x="376" y="195" font-size="10.5">② 入站还原与必考特性：改写目的 IP 与目的端口</text>
    <text class="fig-label" x="376" y="215" font-size="9.5">• 应答分组目的 172.38.1.5:40001 → 查表还原给主机 A</text>
    <text class="fig-label" x="376" y="234" font-size="9.5">• NAPT 结合运输层端口号区分不同主机（跨层操作）</text>
    <text class="fig-muted" x="376" y="254" font-size="9.5">• 默认仅支持内网主动发起；外网主动访问需配端口映射</text>
  </svg>
  <figcaption>图 4-8：NAPT 路由器利用“IP 地址 + 运输层端口号”双向映射实现多台私有 IP 主机共享单公网 IP 通信</figcaption>
</figure>

> <strong>★考点</strong>：判断“某主机能否访问某服务器”看两点——目的地址与主机是否同前缀（同网则直接交付，与网关无关），以及默认网关是否配置成了<strong>路由器接口</strong>地址。把默认网关配成 DHCP 服务器地址时，同网服务器能访问，但无法访问 Internet。

### 4.5 广播 IP 数据报与广播域隔离

主机向本网络发送广播 IP 数据报时，目的地址可填本网络的<strong>广播地址</strong>（如 `192.168.0.127`，即 `192.168.0.0/25` 的主机号全 1），也可填<strong>受限广播地址</strong> `255.255.255.255`；本网络内各设备都会收到。若主机给另一个网络发广播（如填 `192.168.0.255`，`192.168.0.128/25` 的广播地址）并希望路由器转发，路由器判断出是广播数据报后<strong>不会转发</strong>——路由器隔离广播域。

这是必要的：若全网大量路由器都转发广播，会造成巨大的<strong>广播风暴</strong>，严重浪费资源。

| 设备 | 工作层次 | 隔离冲突域 | 隔离广播域 |
|---|---|---|---|
| 中继器、集线器 | 物理层 | 否 | 否 |
| 网桥、交换机 | 数据链路层 | <strong>是</strong> | 否 |
| 路由器 | 网络层 | <strong>是</strong> | <strong>是</strong> |

<figure class="cpd-figure not-content" id="cn-w4-fig-9">
  <svg viewBox="0 0 720 348" role="img" aria-label="路由器隔离广播域、ICMP差错报告与询问报文分类及traceroute逐跳TTL超时原理图">
    <defs>
      <marker id="cn-w4-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="316" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="330" height="272" rx="6"/>
    <rect class="fig-box" x="370" y="48" width="322" height="272" rx="6"/>
    <line class="fig-line-dashed" x1="410" y1="112" x2="410" y2="246"/>
    <line class="fig-line-dashed" x1="488" y1="112" x2="488" y2="246"/>
    <line class="fig-line-dashed" x1="566" y1="112" x2="566" y2="246"/>
    <line class="fig-line-dashed" x1="649" y1="112" x2="649" y2="246"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="410" y1="134" x2="484" y2="134" marker-end="url(#cn-w4-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="488" y1="152" x2="414" y2="152" marker-end="url(#cn-w4-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="410" y1="176" x2="562" y2="176" marker-end="url(#cn-w4-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="566" y1="194" x2="414" y2="194" marker-end="url(#cn-w4-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="410" y1="218" x2="645" y2="218" marker-end="url(#cn-w4-arr-9)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="649" y1="236" x2="414" y2="236" marker-end="url(#cn-w4-arr-9)"/>
    <rect class="fig-box-subtle" x="40" y="76" width="306" height="54" rx="4"/>
    <rect class="fig-box-orange" x="40" y="138" width="194" height="76" rx="4"/>
    <rect class="fig-box-blue" x="242" y="138" width="104" height="76" rx="4"/>
    <rect class="fig-box-red" x="40" y="222" width="306" height="86" rx="4"/>
    <rect class="fig-box-blue" x="382" y="78" width="56" height="34" rx="4"/>
    <rect class="fig-box-purple" x="462" y="78" width="52" height="34" rx="4"/>
    <rect class="fig-box-purple" x="540" y="78" width="52" height="34" rx="4"/>
    <rect class="fig-box-green" x="618" y="78" width="62" height="34" rx="4"/>
    <rect class="fig-box-subtle" x="382" y="252" width="298" height="56" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">广播数据报隔离、ICMP 网际控制报文分类与 traceroute 路径追踪</text>
    <text class="fig-orange" x="193" y="67" text-anchor="middle" font-size="11.5">① 广播隔离与 ICMP 报文（装入 IP 数据报）</text>
    <text class="fig-label" x="50" y="95" font-size="9.5">• 受限广播 255.255.255.255 与跨网直接广播均被路由器拦截</text>
    <text class="fig-muted" x="50" y="114" font-size="9.5">• 路由器隔离广播域防广播风暴；交换机/集线器不隔离广播域</text>
    <text class="fig-orange" x="137" y="155" text-anchor="middle" font-size="10">ICMP 差错报告（5种）</text>
    <text class="fig-label" x="48" y="173" font-size="9">①终点不可达 ②时间超过</text>
    <text class="fig-label" x="48" y="189" font-size="9">③参数问题   ④改变路由</text>
    <text class="fig-muted" x="48" y="205" font-size="9">⑤源点抑制（标准已弃用）</text>
    <text class="fig-blue" x="294" y="155" text-anchor="middle" font-size="10">询问报文(2种)</text>
    <text class="fig-label" x="250" y="175" font-size="9">①回送请求/答</text>
    <text class="fig-muted" x="250" y="191" font-size="8.5">（ping 测连通）</text>
    <text class="fig-label" x="250" y="206" font-size="9">②时间戳请求/答</text>
    <text class="fig-red" x="50" y="239" font-size="9.5">★ 不发送 ICMP 差错报告的 4 种禁忌（高频必考）：</text>
    <text class="fig-label" x="50" y="257" font-size="9.5">1. 对 ICMP 差错报告报文自身，不再发 ICMP 差错报告</text>
    <text class="fig-label" x="50" y="275" font-size="9.5">2. 分片数据报中，除第 1 个分片外的所有后续分片</text>
    <text class="fig-label" x="50" y="293" font-size="9.5">3. 多播地址数据报 ｜ 4. 特殊地址（127.0.0.0 或 0.0.0.0）</text>
    <text class="fig-purple" x="531" y="67" text-anchor="middle" font-size="11.5">② traceroute 逐跳递增 TTL 探测原理</text>
    <text class="fig-blue" x="410" y="99" text-anchor="middle" font-size="10">源 H1</text>
    <text class="fig-purple" x="488" y="99" text-anchor="middle" font-size="10">R1</text>
    <text class="fig-purple" x="566" y="99" text-anchor="middle" font-size="10">R2</text>
    <text class="fig-green" x="649" y="99" text-anchor="middle" font-size="10">目的 H2</text>
    <text class="fig-blue" x="449" y="129" text-anchor="middle" font-size="8.5">TTL=1</text>
    <text class="fig-orange" x="449" y="148" text-anchor="middle" font-size="8.5">时间超过</text>
    <text class="fig-blue" x="514" y="171" text-anchor="middle" font-size="8.5">TTL=2（经R1减为1）</text>
    <text class="fig-orange" x="495" y="190" text-anchor="middle" font-size="8.5">R2置0：ICMP时间超过</text>
    <text class="fig-blue" x="530" y="213" text-anchor="middle" font-size="8.5">TTL=3 + 偏僻UDP端口</text>
    <text class="fig-green" x="530" y="232" text-anchor="middle" font-size="8.5">H2回：ICMP终点(端口)不可达</text>
    <text class="fig-label" x="392" y="271" font-size="9.5">• ping 直接在网络层发 ICMP 回送请求（不经 TCP/UDP！）</text>
    <text class="fig-muted" x="392" y="291" font-size="9.5">• 数据报重组超时亦由目的主机发 ICMP 时间超过报文</text>
  </svg>
  <figcaption>图 4-9：路由器广播域隔离、ICMP 差错报告（含四不发禁忌）与询问报文分类及 `traceroute` 递增 `TTL` 路径探测时序</figcaption>
</figure>

> <strong>★考点</strong>：IP 路由器工作在网际层，不负责可靠传输，不能确保 IP 分组不丢失；它对首部做校验，发现错误即丢弃该分组，并向源主机发送 <strong>ICMP 差错报告报文</strong>。所以“路由器能保证分组不丢失”之类的描述是错的。

### 4.6 路由选择协议概述

路由选择分两类：<strong>静态路由选择</strong>（人工配置）与<strong>动态路由选择</strong>（路由器通过路由选择协议自动获取）。

| 对比项 | 静态路由选择 | 动态路由选择 |
|---|---|---|
| 配置方式 | 人工配置网络路由、默认路由、特定主机路由、黑洞路由等 | 路由协议自动获取并更新 |
| 开销 | 简单、开销小 | 比较复杂、开销较大 |
| 适应网络变化 | 不能及时适应 | 能较好适应 |
| 适用规模 | 小规模网络 | 大规模网络（因特网） |

因特网的路由选择协议有三个特点：<strong>自适应（动态，适应网络状态变化）、分布式（各路由器相互交换信息，共同完成路由信息的获取与更新）、分层次</strong>（把因特网划分为许多较小的<strong>自治系统 AS</strong>，域内与域间使用不同类别的协议）。

<figure class="cpd-figure not-content" id="cn-w4-fig-10">
  <svg viewBox="0 0 720 330" role="img" aria-label="因特网分层次路由选择：自治系统内部运行IGP协议与自治系统之间运行EGP协议BGP-4拓扑图">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="298" rx="8"/>
    <rect class="fig-box-dashed" x="32" y="50" width="268" height="172" rx="10"/>
    <rect class="fig-box-dashed" x="420" y="50" width="268" height="172" rx="10"/>
    <line class="fig-line" x1="88" y1="126" x2="168" y2="126"/>
    <line class="fig-line" x1="88" y1="126" x2="128" y2="188"/>
    <line class="fig-line" x1="168" y1="126" x2="128" y2="188"/>
    <line class="fig-line" x1="168" y1="126" x2="264" y2="148"/>
    <line class="fig-line" x1="128" y1="188" x2="264" y2="148"/>
    <line class="fig-line" x1="456" y1="148" x2="552" y2="126"/>
    <line class="fig-line" x1="456" y1="148" x2="592" y2="188"/>
    <line class="fig-line" x1="552" y1="126" x2="632" y2="126"/>
    <line class="fig-line" x1="552" y1="126" x2="592" y2="188"/>
    <line class="fig-line" x1="632" y1="126" x2="592" y2="188"/>
    <line class="fig-line-accent" x1="264" y1="148" x2="456" y2="148"/>
    <rect class="fig-box-blue" x="308" y="114" width="104" height="26" rx="4"/>
    <rect class="fig-box-purple" x="308" y="156" width="104" height="24" rx="4"/>
    <circle class="fig-box" cx="88" cy="126" r="15"/>
    <circle class="fig-box" cx="168" cy="126" r="15"/>
    <circle class="fig-box" cx="128" cy="188" r="15"/>
    <circle class="fig-box-blue" cx="264" cy="148" r="17"/>
    <circle class="fig-box-blue" cx="456" cy="148" r="17"/>
    <circle class="fig-box" cx="552" cy="126" r="15"/>
    <circle class="fig-box" cx="632" cy="126" r="15"/>
    <circle class="fig-box" cx="592" cy="188" r="15"/>
    <rect class="fig-box" x="32" y="232" width="656" height="68" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">分层次路由选择体系：自治系统（AS）内部用 IGP，自治系统之间用 EGP</text>
    <text class="fig-green" x="48" y="72" font-size="11">自治系统 AS1（例：运行 OSPF）</text>
    <text class="fig-orange" x="436" y="72" font-size="11">自治系统 AS2（例：运行 RIP）</text>
    <text class="fig-label" x="88" y="130" text-anchor="middle" font-size="10">R1</text>
    <text class="fig-label" x="168" y="130" text-anchor="middle" font-size="10">R2</text>
    <text class="fig-label" x="128" y="192" text-anchor="middle" font-size="10">R3</text>
    <text class="fig-blue" x="264" y="152" text-anchor="middle" font-size="10">R4</text>
    <text class="fig-blue" x="456" y="152" text-anchor="middle" font-size="10">R5</text>
    <text class="fig-label" x="552" y="130" text-anchor="middle" font-size="10">R6</text>
    <text class="fig-label" x="632" y="130" text-anchor="middle" font-size="10">R7</text>
    <text class="fig-label" x="592" y="192" text-anchor="middle" font-size="10">R8</text>
    <text class="fig-blue" x="360" y="131" text-anchor="middle" font-size="10">eBGP 会话 (EGP)</text>
    <text class="fig-purple" x="360" y="172" text-anchor="middle" font-size="9.5">封装于 TCP 179</text>
    <text class="fig-muted" x="360" y="202" text-anchor="middle" font-size="9">R4/R5：BGP发言人</text>
    <text class="fig-label" x="44" y="252" font-size="10.5">• 内部网关协议 IGP（域内）：RIP（距离向量，UDP 520）· OSPF（链路状态，IP 协议号 89）· IS-IS · EIGRP</text>
    <text class="fig-label" x="44" y="271" font-size="10.5">• 外部网关协议 EGP（域间）：BGP-4（路径向量，TCP 179）；不同 AS 内部可各自独立选用不同 IGP</text>
    <text class="fig-muted" x="44" y="290" font-size="10">• 易错辨析：IGP 与 EGP 仅是协议「类别名称」而非具体协议名</text>
  </svg>
  <figcaption>图 4-10：因特网两级分层次路由架构——自治系统（AS）内各自运行 IGP（RIP / OSPF），边界发言人间通过 TCP 179 运行 BGP-4</figcaption>
</figure>

- <strong>域间路由选择用外部网关协议 EGP 这一类，域内路由选择</strong>用内部网关协议 IGP 这一类；IGP、EGP 只是<strong>类别名称</strong>，不是具体协议。
- 名称中的“网关”来自早期 RFC，新 RFC 改称“路由器”，因此 EGP/IGP 也可称 ERP/IRP，本课程沿用原名词。
- 具体协议：RIP、IGRP 基于<strong>距离向量</strong>；EIGRP 是取代 IGRP 的<strong>混合型</strong>协议（结合距离向量与链路状态）；OSPF、IS-IS 基于<strong>链路状态</strong>（集成化 IS-IS 常用于骨干网）；外部网关协议最常见的是 <strong>BGP</strong>。RIP 是最早应用在因特网上的路由选择协议。

<figure class="cpd-figure not-content" id="cn-w4-fig-11">
  <svg viewBox="0 0 720 346" role="img" aria-label="内部网关协议对比：RIP距离向量路由更新规则与16跳无穷计数及OSPF主干区域洪泛LSDB机制">
    <defs>
      <marker id="cn-w4-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="314" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="332" height="266" rx="6"/>
    <rect class="fig-box" x="372" y="48" width="320" height="266" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="98" y1="93" x2="128" y2="93"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="180" y1="89" x2="210" y2="89" marker-end="url(#cn-w4-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="212" y1="98" x2="182" y2="98" marker-end="url(#cn-w4-arr-11)"/>
    <line class="fig-line" x1="464" y1="120" x2="464" y2="152"/>
    <line class="fig-line" x1="600" y1="120" x2="600" y2="152"/>
    <line class="fig-line" x1="486" y1="104" x2="578" y2="104"/>
    <rect class="fig-box-red" x="40" y="78" width="58" height="30" rx="4"/>
    <rect class="fig-box-orange" x="128" y="78" width="52" height="30" rx="4"/>
    <rect class="fig-box-orange" x="212" y="78" width="52" height="30" rx="4"/>
    <rect class="fig-box-red" x="272" y="78" width="76" height="30" rx="4"/>
    <rect class="fig-box-blue" x="40" y="116" width="308" height="108" rx="4"/>
    <rect class="fig-box-subtle" x="40" y="232" width="308" height="70" rx="4"/>
    <rect class="fig-box-purple" x="404" y="76" width="256" height="54" rx="8"/>
    <rect class="fig-box-green" x="388" y="150" width="138" height="44" rx="6"/>
    <rect class="fig-box-green" x="538" y="150" width="138" height="44" rx="6"/>
    <rect class="fig-box" x="440" y="92" width="46" height="24" rx="4"/>
    <rect class="fig-box" x="578" y="92" width="46" height="24" rx="4"/>
    <rect class="fig-box-accent" x="434" y="124" width="60" height="22" rx="4"/>
    <rect class="fig-box-accent" x="570" y="124" width="60" height="22" rx="4"/>
    <rect class="fig-box-subtle" x="384" y="202" width="296" height="100" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">内部网关协议（IGP）双核心对比：RIP 距离向量更新 vs OSPF 链路状态分区域</text>
    <text class="fig-orange" x="194" y="67" text-anchor="middle" font-size="11.5">① RIP（UDP 520，跳数度量，16 = 不可达）</text>
    <text class="fig-red" x="69" y="97" text-anchor="middle" font-size="9.5">网N1(断)</text>
    <text class="fig-red" x="113" y="97" text-anchor="middle" font-size="11">×</text>
    <text class="fig-orange" x="154" y="97" text-anchor="middle" font-size="10">R1</text>
    <text class="fig-orange" x="238" y="97" text-anchor="middle" font-size="10">R2</text>
    <text class="fig-red" x="310" y="91" text-anchor="middle" font-size="8.5">慢收敛兜圈</text>
    <text class="fig-muted" x="310" y="103" text-anchor="middle" font-size="8">递增到 16</text>
    <text class="fig-blue" x="50" y="134" font-size="10">★ 收到邻居 X 的距离向量报文更新口诀（必考计算）：</text>
    <text class="fig-label" x="50" y="152" font-size="9.5">1. 预处理：将 X 报文中所有距离 +1，下一跳全部改为 X</text>
    <text class="fig-label" x="50" y="170" font-size="9.5">2. 原表无目的网 N → 直接添加该项</text>
    <text class="fig-label" x="50" y="188" font-size="9.5">3. 原表有 N 且「下一跳 == X」→ 无条件替换为新值！</text>
    <text class="fig-label" x="50" y="206" font-size="9.5">4. 原表有 N 且「下一跳 != X」→ 仅当新距离更小时才更新</text>
    <text class="fig-label" x="48" y="249" font-size="9">• 仅与「相邻路由器」每「30s」交换「整张路由表」（180s 超时）</text>
    <text class="fig-muted" x="48" y="268" font-size="9.5">• 特点：好消息传得快，坏消息传得慢（水平分割/毒性逆转改善）</text>
    <text class="fig-muted" x="48" y="287" font-size="9.5">• 报文封装于应用层 UDP 520 端口，但属于网络层路由协议</text>
    <text class="fig-purple" x="532" y="67" text-anchor="middle" font-size="11.5">② OSPF（直接 IP 封装：协议号 89）</text>
    <text class="fig-purple" x="532" y="89" text-anchor="middle" font-size="9.5">主干区域 Area 0（0.0.0.0）</text>
    <text class="fig-label" x="463" y="108" text-anchor="middle" font-size="9.5">R_A</text>
    <text class="fig-label" x="601" y="108" text-anchor="middle" font-size="9.5">R_B</text>
    <text class="fig-accent" x="464" y="139" text-anchor="middle" font-size="9">ABR1</text>
    <text class="fig-accent" x="600" y="139" text-anchor="middle" font-size="9">ABR2</text>
    <text class="fig-green" x="457" y="170" text-anchor="middle" font-size="10">区域 Area 1</text>
    <text class="fig-muted" x="457" y="185" text-anchor="middle" font-size="8.5">域内洪泛 LSA</text>
    <text class="fig-green" x="607" y="170" text-anchor="middle" font-size="10">区域 Area 2</text>
    <text class="fig-muted" x="607" y="185" text-anchor="middle" font-size="8.5">域内洪泛 LSA</text>
    <text class="fig-label" x="392" y="220" font-size="9">• 向本区域「所有路由器」「洪泛」发送「相邻链路状态 LSA」</text>
    <text class="fig-label" x="392" y="238" font-size="9">• 拓扑变化触发更新（+30min 刷新），每 10s 发 Hello 保活</text>
    <text class="fig-purple" x="392" y="256" font-size="9">• 全区域同步一致「链路状态数据库 LSDB」，各自用 Dijkstra</text>
    <text class="fig-purple" x="392" y="274" font-size="9">  最短路径算法算路由表：收敛极快、无环路、无坏消息慢！</text>
    <text class="fig-muted" x="392" y="292" font-size="9">• 分两级区域缩小洪泛范围；广播网选指定路由器 DR 降复杂度</text>
  </svg>
  <figcaption>图 4-11：RIP 距离向量更新规则及“坏消息传得慢”16 跳不可达与 OSPF 分区域洪泛同步 LSDB、Dijkstra 算路机制对比</figcaption>
</figure>

> <strong>★考点</strong>：RIP、OSPF 是<strong>内部</strong>网关协议；BGP 是<strong>外部</strong>网关协议，且 BGP-4 报文封装在 <strong>TCP</strong> 报文段中传输（不用 UDP）。

### 4.7 路由器的结构与分组转发

路由器是具有多个输入端口和输出端口的专用计算机，任务就是转发分组。结构上分为两大部分：<strong>路由选择部分</strong>与<strong>分组转发部分</strong>。

<figure class="cpd-figure not-content" id="cn-w4-fig-12">
  <svg viewBox="0 0 720 326" role="img" aria-label="路由器体系结构：控制平面路由选择处理机与数据平面输入端口、交换结构、输出端口详解">
    <defs>
      <marker id="cn-w4-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="294" rx="8"/>
    <rect class="fig-box-dashed" x="196" y="48" width="328" height="86" rx="6"/>
    <path class="fig-line" stroke="var(--sl-color-purple)" d="M 116 148 L 116 71 L 208 71" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="360" y1="88" x2="360" y2="96"/>
    <line class="fig-line-accent" x1="360" y1="126" x2="360" y2="146" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="204" y1="162" x2="246" y2="162" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="204" y1="196" x2="246" y2="196" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="204" y1="230" x2="246" y2="230" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="472" y1="162" x2="514" y2="162" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="472" y1="196" x2="514" y2="196" marker-end="url(#cn-w4-arr-12)"/>
    <line class="fig-line" x1="472" y1="230" x2="514" y2="230" marker-end="url(#cn-w4-arr-12)"/>
    <rect class="fig-box-purple" x="210" y="54" width="300" height="34" rx="4"/>
    <rect class="fig-box-blue" x="210" y="96" width="300" height="30" rx="4"/>
    <rect class="fig-box" x="32" y="148" width="172" height="28" rx="4"/>
    <rect class="fig-box" x="32" y="182" width="172" height="28" rx="4"/>
    <rect class="fig-box" x="32" y="216" width="172" height="28" rx="4"/>
    <rect class="fig-box-accent" x="248" y="148" width="224" height="96" rx="6"/>
    <rect class="fig-box-green" x="516" y="148" width="172" height="28" rx="4"/>
    <rect class="fig-box-green" x="516" y="182" width="172" height="28" rx="4"/>
    <rect class="fig-box-green" x="516" y="216" width="172" height="28" rx="4"/>
    <rect class="fig-box" x="32" y="254" width="656" height="44" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">传统路由器结构：路由选择部分（控制平面）＋ 分组转发部分（数据平面）</text>
    <text class="fig-purple" x="116" y="64" text-anchor="middle" font-size="9">路由报文上交处理机</text>
    <text class="fig-purple" x="360" y="75" text-anchor="middle" font-size="10.5">路由选择处理机（软件运行协议，毫秒级交互）</text>
    <text class="fig-blue" x="360" y="115" text-anchor="middle" font-size="10">路由表（计算优化） ──导出──▶ 转发表（查找优化）</text>
    <text class="fig-label" x="118" y="166" text-anchor="middle" font-size="9.5">输入端口1：物理→链路→网络队列</text>
    <text class="fig-label" x="118" y="200" text-anchor="middle" font-size="9.5">输入端口2：物理→链路→网络队列</text>
    <text class="fig-label" x="118" y="234" text-anchor="middle" font-size="9.5">输入端口3：物理→链路→网络队列</text>
    <text class="fig-accent" x="360" y="174" text-anchor="middle" font-size="11.5">交换结构（硬件纳秒级转发）</text>
    <text class="fig-label" x="360" y="196" text-anchor="middle" font-size="9.5">三种实现方式（性能递增）：</text>
    <text class="fig-muted" x="360" y="214" text-anchor="middle" font-size="9.5">① 通过存储器 → ② 通过总线</text>
    <text class="fig-muted" x="360" y="231" text-anchor="middle" font-size="9.5">→ ③ 通过纵横交换网（并行无阻塞）</text>
    <text class="fig-green" x="602" y="166" text-anchor="middle" font-size="9.5">输出端口1：网络队列→链路→物理</text>
    <text class="fig-green" x="602" y="200" text-anchor="middle" font-size="9.5">输出端口2：网络队列→链路→物理</text>
    <text class="fig-green" x="602" y="234" text-anchor="middle" font-size="9.5">输出端口3：网络队列→链路→物理</text>
    <text class="fig-label" x="44" y="272" font-size="10">• 分流规则：普通待转发数据分组查转发表经交换结构高速转发；路由协议报文不转发，送路由选择处理机更新路由表</text>
    <text class="fig-muted" x="44" y="290" font-size="9.5">• 丢包根源：输入/输出端口均设排队缓冲区，突发流量超出缓存容量即发生溢出丢弃（对头阻塞 HOL 发生在输入队列）</text>
  </svg>
  <figcaption>图 4-12：路由器控制平面（路由选择处理机生成路由表/转发表）与数据平面（三层端口队列及三种硬件交换结构）协同架构</figcaption>
</figure>

- <strong>路由选择处理机</strong>：按所用路由协议周期性与其他路由器交互路由信息，计算并更新路由表；路由表面向“拓扑变化的计算最优”。
- <strong>转发部分</strong>：交换结构 + 一组输入端口 + 一组输出端口。信号进入输入端口后，物理层转成比特流，链路层识别出帧、去掉帧头帧尾后交网络层。
- 送交网络层的若是<strong>普通待转发分组</strong>：按首部目的地址查表，命中则送交换结构、由指定输出端口转发，未命中则丢弃；转发前更新首部（如 TTL 减 1），交链路层封装成帧再发送。
- 送交网络层的若是<strong>路由报文</strong>：交路由选择处理机，据其内容更新路由表。
- <strong>转发表</strong>由路由表得出，结构上为“查找过程最优”而设计；本课程后续不严格区分两者。

<figure class="cpd-figure not-content" id="cn-w4-fig-13">
  <svg viewBox="0 0 720 318" role="img" aria-label="SDN软件定义网络控制与数据平面分离架构及OpenFlow匹配加动作通用流表图">
    <defs>
      <marker id="cn-w4-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="286" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="286" height="240" rx="6"/>
    <rect class="fig-box" x="326" y="48" width="366" height="240" rx="6"/>
    <line class="fig-line" x1="171" y1="92" x2="171" y2="112" marker-end="url(#cn-w4-arr-13)"/>
    <line class="fig-line-accent" x1="171" y1="160" x2="171" y2="188" marker-end="url(#cn-w4-arr-13)"/>
    <line class="fig-line-accent" x1="145" y1="160" x2="81" y2="188" marker-end="url(#cn-w4-arr-13)"/>
    <line class="fig-line-accent" x1="197" y1="160" x2="261" y2="188" marker-end="url(#cn-w4-arr-13)"/>
    <line class="fig-line" x1="509" y1="126" x2="509" y2="142" marker-end="url(#cn-w4-arr-13)"/>
    <rect class="fig-box-green" x="44" y="60" width="254" height="32" rx="4"/>
    <rect class="fig-box-purple" x="44" y="114" width="254" height="46" rx="5"/>
    <rect class="fig-box-blue" x="44" y="190" width="74" height="38" rx="4"/>
    <rect class="fig-box-blue" x="134" y="190" width="74" height="38" rx="4"/>
    <rect class="fig-box-blue" x="224" y="190" width="74" height="38" rx="4"/>
    <rect class="fig-box-subtle" x="44" y="236" width="254" height="42" rx="4"/>
    <rect class="fig-box-accent" x="340" y="76" width="338" height="50" rx="4"/>
    <rect class="fig-box-blue" x="340" y="144" width="338" height="30" rx="4"/>
    <rect class="fig-box-subtle" x="340" y="182" width="338" height="96" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">SDN 软件定义网络：控制平面集中上移（远程控制器）＋ 数据平面通用流表转发</text>
    <text class="fig-green" x="171" y="80" text-anchor="middle" font-size="10">网络控制应用（路由 / 负载均衡 / 防火墙）</text>
    <text class="fig-muted" x="218" y="106" font-size="9">北向接口 API</text>
    <text class="fig-purple" x="171" y="133" text-anchor="middle" font-size="10.5">SDN 远程控制器（逻辑集中控制面）</text>
    <text class="fig-muted" x="171" y="150" text-anchor="middle" font-size="9">掌握全网拓扑，全局 Dijkstra 算路并下发流表</text>
    <text class="fig-accent" x="234" y="168" font-size="8.5">南向 OpenFlow</text>
    <text class="fig-blue" x="81" y="207" text-anchor="middle" font-size="9.5">交换机 S1</text>
    <text class="fig-muted" x="81" y="221" text-anchor="middle" font-size="8.5">仅查流表</text>
    <text class="fig-blue" x="171" y="207" text-anchor="middle" font-size="9.5">交换机 S2</text>
    <text class="fig-muted" x="171" y="221" text-anchor="middle" font-size="8.5">仅查流表</text>
    <text class="fig-blue" x="261" y="207" text-anchor="middle" font-size="9.5">交换机 S3</text>
    <text class="fig-muted" x="261" y="221" text-anchor="middle" font-size="8.5">仅查流表</text>
    <text class="fig-label" x="52" y="253" font-size="9.5">• 交换机不再运行 OSPF/RIP，仅保留硬件数据平面</text>
    <text class="fig-muted" x="52" y="270" font-size="9.5">• 实现控制面与数据面彻底解耦、集中可编程控制</text>
    <text class="fig-purple" x="509" y="67" text-anchor="middle" font-size="11.5">OpenFlow「匹配 ＋ 动作」通用流表结构</text>
    <text class="fig-accent" x="509" y="95" text-anchor="middle" font-size="10">跨层次首部匹配域（同时支持 L1/L2/L3/L4 字段）</text>
    <text class="fig-muted" x="509" y="114" text-anchor="middle" font-size="9.5">入端口 ｜ 源/目的MAC ｜ VLAN ｜ 源/目的IP ｜ TCP/UDP端口</text>
    <text class="fig-blue" x="509" y="163" text-anchor="middle" font-size="10">流表三元组：【首部匹配规则】 ｜ 【统计计数器】 ｜ 【动作集】</text>
    <text class="fig-label" x="350" y="200" font-size="9.5">① 充当路由器：匹配目的 IP 前缀 → 动作 Forward(Port k)</text>
    <text class="fig-label" x="350" y="219" font-size="9.5">② 充当二层交换机：匹配目的 MAC → 动作 Forward / 泛洪</text>
    <text class="fig-label" x="350" y="238" font-size="9.5">③ 充当防火墙：匹配源 IP 或目的端口=22 → 动作 Drop 丢弃</text>
    <text class="fig-label" x="350" y="257" font-size="9.5">④ 充当 NAT/负载均衡：动作 Modify-Field 重写 IP/端口后转发</text>
  </svg>
  <figcaption>图 4-13：SDN 控制平面与数据平面分离架构及 OpenFlow“跨层首部匹配＋动作”统一实现路由、交换、防火墙与 NAT</figcaption>
</figure>

> <strong>注意</strong>：路由表面向“拓扑计算的优化”，转发表面向“查找的优化”，这是两者设计目标的差别。

### 4.8 边界网关协议 BGP 的基本工作原理

AS 之间的路由选择不能用统一代价度量：各 AS 的度量不同（如 AS1 用时延、AS2 用跳数、AS3 用带宽），因此“找最佳路由”没有意义；而且 AS 间路由还必须考虑政治、经济、安全等<strong>策略</strong>（例如国内站点互访不应绕经国外）。所以 BGP 只求找一条能到达目的网络且比较好（不兜圈子）的路由，而非最佳路由。

<figure class="cpd-figure not-content" id="cn-w4-fig-14">
  <svg viewBox="0 0 720 338" role="img" aria-label="BGP-4边界网关协议异构AS策略选路、eBGP与iBGP会话、AS-PATH路径向量防环及四大报文机制">
    <defs>
      <marker id="cn-w4-arr-14" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="306" rx="8"/>
    <line class="fig-line-accent" x1="128" y1="137" x2="164" y2="137"/>
    <line class="fig-line-accent" x1="296" y1="118" x2="342" y2="86"/>
    <line class="fig-line-dashed" x1="296" y1="156" x2="342" y2="182"/>
    <line class="fig-line-accent" x1="478" y1="86" x2="524" y2="122"/>
    <line class="fig-line-dashed" x1="478" y1="182" x2="524" y2="152"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 524 108 L 484 74" marker-end="url(#cn-w4-arr-14)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 342 72 L 300 104" marker-end="url(#cn-w4-arr-14)"/>
    <rect class="fig-box-blue" x="32" y="108" width="96" height="58" rx="6"/>
    <rect class="fig-box-purple" x="166" y="96" width="130" height="82" rx="6"/>
    <rect class="fig-box-green" x="342" y="54" width="136" height="64" rx="6"/>
    <rect class="fig-box-orange" x="342" y="150" width="136" height="64" rx="6"/>
    <rect class="fig-box-accent" x="524" y="102" width="164" height="70" rx="6"/>
    <rect class="fig-box" x="32" y="226" width="360" height="82" rx="5"/>
    <rect class="fig-box" x="404" y="226" width="284" height="82" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">BGP-4 域间选路：各 AS 度量不同＋策略约束 → 交换 AS-PATH 找「较好且无环」路由</text>
    <text class="fig-blue" x="80" y="133" text-anchor="middle" font-size="11.5">AS4</text>
    <text class="fig-muted" x="80" y="152" text-anchor="middle" font-size="9.5">收到 &lt;AS1,3,5&gt;</text>
    <text class="fig-accent" x="147" y="128" text-anchor="middle" font-size="8.5">eBGP</text>
    <text class="fig-purple" x="231" y="118" text-anchor="middle" font-size="11.5">AS1（主干网）</text>
    <text class="fig-muted" x="231" y="136" text-anchor="middle" font-size="9.5">内部度量：时延</text>
    <text class="fig-purple" x="231" y="154" text-anchor="middle" font-size="9">R1a ──iBGP── R1b</text>
    <text class="fig-green" x="410" y="76" text-anchor="middle" font-size="11">AS3（优选路径）</text>
    <text class="fig-muted" x="410" y="93" text-anchor="middle" font-size="9.5">内部度量：带宽</text>
    <text class="fig-green" x="410" y="109" text-anchor="middle" font-size="9">通告 N: &lt;AS3, AS5&gt;</text>
    <text class="fig-orange" x="410" y="172" text-anchor="middle" font-size="11">AS2（策略避开）</text>
    <text class="fig-muted" x="410" y="189" text-anchor="middle" font-size="9.5">内部度量：跳数</text>
    <text class="fig-muted" x="410" y="205" text-anchor="middle" font-size="9">政治/商业策略不选</text>
    <text class="fig-accent" x="606" y="128" text-anchor="middle" font-size="11.5">AS5（含目的网 N）</text>
    <text class="fig-label" x="606" y="148" text-anchor="middle" font-size="9.5">源发通告 N: &lt;AS5&gt;</text>
    <text class="fig-label" x="42" y="246" font-size="9.5">• 选路目标：异构度量+政治经济策略 → 求「可达、较好、不兜圈子」</text>
    <text class="fig-red" x="42" y="266" font-size="9.5">• 防环机制：收到 UPDATE 中 AS-PATH 若含本 AS 号，立即拒收！</text>
    <text class="fig-muted" x="44" y="286" font-size="9.5">• 发言人须同时运行 BGP（TCP 179 建立 eBGP/iBGP）与本域 IGP</text>
    <text class="fig-purple" x="416" y="246" font-size="10">BGP-4 四类报文（初连全表，后续增量）：</text>
    <text class="fig-label" x="416" y="266" font-size="9.5">① OPEN（建立邻居） ② UPDATE（通告/撤销）</text>
    <text class="fig-label" x="416" y="286" font-size="9.5">③ KEEPALIVE（保活） ④ NOTIFICATION（差错）</text>
  </svg>
  <figcaption>图 4-14：BGP-4 在异构度量与策略约束下通过 TCP 179 传递 `AS-PATH` 路径向量构造无环树形连通图</figcaption>
</figure>

配置 BGP 时，每个 AS 的管理员至少选一个路由器作为该 AS 的 <strong>BGP 发言人</strong>，通常是 <strong>BGP 边界路由器</strong>；两个发言人一般通过一个共享网络相连。向不同 AS 的发言人交换路由信息，必须先建立 <strong>TCP 连接（端口 179）</strong>，在此连接上交换 BGP 报文以建立 <strong>BGP 会话</strong>；双方互称<strong>邻居（对等站，peer）</strong>。发言人除运行 BGP 外，还必须运行本 AS 使用的 IGP（如 OSPF、RIP）。

发言人交换的是<strong>网络可达性信息</strong>——到达某网络要经过的一系列 AS，即<strong>路径向量</strong>。收到后按策略从中选出较好路由，构造出<strong>树形、无环路</strong>的 AS 连通图。

- 例：AS2 的发言人通知主干网“到 N1、N2、N3、N4 可经过 AS2”；主干网再通告“到这些网络可沿路径 AS1→AS2”，`AS1, AS2` 就是路径向量。
- AS3 收到含 `AS3` 的路径向量时不能采用，否则会兜圈子。

| 报文 | 作用 |
|---|---|
| <strong>OPEN（打开）</strong> | 与相邻另一个 BGP 发言人建立关系，通信初始化 |
| <strong>UPDATE（更新）</strong> | 通告某一路由信息，或列出要撤销的多条路由 |
| <strong>KEEPALIVE（保活）</strong> | 周期性证实邻居的连通性 |
| <strong>NOTIFICATION（通知）</strong> | 发送检测到的差错 |

BGP 刚开始运行时，邻居交换<strong>整个 BGP 路由表</strong>；之后只在发生变化时<strong>更新变化的部分</strong>，以节省带宽、减少路由器处理开销。

> <strong>★考点</strong>：BGP-4 报文封装在 <strong>TCP</strong> 报文段中；BGP 交换的是<strong>路径向量</strong>（AS 序列），不是距离或带宽；BGP 追求的是“较好且不兜圈子”的路由，不是最佳路由。

### 4.9 IPv4 数据报的首部格式

IPv4 数据报首部 = <strong>20 字节固定部分</strong> + <strong>0～40 字节可变部分</strong>（可选字段 + 填充），最大 60 字节；按 32 bit（4 字节）一行来描述。

<figure class="cpd-figure not-content" id="cn-w4-fig-15">
  <svg viewBox="0 0 720 356" role="img" aria-label="IPv4数据报20字节固定首部与可变首部32位结构、三大单位口诀与帧偏移定位图">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="324" rx="8"/>
    <path class="fig-line-accent" d="M 614 60 L 624 60 L 624 230 L 614 230"/>
    <path class="fig-line-dashed" d="M 614 234 L 624 234 L 624 262 L 614 262"/>
    <rect class="fig-box-blue" x="32" y="60" width="72" height="34" rx="3"/>
    <rect class="fig-box-accent" x="104" y="60" width="72" height="34" rx="3"/>
    <rect class="fig-box" x="176" y="60" width="144" height="34" rx="3"/>
    <rect class="fig-box-accent" x="320" y="60" width="288" height="34" rx="3"/>
    <rect class="fig-box-orange" x="32" y="96" width="288" height="34" rx="3"/>
    <rect class="fig-box-orange" x="320" y="96" width="54" height="34" rx="3"/>
    <rect class="fig-box-orange" x="374" y="96" width="234" height="34" rx="3"/>
    <rect class="fig-box-green" x="32" y="132" width="144" height="34" rx="3"/>
    <rect class="fig-box-green" x="176" y="132" width="144" height="34" rx="3"/>
    <rect class="fig-box-green" x="320" y="132" width="288" height="34" rx="3"/>
    <rect class="fig-box-purple" x="32" y="168" width="576" height="30" rx="3"/>
    <rect class="fig-box-purple" x="32" y="200" width="576" height="30" rx="3"/>
    <rect class="fig-box-dashed" x="32" y="234" width="576" height="28" rx="3"/>
    <rect class="fig-box" x="32" y="270" width="656" height="58" rx="5"/>
    <text class="fig-title" x="360" y="36" text-anchor="middle" font-size="13">IPv4 首部格式：20 B 固定部分（5 行 × 4 B）＋ 0–40 B 可变部分</text>
    <text class="fig-muted" x="36" y="54" font-size="9">0</text>
    <text class="fig-muted" x="104" y="54" text-anchor="middle" font-size="9">4</text>
    <text class="fig-muted" x="176" y="54" text-anchor="middle" font-size="9">8</text>
    <text class="fig-muted" x="320" y="54" text-anchor="middle" font-size="9">16</text>
    <text class="fig-muted" x="374" y="54" text-anchor="middle" font-size="9">19</text>
    <text class="fig-muted" x="604" y="54" text-anchor="end" font-size="9">31 bit</text>
    <text class="fig-blue" x="68" y="75" text-anchor="middle" font-size="10">版本</text>
    <text class="fig-muted" x="68" y="88" text-anchor="middle" font-size="8.5">4 bit (=4)</text>
    <text class="fig-accent" x="140" y="75" text-anchor="middle" font-size="10">首部长度</text>
    <text class="fig-muted" x="140" y="88" text-anchor="middle" font-size="8.5">4b (×4B)</text>
    <text class="fig-label" x="248" y="75" text-anchor="middle" font-size="10">区分服务</text>
    <text class="fig-muted" x="248" y="88" text-anchor="middle" font-size="8.5">8 bit</text>
    <text class="fig-accent" x="464" y="75" text-anchor="middle" font-size="10">总长度（首部＋数据载荷）</text>
    <text class="fig-muted" x="464" y="88" text-anchor="middle" font-size="8.5">16 bit（单位：1 B，最大 65535）</text>
    <text class="fig-orange" x="176" y="111" text-anchor="middle" font-size="10">标识（同一数据报各分片恒同）</text>
    <text class="fig-muted" x="176" y="124" text-anchor="middle" font-size="8.5">16 bit</text>
    <text class="fig-orange" x="347" y="111" text-anchor="middle" font-size="9.5">标志</text>
    <text class="fig-muted" x="347" y="124" text-anchor="middle" font-size="8">3 bit</text>
    <text class="fig-orange" x="491" y="111" text-anchor="middle" font-size="10">片偏移（分片载荷相对原数据起点）</text>
    <text class="fig-muted" x="491" y="124" text-anchor="middle" font-size="8.5">13 bit（单位：8 B）</text>
    <text class="fig-green" x="104" y="147" text-anchor="middle" font-size="10">生存时间 TTL</text>
    <text class="fig-muted" x="104" y="160" text-anchor="middle" font-size="8.5">8 bit（每跳减1）</text>
    <text class="fig-green" x="248" y="147" text-anchor="middle" font-size="10">协议 Protocol</text>
    <text class="fig-muted" x="248" y="160" text-anchor="middle" font-size="8.5">8b (1/6/17/89)</text>
    <text class="fig-green" x="464" y="147" text-anchor="middle" font-size="10">首部检验和（只检验首部，每跳重算）</text>
    <text class="fig-muted" x="464" y="160" text-anchor="middle" font-size="8.5">16 bit</text>
    <text class="fig-purple" x="320" y="187" text-anchor="middle" font-size="10.5">源 IP 地址（32 bit，IP 首部内偏移 12~15 B）</text>
    <text class="fig-purple" x="320" y="219" text-anchor="middle" font-size="10.5">目的 IP 地址（32 bit，IP 首部内偏移 16~19 B → 以太网帧第 31~34 B）</text>
    <text class="fig-muted" x="320" y="252" text-anchor="middle" font-size="10">可选字段（0–40 B）＋ 全 0 填充（补齐为 4 B 整数倍）</text>
    <text class="fig-accent" x="658" y="136" text-anchor="middle" font-size="10">固定</text>
    <text class="fig-accent" x="658" y="152" text-anchor="middle" font-size="10">20 B</text>
    <text class="fig-muted" x="658" y="168" text-anchor="middle" font-size="9">(5行×4B)</text>
    <text class="fig-muted" x="658" y="252" text-anchor="middle" font-size="9">0~40 B</text>
    <text class="fig-label" x="44" y="290" font-size="10">★ 单位口诀「一种八片首四总一」：片偏移×8 B ｜ 首部长度×4 B（范围 5~15 即 20~60 B）｜ 总长度×1 B</text>
    <text class="fig-muted" x="44" y="311" font-size="9.5">• 标志 3b = [0 | DF | MF]（DF=1 禁分片；MF=1 后有分片）｜ 协议号：1=ICMP，6=TCP，17=UDP，89=OSPF</text>
  </svg>
  <figcaption>图 4-15：IPv4 首部 32 位对齐结构、“一种八片首四总一”单位换算与以太网帧内目的 IP 字节偏移定位</figcaption>
</figure>

| 字段 | 位数 | 含义 |
|---|---|---|
| 版本 | 4 | IP 协议版本，双方必须一致，现为 4 |
| 首部长度 | 4 | 首部长度，<strong>以 4 字节为单位</strong>；最小 5（20 B），最大 15（60 B） |
| 区分服务 | 8 | 旧称服务类型；用于区分服务质量，仅在使用区分服务时起作用 |
| 总长度 | 16 | <strong>首部 + 数据载荷</strong>的总字节数，最大 65535 |
| 标识 | 16 | 每产生一个数据报计数器加 1 并填入；<strong>同一数据报的各分片标识相同</strong> |
| 标志 | 3 | 保留位（必须 0）/ DF（1 不允许分片）/ MF（1 后面还有分片，0 为最后一片） |
| 片偏移 | 13 | 本片数据载荷在原数据报中的位置，<strong>以 8 字节为单位</strong> |
| 生存时间 TTL | 8 | 现以跳数为单位，路由器转发时减 1，为 0 则丢弃 |
| 协议 | 8 | 数据部分使用的协议：1 = ICMP，6 = TCP，17 = UDP |
| 首部检验和 | 16 | 只检验首部，每经一个路由器重新计算 |
| 源 / 目的 IP 地址 | 各 32 | 发送方 / 接收方主机的 IP 地址 |
| 可选字段 + 填充 | 0–40 B | 支持排错、测量、安全等；填充全 0，保证首部为 4 字节整数倍 |

- <strong>首部长度 vs 总长度</strong>：首部长度字段取值 0101 → 5 × 4 = 20 B；总长度字段为 1020 → 1020 B，则数据载荷 = 1020 − 20 = 1000 B。
- <strong>TTL 的作用</strong>：若静态路由的下一跳互相指错（R1 认为去 N2 应交给 R2，R2 认为应交给 R1），分组就会在 R1、R2 之间来回转发形成<strong>路由环路</strong>；TTL 每经一个路由器减 1，减到 0 即丢弃，保证数据报不会永久兜圈。
- <strong>首部检验和</strong>：因 TTL、标志、片偏移等可能变化，每经路由器都要重算；IPv6 中路由器不再计算首部检验和，以加快转发。

<figure class="cpd-figure not-content" id="cn-w4-fig-16">
  <svg viewBox="0 0 720 336" role="img" aria-label="IPv6固定40字节基本首部格式、128位冒号十六进制零压缩规则及双协议栈和隧道过渡技术">
    <defs>
      <marker id="cn-w4-arr-16" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="304" rx="8"/>
    <rect class="fig-box" x="28" y="48" width="346" height="258" rx="6"/>
    <rect class="fig-box" x="386" y="48" width="306" height="258" rx="6"/>
    <rect class="fig-box-blue" x="40" y="76" width="66" height="28" rx="3"/>
    <rect class="fig-box" x="108" y="76" width="86" height="28" rx="3"/>
    <rect class="fig-box-accent" x="196" y="76" width="166" height="28" rx="3"/>
    <rect class="fig-box-green" x="40" y="106" width="152" height="28" rx="3"/>
    <rect class="fig-box-purple" x="194" y="106" width="86" height="28" rx="3"/>
    <rect class="fig-box-orange" x="282" y="106" width="80" height="28" rx="3"/>
    <rect class="fig-box-purple" x="40" y="136" width="322" height="40" rx="3"/>
    <rect class="fig-box-purple" x="40" y="178" width="322" height="40" rx="3"/>
    <rect class="fig-box-subtle" x="40" y="224" width="322" height="72" rx="4"/>
    <rect class="fig-box-blue" x="398" y="76" width="282" height="86" rx="4"/>
    <rect class="fig-box-subtle" x="398" y="170" width="282" height="126" rx="4"/>
    <line class="fig-line-accent" x1="468" y1="274" x2="610" y2="274" marker-end="url(#cn-w4-arr-16)"/>
    <rect class="fig-box-orange" x="488" y="262" width="102" height="22" rx="3"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">IPv6 核心拓展：固定 40 B 基本首部（取消检验和）、零压缩记法与过渡技术</text>
    <text class="fig-purple" x="201" y="67" text-anchor="middle" font-size="11.5">① IPv6 基本首部（恒定 40 B，8 个字段）</text>
    <text class="fig-blue" x="73" y="94" text-anchor="middle" font-size="9.5">版本=6(4b)</text>
    <text class="fig-label" x="151" y="94" text-anchor="middle" font-size="9.5">通信量类(8b)</text>
    <text class="fig-accent" x="279" y="94" text-anchor="middle" font-size="9.5">流标号 Flow Label (20b)</text>
    <text class="fig-green" x="116" y="124" text-anchor="middle" font-size="9.5">有效载荷长度 (16b)</text>
    <text class="fig-purple" x="237" y="124" text-anchor="middle" font-size="9">下一个首部(8b)</text>
    <text class="fig-orange" x="322" y="124" text-anchor="middle" font-size="9">跳数限制(8b)</text>
    <text class="fig-purple" x="201" y="160" text-anchor="middle" font-size="10.5">源 IPv6 地址（128 bit = 16 B）</text>
    <text class="fig-purple" x="201" y="202" text-anchor="middle" font-size="10.5">目的 IPv6 地址（128 bit = 16 B）</text>
    <text class="fig-label" x="48" y="241" font-size="9.5">• 取消首部检验和：路由器转发免重算，大幅提升吞吐率</text>
    <text class="fig-label" x="48" y="259" font-size="9.5">• 取消中间路由器分片：仅允许【源主机】完成分片！</text>
    <text class="fig-muted" x="48" y="277" font-size="9">• 扩展首部归入有效载荷，由「下一个首部」链式指明</text>
    <text class="fig-blue" x="539" y="67" text-anchor="middle" font-size="11.5">② 冒号十六进制与 IPv4→IPv6 过渡</text>
    <text class="fig-blue" x="408" y="94" font-size="9.5">8 组×16b：FF01:0000:0000:0000:0000:0123:0000:0045</text>
    <text class="fig-label" x="408" y="114" font-size="10">零压缩后：FF01 :: 123 : 0 : 45</text>
    <text class="fig-muted" x="408" y="133" font-size="9">• 每组前导 0 可省（全 0 至少留一个 0）</text>
    <text class="fig-muted" x="408" y="150" font-size="9">• 连续全 0 组压缩为「::」，全地址最多用 1 次（防歧义）</text>
    <text class="fig-green" x="406" y="188" font-size="9.5">① 双协议栈：节点装 IPv4+IPv6 双栈，按域名解析选路</text>
    <text class="fig-orange" x="406" y="208" font-size="9.5">② 隧道技术（6in4）：将 IPv6 报文装入 IPv4 载荷传输</text>
    <text class="fig-muted" x="408" y="226" font-size="9">  穿越 IPv4 海洋后在隧道出口剥除 IPv4 首部还原 IPv6</text>
    <text class="fig-muted" x="408" y="244" font-size="9">• 地址根本解决耗尽：2¹²⁸ 空间，支持无状态自动配置</text>
    <text class="fig-purple" x="436" y="277" text-anchor="middle" font-size="9">IPv6 孤岛</text>
    <text class="fig-orange" x="539" y="276" text-anchor="middle" font-size="8.5">[IPv4头|IPv6报]</text>
    <text class="fig-purple" x="644" y="277" text-anchor="middle" font-size="9">IPv6 孤岛</text>
  </svg>
  <figcaption>图 4-16：IPv6 固定 40 字节基本首部（取消首部检验和与路由器分片）、`::` 零压缩规则及双协议栈 / 隧道过渡机制</figcaption>
</figure>

> <strong>★考点</strong>：以太网帧中，前 14 字节为帧头，IP 首部从第 15 字节开始；目的 IP 地址在 IP 首部中偏移 16 字节，故位于以太网帧的<strong>第 31～34 字节</strong>。帧头的目的 MAC 地址填的是<strong>默认网关的 MAC 地址</strong>，不是目的主机的 MAC。
>
> <strong>注意</strong>：IP 分组经过路由器时，TTL 减 1、首部检验和重算；若总长度超过 MTU 还要分片，此时<strong>总长度、标志、片偏移</strong>三个字段都要修改。

### 4.10 IPv4 数据报的分片

每种链路层协议都规定了帧数据载荷的最大长度，即<strong>最大传输单元 MTU</strong>（以太网为 1500 字节）。IP 数据报总长度超过 MTU 时无法封装成帧，必须<strong>分片</strong>，每片各自加首部后独立封装传输。分片依赖首部的标识、标志、片偏移三个字段。

<figure class="cpd-figure not-content" id="cn-w4-fig-17">
  <svg viewBox="0 0 720 356" role="img" aria-label="IPv4数据报分片计算追踪：3820字节数据报经MTU=1500三分片、分片2二次分片及2018真题8字节对齐推导">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="324" rx="8"/>
    <rect class="fig-box-purple" x="32" y="52" width="66" height="28" rx="3"/>
    <rect class="fig-box-accent" x="98" y="52" width="590" height="28" rx="3"/>
    <rect class="fig-box-purple" x="32" y="92" width="60" height="28" rx="3"/>
    <rect class="fig-box-blue" x="92" y="92" width="212" height="28" rx="3"/>
    <rect class="fig-box" x="312" y="92" width="376" height="28" rx="3"/>
    <rect class="fig-box-purple" x="32" y="128" width="60" height="28" rx="3"/>
    <rect class="fig-box-blue" x="92" y="128" width="212" height="28" rx="3"/>
    <rect class="fig-box" x="312" y="128" width="376" height="28" rx="3"/>
    <rect class="fig-box-purple" x="32" y="164" width="60" height="28" rx="3"/>
    <rect class="fig-box-green" x="92" y="164" width="166" height="28" rx="3"/>
    <rect class="fig-box" x="312" y="164" width="376" height="28" rx="3"/>
    <rect class="fig-box" x="32" y="204" width="320" height="122" rx="5"/>
    <rect class="fig-box-orange" x="364" y="204" width="324" height="122" rx="5"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">IPv4 数据报分片计算追踪：3820 B 经 MTU=1500 分片、二次分片与 8B 对齐公式</text>
    <text class="fig-purple" x="65" y="70" text-anchor="middle" font-size="9.5">首部 20B</text>
    <text class="fig-accent" x="393" y="70" text-anchor="middle" font-size="10">原载荷 3800 B（字节 0–3799）｜ 总长 3820 B，标识=x，DF=0，MF=0，片偏移=0</text>
    <text class="fig-purple" x="62" y="110" text-anchor="middle" font-size="9.5">20B 头</text>
    <text class="fig-blue" x="198" y="110" text-anchor="middle" font-size="9.5">分片1：字节 0–1399 (1400B)</text>
    <text class="fig-label" x="324" y="110" font-size="9.5">总长=1420 B ｜ 标识=x ｜ MF=1 ｜ 片偏移 = 0÷8 = 0</text>
    <text class="fig-purple" x="62" y="146" text-anchor="middle" font-size="9.5">20B 头</text>
    <text class="fig-blue" x="198" y="146" text-anchor="middle" font-size="9.5">分片2：字节 1400–2799 (1400B)</text>
    <text class="fig-label" x="324" y="146" font-size="9.5">总长=1420 B ｜ 标识=x ｜ MF=1 ｜ 片偏移 = 1400÷8 = 175</text>
    <text class="fig-purple" x="62" y="182" text-anchor="middle" font-size="9.5">20B 头</text>
    <text class="fig-green" x="175" y="182" text-anchor="middle" font-size="9.5">分片3：2800–3799 (1000B)</text>
    <text class="fig-green" x="324" y="182" font-size="9.5">总长=1020 B ｜ 标识=x ｜ MF=0（末片）｜ 偏移 = 2800÷8 = 350</text>
    <text class="fig-blue" x="44" y="224" font-size="10.5">① 分片 2 二次分片（拆为 800B + 600B）</text>
    <text class="fig-label" x="44" y="244" font-size="9.5">• 子片 2-1（1400–2199）：偏移 = 1400÷8 = 175，MF=1</text>
    <text class="fig-label" x="44" y="263" font-size="9.5">• 子片 2-2（2200–2799）：偏移 = 2200÷8 = 275，MF=1</text>
    <text class="fig-muted" x="44" y="283" font-size="9.5">• 因原报文后仍有分片 3，两子片 MF 均须保持为 1！</text>
    <text class="fig-muted" x="44" y="303" font-size="9.5">• 各分片独立选路，仅在【最终目的主机】重组还原</text>
    <text class="fig-orange" x="376" y="224" font-size="10.5">② 2018 真题陷阱：除末片外载荷必为 8 的倍数</text>
    <text class="fig-label" x="376" y="244" font-size="9.5">• 题设：总长 1500B（载荷 1480B）经 MTU=800 链路</text>
    <text class="fig-label" x="376" y="263" font-size="9.5">• 780÷8 = 97.5 非整 → 向下取整 97×8 = 776 B！</text>
    <text class="fig-label" x="376" y="283" font-size="9.5">• 片1：载荷 776B，总长 796B，MF=1，片偏移 = 0</text>
    <text class="fig-label" x="376" y="303" font-size="9.5">• 片2：载荷 704B，总长 724B，MF=0，偏移 = 776÷8 = 97</text>
  </svg>
  <figcaption>图 4-17：IPv4 分片首部三字段（标识、MF、片偏移×8B）追踪、二次分片继承规则与 2018 真题 8 字节向下取整推导</figcaption>
</figure>

原数据报为 20 B 首部 + 3800 B 数据载荷（总长 3820 B），MTU 1500 时每片数据载荷最多 1480 B，按 1400/1400/1000 切分：

| 分片 | 数据载荷字节范围 | 总长度 | 标识 | DF | MF | 片偏移 |
|---|---|---|---|---|---|---|
| 原数据报（未分片） | 0–3799 | 3820 | x | 0 | 0 | 0 |
| 分片 1 | 0–1399 | 1420 | x | 0 | 1 | 0 |
| 分片 2 | 1400–2799 | 1420 | x | 0 | 1 | 175 |
| 分片 3 | 2800–3799 | 1020 | x | 0 | 0 | 350 |

若分片 2 再经一个 MTU 更小的网络，又分成 800 B 与 600 B 两片：两片标识仍为 x，片偏移分别为 1400 ÷ 8 = 175 与 (1400 + 800) ÷ 8 = 275；因原数据报后面还有分片 3，两片的 MF 仍为 1。

> <strong>★考点</strong>：片偏移以 <strong>8 字节</strong>为单位，因此除最后一片外，每片数据载荷必须是 8 的整数倍。2018 真题：总长 1500 B、首部 20 B → 数据载荷 1480 B；MTU 800 → 每片载荷不超过 780 B，取 8 的倍数 776 B，故第一片载荷 776 B、总长 796 B、片偏移 0、MF=1；第二片载荷 704 B、总长 724 B、片偏移 97、MF=0。
>
> <strong>注意</strong>：分片只在“总长度 > MTU”时发生；DF=1 表示不允许分片，此时无法拆分该数据报。

## 第5章 运输层

### 5.1 运输层概述：应用进程之间的端到端通信

物理层、数据链路层、网络层共同解决<strong>主机通过异构网络互联</strong>的问题，作用范围是<strong>主机到主机</strong>。但网络中真正通信的实体是主机里的<strong>应用进程</strong>（AP，Application Process）。运输层的任务，就是为运行在不同主机上的应用进程提供直接的通信服务，因此运输层协议又称<strong>端到端协议</strong>，作用范围是<strong>应用进程到应用进程</strong>。

运输层用<strong>端口</strong>区分同一主机中的不同应用进程：发送方把应用报文交给不同端口的运输层实体，接收方再按端口把数据交付给对应的应用进程。

<figure class="cpd-figure not-content" id="cn-w5-fig-1">
  <svg viewBox="0 0 720 344" role="img" aria-label="运输层应用进程端到端逻辑通信与网络层主机到主机实际物理传输路径对比">
    <defs>
      <marker id="cn-w5-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="208" height="214" rx="6"/>
    <rect class="fig-box-subtle" x="284" y="138" width="152" height="118" rx="6"/>
    <rect class="fig-box-subtle" x="488" y="42" width="208" height="214" rx="6"/>
    <line class="fig-line-dashed" x1="218" y1="131" x2="502" y2="131" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-start="url(#cn-w5-arr-1)" marker-end="url(#cn-w5-arr-1)"/>
    <line class="fig-line-dashed" x1="218" y1="178" x2="298" y2="178"/>
    <line class="fig-line-dashed" x1="422" y1="178" x2="502" y2="178"/>
    <path class="fig-line" d="M 80 98 L 80 114" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 128 148 L 128 162" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 128 194 L 128 210" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 128 242 L 128 268 L 336 268 L 336 242" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 336 210 L 336 194" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 384 194 L 384 210" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 384 242 L 384 268 L 544 268 L 544 242" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 544 210 L 544 194" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 544 162 L 544 148" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <path class="fig-line" d="M 544 114 L 544 98" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-1)"/>
    <rect class="fig-box-blue" x="38" y="68" width="84" height="30" rx="4"/>
    <rect class="fig-box-purple" x="134" y="68" width="84" height="30" rx="4"/>
    <rect class="fig-box-accent" x="38" y="114" width="180" height="34" rx="4"/>
    <rect class="fig-box-orange" x="38" y="162" width="180" height="32" rx="4"/>
    <rect class="fig-box" x="38" y="210" width="180" height="32" rx="4"/>
    <rect class="fig-box-orange" x="298" y="162" width="124" height="32" rx="4"/>
    <rect class="fig-box" x="298" y="210" width="124" height="32" rx="4"/>
    <rect class="fig-box-blue" x="502" y="68" width="84" height="30" rx="4"/>
    <rect class="fig-box-purple" x="598" y="68" width="84" height="30" rx="4"/>
    <rect class="fig-box-accent" x="502" y="114" width="180" height="34" rx="4"/>
    <rect class="fig-box-orange" x="502" y="162" width="180" height="32" rx="4"/>
    <rect class="fig-box" x="502" y="210" width="180" height="32" rx="4"/>
    <rect class="fig-box" x="262" y="96" width="196" height="24" rx="4"/>
    <rect class="fig-box" x="24" y="286" width="672" height="44" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">运输层端到端（进程↔进程）逻辑通信 vs 网络核心逐跳物理传输路径</text>
    <text class="fig-label" x="128" y="58" text-anchor="middle" font-size="12">主机 A（192.168.1.2）</text>
    <text class="fig-label" x="360" y="154" text-anchor="middle" font-size="11">核心路由器 R（仅下三层）</text>
    <text class="fig-label" x="592" y="58" text-anchor="middle" font-size="12">主机 B（203.0.113.8）</text>
    <text class="fig-blue" x="80" y="87" text-anchor="middle" font-size="11">AP1 :49152</text>
    <text class="fig-purple" x="176" y="87" text-anchor="middle" font-size="11">AP2 :49153</text>
    <text class="fig-accent" x="128" y="135" text-anchor="middle" font-size="12">运输层（TCP / UDP）</text>
    <text class="fig-orange" x="128" y="182" text-anchor="middle" font-size="11">网络层（IP 主机寻址）</text>
    <text class="fig-label" x="128" y="230" text-anchor="middle" font-size="11">数据链路层 + 物理层</text>
    <text class="fig-orange" x="360" y="182" text-anchor="middle" font-size="11">网络层（分组转发）</text>
    <text class="fig-label" x="360" y="230" text-anchor="middle" font-size="11">链路层 + 物理层</text>
    <text class="fig-blue" x="544" y="87" text-anchor="middle" font-size="11">Web :80</text>
    <text class="fig-purple" x="640" y="87" text-anchor="middle" font-size="11">DNS :53</text>
    <text class="fig-accent" x="592" y="135" text-anchor="middle" font-size="12">运输层（TCP / UDP）</text>
    <text class="fig-orange" x="592" y="182" text-anchor="middle" font-size="11">网络层（IP 主机寻址）</text>
    <text class="fig-label" x="592" y="230" text-anchor="middle" font-size="11">数据链路层 + 物理层</text>
    <text class="fig-blue" x="360" y="112" text-anchor="middle" font-size="11">端到端逻辑通信（屏蔽网络核心细节）</text>
    <text class="fig-orange" x="360" y="280" text-anchor="middle" font-size="10">实际物理路径：发送端逐层封装 → 路由器拆封至网络层转发 → 接收端逐层解封按端口交付</text>
    <text class="fig-label" x="38" y="304" font-size="11">• 套接字 Socket = (IP 地址 : 端口号)：网络层 IP 只送到「主机」，运输层端口号才精准定位主机内的「应用进程」。</text>
    <text class="fig-muted" x="38" y="322" font-size="9.2">• 端点标识：UDP 无连接，由二元组 (目的 IP, 目的端口) 分用；TCP 面向连接，由四元组 (源 IP, 源端口, 目的 IP, 目的端口) 唯一标识。</text>
  </svg>
  <figcaption>图 5-1：运输层应用进程间端到端逻辑通信、路由器三层转发边界与套接字（Socket）四元组标识</figcaption>
</figure>

- 网络层解决<strong>主机到主机</strong>的通信；运输层解决<strong>进程到进程</strong>的通信。
- <strong>逻辑通信</strong>：两个运输层实体之间并没有一条水平方向的物理连接，数据实际要逐层封装、逐层上传，但应用层看起来就像有一条端到端的通路。
- 英特网运输层向应用层提供两种协议：TCP（面向连接）与 <strong>UDP（无连接）</strong>。

### 5.2 端口号：运输层的进程标识

不同操作系统（Windows、Linux、macOS）的<strong>进程标识符 PID</strong> 格式不同，无法在互联网上统一使用。TCP/IP 体系改用<strong>端口号</strong>统一标识应用进程。

端口号占 <strong>16 比特</strong>，取值 <strong>0～65535</strong>，分三类：

| 类别 | 范围 | 说明与例子 |
|---|---|---|
| 熟知端口号 | 0～1023 | 由 IANA 指派给最重要的应用协议，如 FTP 21、HTTP 80、DNS 53 |
| 登记端口号 | 1024～49151 | 供没有熟知端口号的程序使用，须向 IANA 登记以防重复，如微软远程桌面 3389 |
| 短暂端口号 | 49152～65535 | 留给客户进程临时选择；通信结束后可再供其他客户进程使用 |

<figure class="cpd-figure not-content" id="cn-w5-fig-2">
  <svg viewBox="0 0 720 276" role="img" aria-label="16比特端口号空间三类划分与408常考应用层协议熟知端口号对照">
    <rect class="fig-box-blue" x="28" y="46" width="184" height="54" rx="5"/>
    <rect class="fig-box" x="216" y="46" width="256" height="54" rx="5"/>
    <rect class="fig-box-green" x="476" y="46" width="216" height="54" rx="5"/>
    <rect class="fig-box-subtle" x="28" y="130" width="356" height="130" rx="6"/>
    <rect class="fig-box-subtle" x="396" y="130" width="296" height="130" rx="6"/>
    <line class="fig-divider" x1="40" y1="158" x2="372" y2="158"/>
    <line class="fig-divider" x1="408" y1="158" x2="680" y2="158"/>
    <text class="fig-title" x="360" y="28" text-anchor="middle" font-size="13">16 比特端口号空间（0～65535）三类划分与 408 常考协议端口速查</text>
    <text class="fig-blue" x="120" y="69" text-anchor="middle" font-size="12">服务器端：熟知端口号</text>
    <text class="fig-label" x="120" y="88" text-anchor="middle" font-size="11">0 ～ 1023（IANA 统一指派）</text>
    <text class="fig-label" x="344" y="69" text-anchor="middle" font-size="12">服务器端：登记端口号</text>
    <text class="fig-muted" x="344" y="88" text-anchor="middle" font-size="11">1024 ～ 49151（向 IANA 登记）</text>
    <text class="fig-green" x="584" y="69" text-anchor="middle" font-size="12">客户端：短暂（临时）端口号</text>
    <text class="fig-label" x="584" y="88" text-anchor="middle" font-size="11">49152 ～ 65535（动态分配释放）</text>
    <text class="fig-muted" x="28" y="116" font-size="10">0</text>
    <text class="fig-muted" x="212" y="116" text-anchor="end" font-size="10">1023</text>
    <text class="fig-muted" x="218" y="116" font-size="10">1024</text>
    <text class="fig-muted" x="472" y="116" text-anchor="end" font-size="10">49151</text>
    <text class="fig-muted" x="478" y="116" font-size="10">49152</text>
    <text class="fig-muted" x="692" y="116" text-anchor="end" font-size="10">65535</text>
    <text class="fig-blue" x="42" y="149" font-size="12">基于 TCP 的常考熟知端口（面向连接）</text>
    <text class="fig-label" x="42" y="176" font-size="11">• FTP 数据 20 / 控制 21　• SSH 22　• Telnet 23</text>
    <text class="fig-label" x="42" y="196" font-size="11">• SMTP 25　• HTTP 80　• POP3 110　• HTTPS 443</text>
    <text class="fig-orange" x="42" y="218" font-size="11">• BGP 179（唯一直接运行在 TCP 上的路由协议！）</text>
    <text class="fig-muted" x="42" y="242" font-size="10">注：登记端口典型代表如 RDP 3389、MySQL 3306、Redis 6379</text>
    <text class="fig-green" x="410" y="149" font-size="12">基于 UDP 的常考熟知端口（无连接）</text>
    <text class="fig-label" x="410" y="176" font-size="11">• DNS 53（默认 UDP；区域传送/超 512B 用 TCP）</text>
    <text class="fig-label" x="410" y="196" font-size="11">• DHCP 服务端 67 / 客户端 68　• TFTP 69</text>
    <text class="fig-label" x="410" y="218" font-size="11">• SNMP 161 / 162　• RIP 路由协议 520</text>
    <text class="fig-purple" x="410" y="242" font-size="10">★ 本地意义：TCP 80 端口与 UDP 80 端口各自独立互不冲突</text>
  </svg>
  <figcaption>图 5-2：16 比特端口号三区间划分与 CS408 必背 TCP/UDP 应用层协议端口号映射表</figcaption>
</figure>

> <strong>注意</strong>：端口号不是看得见摸得着的物理端口，只是<strong>区分本机应用进程的标识符</strong>；同一端口号在不同主机上互不相干。

### 5.3 复用与分用

复用（multiplexing）发生在发送方：多个应用进程的报文交给运输层，分别用 UDP 或 TCP 封装；UDP 封装成<strong>UDP 用户数据报</strong>，TCP 封装成 <strong>TCP 报文段</strong>。到网络层，UDP 用户数据报和 TCP 报文段又统一封装进 <strong>IP 数据报</strong>，称为 <strong>IP 复用</strong>。

分用（demultiplexing）发生在接收方，分两步：

1. <strong>网络层按协议字段分用</strong>：IP 数据报首部的协议字段值为 <strong>6</strong> 表示数据载荷是 TCP 报文段，为 <strong>17</strong> 表示是 UDP 用户数据报；据此上交运输层的 TCP 或 UDP。
2. <strong>运输层按端口号分用</strong>：TCP/UDP 首部中的目的端口号决定把数据载荷交付给哪个应用进程。

<figure class="cpd-figure not-content" id="cn-w5-fig-3">
  <svg viewBox="0 0 720 332" role="img" aria-label="发送方运输层与IP层多级复用及接收方按IP协议号与端口号两级分用流程">
    <defs>
      <marker id="cn-w5-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="672" height="128" rx="6"/>
    <rect class="fig-box-subtle" x="24" y="184" width="672" height="132" rx="6"/>
    <line class="fig-line" x1="172" y1="87" x2="220" y2="87" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <line class="fig-line" x1="172" y1="137" x2="220" y2="137" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <path class="fig-line" d="M 364 87 L 394 87 L 394 104 L 424 104" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <path class="fig-line" d="M 364 137 L 394 137 L 394 120 L 424 120" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <line class="fig-line" x1="572" y1="112" x2="594" y2="112" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <path class="fig-line-dashed" d="M 640 134 L 640 232" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <line class="fig-line" x1="594" y1="254" x2="572" y2="254" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <path class="fig-line" d="M 424 246 L 394 246 L 394 229 L 364 229" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <path class="fig-line" d="M 424 262 L 394 262 L 394 279 L 364 279" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <line class="fig-line" x1="220" y1="229" x2="172" y2="229" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <line class="fig-line" x1="220" y1="279" x2="172" y2="279" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-3)"/>
    <rect class="fig-box-green" x="38" y="70" width="134" height="34" rx="4"/>
    <rect class="fig-box-blue" x="38" y="120" width="134" height="34" rx="4"/>
    <rect class="fig-box-green" x="220" y="70" width="144" height="34" rx="4"/>
    <rect class="fig-box-blue" x="220" y="120" width="144" height="34" rx="4"/>
    <rect class="fig-box-orange" x="424" y="88" width="148" height="48" rx="4"/>
    <rect class="fig-box" x="594" y="90" width="90" height="44" rx="4"/>
    <rect class="fig-box" x="594" y="232" width="90" height="44" rx="4"/>
    <rect class="fig-box-orange" x="424" y="230" width="148" height="48" rx="4"/>
    <rect class="fig-box-green" x="220" y="212" width="144" height="34" rx="4"/>
    <rect class="fig-box-blue" x="220" y="262" width="144" height="34" rx="4"/>
    <rect class="fig-box-green" x="38" y="212" width="134" height="34" rx="4"/>
    <rect class="fig-box-blue" x="38" y="262" width="134" height="34" rx="4"/>
    <rect class="fig-box" x="590" y="171" width="98" height="20" rx="3"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">发送方多级复用（Multiplexing）与接收方两级分用（Demultiplexing）机制</text>
    <text class="fig-label" x="38" y="60" font-size="12">① 发送方：运输层复用 + IP 复用</text>
    <text class="fig-green" x="105" y="91" text-anchor="middle" font-size="11">DNS 客户 (:49152)</text>
    <text class="fig-blue" x="105" y="141" text-anchor="middle" font-size="11">HTTP 客户 (:49153)</text>
    <text class="fig-green" x="292" y="91" text-anchor="middle" font-size="11">UDP 封装 (目的 53)</text>
    <text class="fig-blue" x="292" y="141" text-anchor="middle" font-size="11">TCP 封装 (目的 80)</text>
    <text class="fig-orange" x="498" y="108" text-anchor="middle" font-size="11">IP 复用封装</text>
    <text class="fig-muted" x="498" y="125" text-anchor="middle" font-size="10">写首部协议字段 17 / 6</text>
    <text class="fig-label" x="639" y="116" text-anchor="middle" font-size="11">链路层发送</text>
    <text class="fig-muted" x="394" y="66" text-anchor="middle" font-size="10">IP 复用</text>
    <text class="fig-orange" x="639" y="185" text-anchor="middle" font-size="10">异构网络传输</text>
    <text class="fig-label" x="38" y="202" font-size="12">② 接收方：先按 IP 协议号分用（17=UDP / 6=TCP），再按套接字端口分用</text>
    <text class="fig-label" x="639" y="258" text-anchor="middle" font-size="11">链路层接收</text>
    <text class="fig-orange" x="498" y="250" text-anchor="middle" font-size="11">IP 协议字段分用</text>
    <text class="fig-muted" x="498" y="267" text-anchor="middle" font-size="10">17 → UDP ｜ 6 → TCP</text>
    <text class="fig-green" x="292" y="233" text-anchor="middle" font-size="11">UDP 按目的端口 53</text>
    <text class="fig-blue" x="292" y="283" text-anchor="middle" font-size="11">TCP 按四元组 (:80)</text>
    <text class="fig-green" x="105" y="233" text-anchor="middle" font-size="11">DNS 服务器 (:53)</text>
    <text class="fig-blue" x="105" y="283" text-anchor="middle" font-size="11">Web 服务器 (:80)</text>
    <text class="fig-muted" x="394" y="308" text-anchor="middle" font-size="10">注：ICMP=1、IGMP=2、TCP=6、UDP=17、OSPF=89 均为 IP 首部「协议」字段常考取值</text>
  </svg>
  <figcaption>图 5-3：发送方运输层与网络层复用封装，以及接收方“IP 协议号（17/6）→ 运输层端口号”两级分用流程</figcaption>
</figure>

以浏览器访问 `www.ptest.com` 为例：

| 报文 | 方向 | 源端口 | 目的端口 | 运输层协议 |
|---|---|---|---|---|
| DNS 查询请求 | PC → DNS 服务器 | 49152（短暂端口） | 53（熟知端口） | UDP |
| DNS 响应 | DNS 服务器 → PC | 53 | 49152 | UDP |
| HTTP 请求 | PC → Web 服务器 | 49152 | 80（熟知端口） | TCP |
| HTTP 响应 | Web 服务器 → PC | 80 | 49152 | TCP |

客户进程从短暂端口号中挑选未被占用的端口（如 49152）标识自己；服务器进程用熟知端口号（53、80）标识自己。服务器回送报文时，把收到的源端口作为目的端口，报文才能分用回原来的客户进程。

<figure class="cpd-figure not-content" id="cn-w5-fig-4">
  <svg viewBox="0 0 720 340" role="img" aria-label="UDP面向报文边界保留与TCP面向字节流对比及UDP的8字节首部与12字节伪首部检验和结构">
    <defs>
      <marker id="cn-w5-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="44" width="328" height="280" rx="6"/>
    <rect class="fig-box-subtle" x="364" y="44" width="332" height="280" rx="6"/>
    <line class="fig-line" x1="126" y1="92" x2="164" y2="92" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-4)"/>
    <line class="fig-line" x1="126" y1="176" x2="164" y2="176" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-4)"/>
    <rect class="fig-box-green" x="38" y="76" width="88" height="32" rx="4"/>
    <rect class="fig-box-purple" x="164" y="76" width="52" height="32" rx="3"/>
    <rect class="fig-box-green" x="216" y="76" width="122" height="32" rx="3"/>
    <rect class="fig-box-blue" x="38" y="160" width="88" height="32" rx="4"/>
    <rect class="fig-box-accent" x="164" y="160" width="82" height="32" rx="3"/>
    <rect class="fig-box-accent" x="254" y="160" width="84" height="32" rx="3"/>
    <rect class="fig-box" x="38" y="232" width="300" height="78" rx="4"/>
    <rect class="fig-box-dashed" x="378" y="74" width="304" height="84" rx="4"/>
    <rect class="fig-box-purple" x="382" y="78" width="296" height="24" rx="2"/>
    <rect class="fig-box-purple" x="382" y="104" width="296" height="24" rx="2"/>
    <rect class="fig-box-purple" x="382" y="130" width="70" height="24" rx="2"/>
    <rect class="fig-box-purple" x="454" y="130" width="76" height="24" rx="2"/>
    <rect class="fig-box-purple" x="532" y="130" width="146" height="24" rx="2"/>
    <rect class="fig-box-green" x="382" y="166" width="146" height="28" rx="2"/>
    <rect class="fig-box-green" x="532" y="166" width="146" height="28" rx="2"/>
    <rect class="fig-box-orange" x="382" y="196" width="146" height="28" rx="2"/>
    <rect class="fig-box-orange" x="532" y="196" width="146" height="28" rx="2"/>
    <rect class="fig-box" x="382" y="226" width="296" height="28" rx="2"/>
    <rect class="fig-box" x="378" y="262" width="304" height="48" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">UDP 与 TCP 传输粒度对比及 UDP 8 字节首部 + 12 字节伪首部结构</text>
    <text class="fig-green" x="38" y="64" font-size="12">① UDP 面向报文（保留应用层边界）</text>
    <text class="fig-green" x="82" y="96" text-anchor="middle" font-size="11">应用报文 M</text>
    <text class="fig-purple" x="190" y="96" text-anchor="middle" font-size="10">8B 首部</text>
    <text class="fig-green" x="277" y="96" text-anchor="middle" font-size="11">完整报文 M（不拆不合）</text>
    <text class="fig-muted" x="38" y="124" font-size="10">一次发送交付一个完整报文；若太长导致 IP 层分片会降低效率</text>
    <text class="fig-blue" x="38" y="148" font-size="12">② TCP 面向字节流（无边界按 MSS 切分）</text>
    <text class="fig-blue" x="82" y="180" text-anchor="middle" font-size="11">连续字节流</text>
    <text class="fig-accent" x="205" y="180" text-anchor="middle" font-size="10">TCP 段 1</text>
    <text class="fig-accent" x="296" y="180" text-anchor="middle" font-size="10">TCP 段 2</text>
    <text class="fig-muted" x="38" y="208" font-size="10">发送缓存按 MSS 与窗口动态切分，接收方无法感知原写操作次数</text>
    <text class="fig-label" x="48" y="250" font-size="11">• UDP 核心考点：无连接、尽最大努力交付、无拥塞控制</text>
    <text class="fig-label" x="48" y="270" font-size="11">• 实时音视频/DNS 选 UDP：恒定速率发送，不因丢包降速</text>
    <text class="fig-muted" x="48" y="290" font-size="10">• 支持一对一、一对多、多对一及多对多交互通信（TCP 仅一对一）</text>
    <text class="fig-purple" x="378" y="64" font-size="12">UDP 首部（8B）与伪首部（12B，每行 32 位）</text>
    <text class="fig-purple" x="530" y="94" text-anchor="middle" font-size="11">伪首部：源 IPv4 地址（32 位 / 4B）</text>
    <text class="fig-purple" x="530" y="120" text-anchor="middle" font-size="11">伪首部：目的 IPv4 地址（32 位 / 4B）</text>
    <text class="fig-purple" x="417" y="146" text-anchor="middle" font-size="10">全 0 (8b)</text>
    <text class="fig-purple" x="492" y="146" text-anchor="middle" font-size="10">协议=17</text>
    <text class="fig-purple" x="605" y="146" text-anchor="middle" font-size="10">UDP 长度（16 位）</text>
    <text class="fig-green" x="455" y="184" text-anchor="middle" font-size="11">源端口（16 位）</text>
    <text class="fig-green" x="605" y="184" text-anchor="middle" font-size="11">目的端口（16 位）</text>
    <text class="fig-orange" x="455" y="214" text-anchor="middle" font-size="11">UDP 总长度（≥8B）</text>
    <text class="fig-orange" x="605" y="214" text-anchor="middle" font-size="11">检验和（16 位）</text>
    <text class="fig-label" x="530" y="244" text-anchor="middle" font-size="11">数据载荷（奇数个字节时仅在计算检验和末尾补 0）</text>
    <text class="fig-orange" x="388" y="280" font-size="9">★ 12B 伪首部仅计算检验和时临时拼接，既不向下传也不上交！</text>
    <text class="fig-muted" x="388" y="298" font-size="9">★ 双重校验：同时核对运输层端口/数据与 IP 层源/目的地址。</text>
  </svg>
  <figcaption>图 5-4：UDP 面向报文与 TCP 面向字节流的封装差异，以及 UDP 8 字节首部和 12 字节伪首部检验和计算范围</figcaption>
</figure>

### 5.4 TCP 报文段首部

TCP 报文段首部固定部分 <strong>20 字节</strong>，加上可变选项最多 <strong>60 字节</strong>。连接管理依赖其中的序号、确认号与标志位。

<figure class="cpd-figure not-content" id="cn-w5-fig-5">
  <svg viewBox="0 0 720 322" role="img" aria-label="TCP报文段20字节固定首部与最长40字节选项位字段布局及单位考点说明">
    <rect class="fig-box-subtle" x="28" y="38" width="664" height="210" rx="6"/>
    <line class="fig-line" x1="636" y1="62" x2="646" y2="62"/>
    <line class="fig-line" x1="646" y1="62" x2="646" y2="206"/>
    <line class="fig-line" x1="636" y1="206" x2="646" y2="206"/>
    <line class="fig-line-dashed" x1="636" y1="208" x2="646" y2="208"/>
    <line class="fig-line-dashed" x1="646" y1="208" x2="646" y2="236"/>
    <line class="fig-line-dashed" x1="636" y1="236" x2="646" y2="236"/>
    <rect class="fig-box-blue" x="52" y="62" width="288" height="28" rx="2"/>
    <rect class="fig-box-blue" x="340" y="62" width="288" height="28" rx="2"/>
    <rect class="fig-box-accent" x="52" y="90" width="576" height="28" rx="2"/>
    <rect class="fig-box-green" x="52" y="118" width="576" height="28" rx="2"/>
    <rect class="fig-box-purple" x="52" y="146" width="72" height="32" rx="2"/>
    <rect class="fig-box" x="124" y="146" width="66" height="32" rx="2"/>
    <rect class="fig-box-orange" x="190" y="146" width="25" height="32"/>
    <rect class="fig-box-orange" x="215" y="146" width="25" height="32"/>
    <rect class="fig-box-orange" x="240" y="146" width="25" height="32"/>
    <rect class="fig-box-orange" x="265" y="146" width="25" height="32"/>
    <rect class="fig-box-red" x="290" y="146" width="25" height="32"/>
    <rect class="fig-box-red" x="315" y="146" width="25" height="32"/>
    <rect class="fig-box-orange" x="340" y="146" width="288" height="32" rx="2"/>
    <rect class="fig-box" x="52" y="178" width="288" height="28" rx="2"/>
    <rect class="fig-box" x="340" y="178" width="288" height="28" rx="2"/>
    <rect class="fig-box-dashed" x="52" y="208" width="466" height="28" rx="2"/>
    <rect class="fig-box" x="518" y="208" width="110" height="28" rx="2"/>
    <rect class="fig-box" x="28" y="258" width="664" height="50" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TCP 报文段首部格式（20B 固定首部 + 0～40B 可变选项，每行 32 位 / 4B）</text>
    <text class="fig-muted" x="56" y="55" font-size="10">0</text>
    <text class="fig-muted" x="124" y="55" text-anchor="middle" font-size="10">4</text>
    <text class="fig-muted" x="190" y="55" text-anchor="middle" font-size="10">10</text>
    <text class="fig-muted" x="340" y="55" text-anchor="middle" font-size="10">16</text>
    <text class="fig-muted" x="624" y="55" text-anchor="end" font-size="10">31 位</text>
    <text class="fig-blue" x="196" y="80" text-anchor="middle" font-size="11">源端口（16 位）</text>
    <text class="fig-blue" x="484" y="80" text-anchor="middle" font-size="11">目的端口（16 位）</text>
    <text class="fig-accent" x="340" y="108" text-anchor="middle" font-size="11">序号 seq（32 位，本段首字节在字节流中的编号）</text>
    <text class="fig-green" x="340" y="136" text-anchor="middle" font-size="11">确认号 ack（32 位，期望收到对方下一段首字节序号，累积确认）</text>
    <text class="fig-purple" x="88" y="166" text-anchor="middle" font-size="10">数据偏移</text>
    <text class="fig-muted" x="157" y="166" text-anchor="middle" font-size="10">保留(6b)</text>
    <text class="fig-orange" x="202.5" y="166" text-anchor="middle" font-size="9">URG</text>
    <text class="fig-orange" x="227.5" y="166" text-anchor="middle" font-size="9">ACK</text>
    <text class="fig-orange" x="252.5" y="166" text-anchor="middle" font-size="9">PSH</text>
    <text class="fig-orange" x="277.5" y="166" text-anchor="middle" font-size="9">RST</text>
    <text class="fig-red" x="302.5" y="166" text-anchor="middle" font-size="9">SYN</text>
    <text class="fig-red" x="327.5" y="166" text-anchor="middle" font-size="9">FIN</text>
    <text class="fig-orange" x="484" y="166" text-anchor="middle" font-size="11">窗口 rwnd（16 位，接收方允许对方发送量）</text>
    <text class="fig-label" x="196" y="196" text-anchor="middle" font-size="11">检验和（16 位，含 12B 伪首部，协议=6）</text>
    <text class="fig-label" x="484" y="196" text-anchor="middle" font-size="11">紧急指针（16 位，仅 URG=1 时有效）</text>
    <text class="fig-muted" x="285" y="226" text-anchor="middle" font-size="11">选项（0～40B：MSS、窗口扩大、时间戳、SACK 选择确认）</text>
    <text class="fig-muted" x="573" y="226" text-anchor="middle" font-size="10">填充（凑 4B 整数倍）</text>
    <text class="fig-label" x="668" y="136" text-anchor="middle" font-size="10">固定</text>
    <text class="fig-label" x="668" y="150" text-anchor="middle" font-size="10">20B</text>
    <text class="fig-muted" x="668" y="226" text-anchor="middle" font-size="10">≤40B</text>
    <text class="fig-purple" x="42" y="278" font-size="11">★ 408 必考单位对比：「数据偏移」以 4 字节为单位（取值 5～15 对应首部 20～60B）；窗口、序号、确认号均以 1 字节为单位！</text>
    <text class="fig-muted" x="42" y="297" font-size="10">★ 标志位速记：SYN=1 建连请求/确认、FIN=1 释放连接（均消耗 1 个序号）；RST=1 严重差错强制重置连接；PSH=1 尽快交付应用进程。</text>
  </svg>
  <figcaption>图 5-5：TCP 报文段首部 32 位对齐结构、6 个控制标志位与“数据偏移 4B 单位 vs 窗口 1B 单位”核心考点</figcaption>
</figure>

- <strong>序号 seq</strong>：本报文段数据部分第一个字节的序号。TCP 是<strong>字节流</strong>，每个字节都编号。
- <strong>确认号 ack</strong>：期望收到对方下一个报文段第一个数据字节的序号，等于<strong>已收到数据的最后一个字节序号加 1</strong>。
- <strong>SYN</strong>：连接请求/连接请求确认；<strong>ACK</strong>：确认；<strong>FIN</strong>：释放连接。

> <strong>注意</strong>：`SYN=1` 的报文段<strong>不能携带数据，但要消耗一个序号</strong>；`FIN=1` 的报文段<strong>即使不携带数据也要消耗一个序号；普通确认报文段可以携带数据，不携带数据则不消耗序号</strong>。

<figure class="cpd-figure not-content" id="cn-w5-fig-6">
  <svg viewBox="0 0 720 356" role="img" aria-label="TCP面向字节流发送缓存滑动窗口P1与P2与P3三指针划分、零窗口持续计时器与糊涂窗口综合征Nagle算法">
    <defs>
      <marker id="cn-w5-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="672" height="158" rx="6"/>
    <rect class="fig-box-subtle" x="24" y="212" width="328" height="130" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="212" width="328" height="130" rx="6"/>
    <line class="fig-line" x1="184" y1="68" x2="520" y2="68" stroke="var(--sl-color-accent)" stroke-width="1.6" marker-start="url(#cn-w5-arr-6)" marker-end="url(#cn-w5-arr-6)"/>
    <line class="fig-line-dashed" x1="184" y1="62" x2="184" y2="156" stroke="var(--sl-color-purple)"/>
    <line class="fig-line-dashed" x1="352" y1="78" x2="352" y2="156" stroke="var(--sl-color-purple)"/>
    <line class="fig-line-dashed" x1="520" y1="62" x2="520" y2="156" stroke="var(--sl-color-purple)"/>
    <line class="fig-line" x1="352" y1="146" x2="520" y2="146" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-start="url(#cn-w5-arr-6)" marker-end="url(#cn-w5-arr-6)"/>
    <line class="fig-divider" x1="38" y1="240" x2="338" y2="240"/>
    <line class="fig-divider" x1="382" y1="240" x2="682" y2="240"/>
    <rect class="fig-box-green" x="40" y="90" width="144" height="38" rx="3"/>
    <rect class="fig-box-orange" x="184" y="90" width="168" height="38" rx="3"/>
    <rect class="fig-box-blue" x="352" y="90" width="168" height="38" rx="3"/>
    <rect class="fig-box" x="520" y="90" width="160" height="38" rx="3"/>
    <rect class="fig-box" x="236" y="56" width="232" height="20" rx="3"/>
    <rect class="fig-box" x="378" y="136" width="116" height="18" rx="3"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TCP 字节流滑动窗口（P1/P2/P3 三指针）、流量控制持续计时器与 Nagle 算法</text>
    <text class="fig-accent" x="352" y="70" text-anchor="middle" font-size="10">发送窗口 swnd = min(rwnd, cwnd) = P3 − P1</text>
    <text class="fig-green" x="112" y="107" text-anchor="middle" font-size="10">已发送并收到确认</text>
    <text class="fig-muted" x="112" y="121" text-anchor="middle" font-size="9">字节 1～200（可移出缓存）</text>
    <text class="fig-orange" x="268" y="107" text-anchor="middle" font-size="10">已发送但尚未收到确认</text>
    <text class="fig-muted" x="268" y="121" text-anchor="middle" font-size="9">字节 201～400（须保留备重传）</text>
    <text class="fig-blue" x="436" y="107" text-anchor="middle" font-size="10">允许发送但尚未发送</text>
    <text class="fig-muted" x="436" y="121" text-anchor="middle" font-size="9">字节 401～600（可用窗口）</text>
    <text class="fig-label" x="600" y="107" text-anchor="middle" font-size="10">窗口外暂不允许发送</text>
    <text class="fig-muted" x="600" y="121" text-anchor="middle" font-size="9">字节 601～…</text>
    <text class="fig-blue" x="436" y="149" text-anchor="middle" font-size="9.5">可用窗口 = P3 − P2</text>
    <text class="fig-purple" x="184" y="172" text-anchor="middle" font-size="10">P1（后沿=201）</text>
    <text class="fig-purple" x="352" y="172" text-anchor="middle" font-size="10">P2（下一待发=401）</text>
    <text class="fig-purple" x="520" y="172" text-anchor="middle" font-size="10">P3（前沿=601）</text>
    <text class="fig-muted" x="360" y="191" text-anchor="middle" font-size="10">后沿 P1 不动（未收到新确认）或前移（收到累积确认）；前沿 P3 通常前移，若对方大幅缩小 rwnd 也可不动（极少后缩）</text>
    <text class="fig-orange" x="38" y="232" font-size="12">① 零窗口死锁与持续计时器（流量控制）</text>
    <text class="fig-label" x="38" y="258" font-size="10.5">• 接收方通告 rwnd = 0 时，发送方暂停发送并启动持续计时器</text>
    <text class="fig-label" x="38" y="278" font-size="10.5">• 到期发送仅含 1 字节数据的「零窗口探测报文段」</text>
    <text class="fig-label" x="38" y="298" font-size="10.5">• 防止接收方恢复非零 rwnd 的通知报文丢失引发双方永久死锁</text>
    <text class="fig-muted" x="38" y="320" font-size="10">★ 规定：即使接收缓存已满（rwnd=0），仍必须接收零窗口探测、确认及紧急数据</text>
    <text class="fig-blue" x="382" y="232" font-size="12">② 糊涂窗口综合征（SWS）与 Nagle 算法</text>
    <text class="fig-label" x="382" y="258" font-size="9.5">• 症状：每次仅发/腾出 1B，却挂 40B 首部，效率极低</text>
    <text class="fig-label" x="382" y="278" font-size="10.5">• 发送方 Nagle：首字节先发，后续缓存至收到 ACK 或攒满 MSS</text>
    <text class="fig-label" x="382" y="298" font-size="10.5">• 接收方 Clark：推迟通告，直到空闲缓存达 min(缓存一半, MSS)</text>
    <text class="fig-muted" x="382" y="320" font-size="9.2">★ 捎带确认延迟通常 ≤ 200ms（RFC ≤ 500ms），兼顾吞吐与时延</text>
  </svg>
  <figcaption>图 5-6：TCP 发送缓存 P1/P2/P3 字节流滑动窗口、零窗口持续计时器防死锁与糊涂窗口综合征治理</figcaption>
</figure>

### 5.5 TCP 超时重传时间的选择

发送方每发送一个报文段就启动重传计时器，若在<strong>超时重传时间 RTO</strong> 内未收到确认，就重传该报文段。RTO 是 TCP 最复杂的问题之一。

设报文段从发出到收到对应确认的时间为 <strong>往返时间样本 RTT</strong>。

- RTO 设得<strong>比 RTT 小</strong>：会引起不必要的重传，增大网络负荷。
- RTO 设得<strong>远大于 RTT</strong>：重传推迟太久，网络空闲时间增大，传输效率降低。
- 结论：<strong>RTO 应略大于 RTT</strong>。

但互联网路径复杂：同一连接的不同报文段可能经过不同速率、不同路由的网络，各次 RTT 样本差别很大，<strong>不能直接用某一次的 RTT 样本计算 RTO</strong>，而要用<strong>加权平均往返时间</strong>。

第一个样本时，$\text{RTT\_S}$ 直接取该样本值；以后每测到一个新样本：

$$
\text{RTT\_S\_new} = (1-\alpha)\cdot\text{RTT\_S\_old} + \alpha\cdot\text{RTT\_sample}
$$

其中 $0 \le \alpha < 1$，RFC 6298 建议 $\alpha = 1/8$。α 越接近 0，新样本影响越小；越接近 1，新样本影响越大。加权平均使 RTT 估计更平滑。

<strong>RTT 偏差的加权平均</strong> $\text{RTT\_D}$：第一个样本时取该样本值的一半；以后按下式更新：

$$
\text{RTT\_D\_new} = (1-\beta)\cdot\text{RTT\_D\_old} + \beta\cdot\left|\text{RTT\_S} - \text{RTT\_sample}\right|
$$

其中 $0 \le \beta < 1$，RFC 6298 建议 $\beta = 1/4$。

<strong>超时重传时间</strong>由加权平均往返时间与偏差共同决定：

$$
\text{RTO} = \text{RTT\_S} + 4\cdot\text{RTT\_D}
$$

<figure class="cpd-figure not-content" id="cn-w5-fig-7">
  <svg viewBox="0 0 720 350" role="img" aria-label="TCP往返时间RTT测量歧义、Karn修正算法指数退避与SACK选择确认选项结构">
    <defs>
      <marker id="cn-w5-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="212" height="222" rx="6"/>
    <rect class="fig-box-subtle" x="248" y="42" width="218" height="222" rx="6"/>
    <rect class="fig-box-subtle" x="478" y="42" width="218" height="222" rx="6"/>
    <line class="fig-line" x1="82" y1="74" x2="82" y2="236"/>
    <line class="fig-line" x1="206" y1="74" x2="206" y2="236"/>
    <line class="fig-line" x1="82" y1="90" x2="206" y2="122" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="206" y1="130" x2="82" y2="162" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line-dashed" x1="56" y1="90" x2="82" y2="90"/>
    <line class="fig-line-dashed" x1="56" y1="162" x2="82" y2="162"/>
    <line class="fig-line" x1="60" y1="90" x2="60" y2="162" stroke="var(--sl-color-accent)" marker-start="url(#cn-w5-arr-7)" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line-dashed" x1="56" y1="206" x2="82" y2="206"/>
    <line class="fig-line" x1="42" y1="90" x2="42" y2="206" stroke="var(--sl-color-orange)" marker-start="url(#cn-w5-arr-7)" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="312" y1="74" x2="312" y2="236"/>
    <line class="fig-line" x1="440" y1="74" x2="440" y2="236"/>
    <line class="fig-line-dashed" x1="312" y1="88" x2="394" y2="108" stroke="var(--sl-color-red)" stroke-width="1.5"/>
    <line class="fig-line" x1="396" y1="103" x2="406" y2="113" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <line class="fig-line" x1="406" y1="103" x2="396" y2="113" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <line class="fig-line" x1="312" y1="142" x2="440" y2="170" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="440" y1="178" x2="312" y2="206" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="274" y1="88" x2="274" y2="206" stroke="var(--sl-color-red)" marker-start="url(#cn-w5-arr-7)" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="294" y1="142" x2="294" y2="206" stroke="var(--sl-color-green)" marker-start="url(#cn-w5-arr-7)" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="542" y1="74" x2="542" y2="236"/>
    <line class="fig-line" x1="670" y1="74" x2="670" y2="236"/>
    <line class="fig-line" x1="542" y1="88" x2="670" y2="120" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="670" y1="128" x2="542" y2="184" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="542" y1="154" x2="670" y2="196" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-end="url(#cn-w5-arr-7)"/>
    <line class="fig-line" x1="518" y1="154" x2="518" y2="184" stroke="var(--sl-color-red)" marker-start="url(#cn-w5-arr-7)" marker-end="url(#cn-w5-arr-7)"/>
    <rect class="fig-box" x="110" y="92" width="68" height="16" rx="3"/>
    <rect class="fig-box" x="110" y="140" width="68" height="16" rx="3"/>
    <rect class="fig-box" x="342" y="142" width="68" height="16" rx="3"/>
    <rect class="fig-box" x="572" y="138" width="76" height="16" rx="3"/>
    <rect class="fig-box" x="24" y="274" width="672" height="62" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">RTT 样本测量二义性、Karn 指数退避算法与 SACK 选择确认机制</text>
    <text class="fig-label" x="130" y="60" text-anchor="middle" font-size="11">① 正常传输（无重传）</text>
    <text class="fig-muted" x="82" y="70" text-anchor="middle" font-size="10">发 A</text>
    <text class="fig-muted" x="206" y="70" text-anchor="middle" font-size="10">收 B</text>
    <text class="fig-blue" x="144" y="104" text-anchor="middle" font-size="9.5">数据段 seq</text>
    <text class="fig-green" x="144" y="152" text-anchor="middle" font-size="9.5">确认段 ack</text>
    <text class="fig-accent" x="74" y="130" text-anchor="end" font-size="9.5">RTT</text>
    <text class="fig-orange" x="38" y="152" text-anchor="middle" font-size="9">RTO</text>
    <text class="fig-muted" x="130" y="254" text-anchor="middle" font-size="9.5">RTO = RTT_S + 4·RTT_D</text>
    <text class="fig-label" x="357" y="60" text-anchor="middle" font-size="11">② 歧义一：原段丢失重传</text>
    <text class="fig-muted" x="312" y="70" text-anchor="middle" font-size="10">发 A</text>
    <text class="fig-muted" x="440" y="70" text-anchor="middle" font-size="10">收 B</text>
    <text class="fig-red" x="362" y="88" text-anchor="middle" font-size="9.5">原段丢失</text>
    <text class="fig-blue" x="376" y="154" text-anchor="middle" font-size="9.5">超时重传</text>
    <text class="fig-green" x="386" y="204" text-anchor="middle" font-size="9.5">对重传的 ACK</text>
    <text class="fig-red" x="268" y="148" text-anchor="end" font-size="9">误判</text>
    <text class="fig-red" x="268" y="160" text-anchor="end" font-size="9">偏大</text>
    <text class="fig-green" x="308" y="178" text-anchor="end" font-size="9">真值</text>
    <text class="fig-muted" x="357" y="254" text-anchor="middle" font-size="9.5">误算为原段确认 → RTO 虚高</text>
    <text class="fig-label" x="587" y="60" text-anchor="middle" font-size="11">③ 歧义二：原 ACK 迟到</text>
    <text class="fig-muted" x="542" y="70" text-anchor="middle" font-size="10">发 A</text>
    <text class="fig-muted" x="670" y="70" text-anchor="middle" font-size="10">收 B</text>
    <text class="fig-blue" x="614" y="96" text-anchor="middle" font-size="9.5">原数据段</text>
    <text class="fig-green" x="610" y="150" text-anchor="middle" font-size="9.5">迟到的原 ACK</text>
    <text class="fig-blue" x="622" y="198" text-anchor="middle" font-size="9.5">超时重传</text>
    <text class="fig-red" x="512" y="168" text-anchor="end" font-size="9">误判</text>
    <text class="fig-red" x="512" y="180" text-anchor="end" font-size="9">偏小</text>
    <text class="fig-muted" x="587" y="254" text-anchor="middle" font-size="9.5">误算为重传确认 → RTO 偏小</text>
    <text class="fig-orange" x="38" y="294" font-size="11">• Karn 修正算法：凡发生重传的报文段一律不采样 RTT；每重传一次令新 RTO = 2 × 旧 RTO（指数退避防时延突增死循环）。</text>
    <text class="fig-label" x="38" y="314" font-size="9.5">• 选择确认 SACK（选项）：接收方缓存失序块并在选项中报告左/右边界（每边界 4B，40B 选项最多 4 块）。</text>
    <text class="fig-muted" x="38" y="329" font-size="10">注：即使启用 SACK，TCP 首部中的 32 位「确认号 ack」含义不变，仍然表示累积确认（按序到达最高字节序号 + 1）。</text>
  </svg>
  <figcaption>图 5-7：TCP 超时重传中的两种 RTT 测量歧义、Karn 指数退避算法与 SACK 选择确认选项</figcaption>
</figure>

出现超时重传后，发送方无法判断收到的确认是针对原报文段还是重传报文段：

- 若把重传的确认误当成原报文段的确认，算出的 RTT 与 RTO <strong>偏大</strong>，传输效率降低。
- 若把迟到的确认误当成重传的确认，算出的 RTT 与 RTO <strong>偏小</strong>，引起不必要重传、增大网络负荷。

<strong>Karn 算法</strong>：只要报文段重传了，就<strong>不采用该报文段的 RTT 样本</strong>，也不重新计算 RTO。

> <strong>注意</strong>：Karn 算法会带来新问题——若时延突然大幅增大并长期保持，旧 RTO 内收不到确认而重传，又不采样，RTO 永不更新，会反复重传。解决方法是<strong>报文段每重传一次，就把 RTO 增大一些</strong>，典型做法是<strong>新 RTO 取旧 RTO 的两倍</strong>。

<figure class="cpd-figure not-content" id="cn-w5-fig-8">
  <svg viewBox="0 0 720 356" role="img" aria-label="TCP拥塞控制慢开始、拥塞避免、超时重传与3个重复ACK快重传快恢复的cwnd完整演化曲线">
    <defs>
      <marker id="cn-w5-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="40" width="672" height="232" rx="6"/>
    <line class="fig-divider" x1="68" y1="124" x2="672" y2="124"/>
    <line class="fig-divider" x1="68" y1="156" x2="672" y2="156"/>
    <line class="fig-divider" x1="68" y1="188" x2="672" y2="188"/>
    <line class="fig-divider" x1="68" y1="220" x2="672" y2="220"/>
    <line class="fig-line" x1="68" y1="252" x2="676" y2="252" marker-end="url(#cn-w5-arr-8)"/>
    <line class="fig-line" x1="68" y1="252" x2="68" y2="50" marker-end="url(#cn-w5-arr-8)"/>
    <line class="fig-line-dashed" x1="68" y1="124" x2="380" y2="124" stroke="var(--sl-color-purple)" stroke-width="1.6"/>
    <line class="fig-line-dashed" x1="380" y1="156" x2="588" y2="156" stroke="var(--sl-color-purple)" stroke-width="1.6"/>
    <line class="fig-line-dashed" x1="588" y1="188" x2="666" y2="188" stroke="var(--sl-color-purple)" stroke-width="1.6"/>
    <polyline class="fig-line" points="68,244 94,236 120,220 146,188 172,124" stroke="var(--sl-color-green)" stroke-width="2.2"/>
    <polyline class="fig-line" points="172,124 380,60" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <line class="fig-line-dashed" x1="380" y1="60" x2="380" y2="244" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <polyline class="fig-line" points="380,244 406,236 432,220 458,188 484,156" stroke="var(--sl-color-green)" stroke-width="2.2"/>
    <polyline class="fig-line" points="484,156 588,124" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <line class="fig-line-dashed" x1="588" y1="124" x2="588" y2="188" stroke="var(--sl-color-orange)" stroke-width="1.8"/>
    <polyline class="fig-line" points="588,188 666,164" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <circle class="fig-dot" cx="68" cy="244" r="3"/>
    <circle class="fig-dot" cx="94" cy="236" r="3"/>
    <circle class="fig-dot" cx="120" cy="220" r="3"/>
    <circle class="fig-dot" cx="146" cy="188" r="3"/>
    <circle class="fig-dot" cx="172" cy="124" r="3.5"/>
    <circle class="fig-box-red" cx="380" cy="60" r="4.5"/>
    <circle class="fig-dot" cx="380" cy="244" r="3.5"/>
    <circle class="fig-dot" cx="458" cy="188" r="3"/>
    <circle class="fig-dot" cx="484" cy="156" r="3.5"/>
    <circle class="fig-box-orange" cx="588" cy="124" r="4.5"/>
    <circle class="fig-dot" cx="588" cy="188" r="3.5"/>
    <circle class="fig-dot" cx="666" cy="164" r="3"/>
    <rect class="fig-box" x="78" y="103" width="84" height="17" rx="3"/>
    <rect class="fig-box" x="388" y="135" width="86" height="17" rx="3"/>
    <rect class="fig-box" x="594" y="193" width="76" height="17" rx="3"/>
    <rect class="fig-box" x="198" y="72" width="112" height="18" rx="3"/>
    <rect class="fig-box" x="304" y="46" width="144" height="18" rx="3"/>
    <rect class="fig-box" x="518" y="98" width="154" height="18" rx="3"/>
    <rect class="fig-box" x="24" y="282" width="672" height="60" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TCP 拥塞控制完整演化曲线：慢开始、拥塞避免、超时重传 vs 快重传与快恢复</text>
    <text class="fig-label" x="44" y="54" font-size="10">cwnd</text>
    <text class="fig-muted" x="60" y="64" text-anchor="end" font-size="9.5">24</text>
    <text class="fig-muted" x="60" y="128" text-anchor="end" font-size="9.5">16</text>
    <text class="fig-muted" x="60" y="160" text-anchor="end" font-size="9.5">12</text>
    <text class="fig-muted" x="60" y="192" text-anchor="end" font-size="9.5">8</text>
    <text class="fig-muted" x="60" y="224" text-anchor="end" font-size="9.5">4</text>
    <text class="fig-muted" x="60" y="247" text-anchor="end" font-size="9.5">1</text>
    <text class="fig-muted" x="68" y="266" text-anchor="middle" font-size="9.5">0</text>
    <text class="fig-muted" x="172" y="266" text-anchor="middle" font-size="9.5">4</text>
    <text class="fig-muted" x="380" y="266" text-anchor="middle" font-size="9.5">12</text>
    <text class="fig-muted" x="484" y="266" text-anchor="middle" font-size="9.5">16</text>
    <text class="fig-muted" x="588" y="266" text-anchor="middle" font-size="9.5">20</text>
    <text class="fig-label" x="652" y="266" text-anchor="middle" font-size="9.5">传输轮次 RTT</text>
    <text class="fig-purple" x="120" y="115" text-anchor="middle" font-size="9.5">ssthresh₁=16</text>
    <text class="fig-purple" x="431" y="147" text-anchor="middle" font-size="9.5">ssthresh₂=12</text>
    <text class="fig-purple" x="632" y="205" text-anchor="middle" font-size="9.5">ssthresh₃=8</text>
    <text class="fig-green" x="124" y="168" text-anchor="middle" font-size="10">慢开始(×2)</text>
    <text class="fig-blue" x="254" y="85" text-anchor="middle" font-size="10">拥塞避免 (+1/RTT)</text>
    <text class="fig-red" x="376" y="59" text-anchor="middle" font-size="9.5">① 超时：cwnd→1</text>
    <text class="fig-green" x="424" y="206" text-anchor="middle" font-size="9.5">慢开始(封顶12)</text>
    <text class="fig-orange" x="595" y="111" text-anchor="middle" font-size="9.5">② 3个重复ACK：快恢复</text>
    <text class="fig-blue" x="634" y="156" text-anchor="middle" font-size="9.5">拥塞避免</text>
    <text class="fig-red" x="38" y="301" font-size="10.5">• 超时重传（重度拥塞）：ssthresh = cwnd / 2，cwnd 骤降为 1 MSS，重新执行「慢开始」（每轮指数倍增，严禁越过新 ssthresh）。</text>
    <text class="fig-orange" x="38" y="320" font-size="9.2">• 收到 3 个重复 ACK（轻度丢包）：快重传立即补发；快恢复令 ssthresh = cwnd/2 且 cwnd = ssthresh，直接「拥塞避免」（+1）。</text>
    <text class="fig-muted" x="38" y="336" font-size="10">★ 408 计算必背：实际发送窗口上限 swnd = min(接收窗口 rwnd, 拥塞窗口 cwnd)，同时受接收端缓存与网络拥塞状态双重制约！</text>
  </svg>
  <figcaption>图 5-8：TCP 拥塞控制四大算法（慢开始、拥塞避免、快重传、快恢复）与超时 vs 三重复 ACK 状态跃迁对比</figcaption>
</figure>

### 5.6 TCP 连接的建立：三次握手

TCP 面向连接，基于运输连接传送报文段。运输连接管理分三个阶段：<strong>连接建立（三报文握手）→ 数据传送 → 连接释放（四报文挥手）</strong>。

建立连接要解决三个问题：

1. 双方能<strong>确知对方的存在</strong>。
2. 双方能<strong>协商参数</strong>：最大窗口值、是否使用窗口扩大选项和时间戳选项、服务质量等。
3. 双方能对运输实体资源进行<strong>分配</strong>：缓存大小、连接表中的项目等。

服务器和客户都要先创建<strong>传输控制块 TCB</strong>，记录连接表、指向发送和接收缓存的指针、指向重传队列的指针、当前发送和接收序号等。

<strong>过程</strong>：

1. 服务器创建 TCB，进入<strong>监听状态 LISTEN</strong>，被动等待客户请求，称为<strong>被动打开连接</strong>。
2. 客户创建 TCB，发送连接请求报文段：`SYN=1，seq=x`，进入 <strong>SYN-SENT（同步已发送）</strong>。这是<strong>主动打开连接</strong>。
3. 服务器同意则发送连接请求确认报文段：`SYN=1，ACK=1，seq=y，ack=x+1`，进入 <strong>SYN-RCVD（同步已接收）</strong>。`ack=x+1` 是对客户初始序号 x 的确认。
4. 客户收到后发送普通确认报文段：`ACK=1，seq=x+1，ack=y+1`，进入 <strong>ESTABLISHED（连接已建立）</strong>。
5. 服务器收到该确认后也进入 <strong>ESTABLISHED</strong>，双方可以开始可靠数据传输。

<figure class="cpd-figure not-content" id="cn-w5-fig-9">
  <svg viewBox="0 0 720 356" role="img" aria-label="TCP三次握手连接建立过程、两端状态变迁与序号确认号消耗规则">
    <defs>
      <marker id="cn-w5-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="40" width="672" height="248" rx="6"/>
    <line class="fig-line" x1="184" y1="68" x2="184" y2="274" stroke-width="1.8"/>
    <line class="fig-line" x1="536" y1="68" x2="536" y2="274" stroke-width="1.8"/>
    <line class="fig-line" x1="184" y1="96" x2="536" y2="128" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w5-arr-9)"/>
    <line class="fig-line" x1="536" y1="146" x2="184" y2="178" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#cn-w5-arr-9)"/>
    <line class="fig-line" x1="184" y1="196" x2="536" y2="228" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w5-arr-9)"/>
    <line class="fig-line-dashed" x1="184" y1="256" x2="536" y2="256" stroke="var(--sl-color-green)" stroke-width="1.6" marker-start="url(#cn-w5-arr-9)" marker-end="url(#cn-w5-arr-9)"/>
    <rect class="fig-box" x="38" y="64" width="130" height="22" rx="4"/>
    <rect class="fig-box-blue" x="38" y="92" width="130" height="24" rx="4"/>
    <rect class="fig-box-green" x="38" y="190" width="130" height="26" rx="4"/>
    <rect class="fig-box" x="552" y="56" width="130" height="20" rx="4"/>
    <rect class="fig-box-orange" x="552" y="80" width="130" height="24" rx="4"/>
    <rect class="fig-box-purple" x="552" y="132" width="130" height="24" rx="4"/>
    <rect class="fig-box-green" x="552" y="222" width="130" height="26" rx="4"/>
    <rect class="fig-box" x="242" y="90" width="236" height="20" rx="3"/>
    <rect class="fig-box" x="220" y="144" width="280" height="20" rx="3"/>
    <rect class="fig-box" x="234" y="196" width="252" height="20" rx="3"/>
    <rect class="fig-box" x="236" y="246" width="248" height="20" rx="3"/>
    <rect class="fig-box" x="24" y="298" width="672" height="44" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TCP 连接建立：三次握手（三报文握手）状态跃迁与序号规则</text>
    <text class="fig-label" x="184" y="58" text-anchor="middle" font-size="12">TCP 客户（主动打开）</text>
    <text class="fig-label" x="536" y="52" text-anchor="middle" font-size="12">TCP 服务器（被动打开）</text>
    <text class="fig-muted" x="103" y="79" text-anchor="middle" font-size="10">CLOSED → 创建 TCB</text>
    <text class="fig-blue" x="103" y="108" text-anchor="middle" font-size="10.5">SYN-SENT（同步已发）</text>
    <text class="fig-green" x="103" y="207" text-anchor="middle" font-size="10.5">ESTABLISHED</text>
    <text class="fig-muted" x="617" y="70" text-anchor="middle" font-size="10">CLOSED → 创建 TCB</text>
    <text class="fig-orange" x="617" y="96" text-anchor="middle" font-size="10.5">LISTEN（监听）</text>
    <text class="fig-purple" x="617" y="148" text-anchor="middle" font-size="10.5">SYN-RCVD（同步已收）</text>
    <text class="fig-green" x="617" y="239" text-anchor="middle" font-size="10.5">ESTABLISHED</text>
    <text class="fig-blue" x="360" y="104" text-anchor="middle" font-size="10.5">① SYN=1, seq=x（禁携数据，消耗 1 序号）</text>
    <text class="fig-purple" x="360" y="158" text-anchor="middle" font-size="10.5">② SYN=1, ACK=1, seq=y, ack=x+1（消耗 1 序号）</text>
    <text class="fig-green" x="360" y="210" text-anchor="middle" font-size="10.5">③ ACK=1, seq=x+1, ack=y+1（普通确认段）</text>
    <text class="fig-green" x="360" y="260" text-anchor="middle" font-size="10.5">双向可靠数据传输（首段数据 seq = x+1）</text>
    <text class="fig-label" x="38" y="316" font-size="11">• 408 序号铁律：① 与 ② 因含 SYN=1 均不能携带数据但各占 1 个序号；③ 为普通确认段，可携带数据，若不携数据则不消耗序号！</text>
    <text class="fig-muted" x="38" y="334" font-size="10">• 初始序号 x、y 由通信双方各自独立随机产生（防旧连接滞留报文混淆及序号猜测攻击），二者数值没有任何函数关系。</text>
  </svg>
  <figcaption>图 5-9：TCP 三报文握手建连时序、两端 TCB 状态变迁与 SYN/ACK 序号消耗规则</figcaption>
</figure>

> <strong>★考点</strong>：为什么不能简化为两报文握手？防止<strong>失效的连接请求报文段</strong>在网络中滞留后突然到达服务器。若只有两次握手，服务器收到该失效请求就发确认并进入连接已建立状态，而客户并不理会，服务器却一直空等客户数据，白白浪费资源。三次握手让服务器在收到客户的第三个确认后才建立连接，避免这种错误。

<figure class="cpd-figure not-content" id="cn-w5-fig-10">
  <svg viewBox="0 0 720 304" role="img" aria-label="为什么TCP不能采用两次握手：滞留失效SYN连接请求在两次握手下的资源浪费与三次握手拦截对比">
    <defs>
      <marker id="cn-w5-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="328" height="248" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="42" width="328" height="248" rx="6"/>
    <line class="fig-line" x1="88" y1="78" x2="88" y2="246"/>
    <line class="fig-line" x1="284" y1="78" x2="284" y2="246"/>
    <path class="fig-line-dashed" d="M 88 90 C 168 96, 152 152, 284 162" stroke="var(--sl-color-orange)" stroke-width="1.7" marker-end="url(#cn-w5-arr-10)"/>
    <line class="fig-line" x1="88" y1="114" x2="284" y2="128" stroke="var(--sl-color-green)" stroke-width="1.4" marker-end="url(#cn-w5-arr-10)"/>
    <line class="fig-line" x1="284" y1="172" x2="88" y2="196" stroke="var(--sl-color-red)" stroke-width="1.6" marker-end="url(#cn-w5-arr-10)"/>
    <line class="fig-line" x1="432" y1="78" x2="432" y2="246"/>
    <line class="fig-line" x1="628" y1="78" x2="628" y2="246"/>
    <path class="fig-line-dashed" d="M 432 90 C 512 96, 496 152, 628 162" stroke="var(--sl-color-orange)" stroke-width="1.7" marker-end="url(#cn-w5-arr-10)"/>
    <line class="fig-line" x1="628" y1="172" x2="432" y2="196" stroke="var(--sl-color-purple)" stroke-width="1.6" marker-end="url(#cn-w5-arr-10)"/>
    <line class="fig-line-dashed" x1="432" y1="206" x2="628" y2="226" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-end="url(#cn-w5-arr-10)"/>
    <rect class="fig-box-red" x="218" y="204" width="122" height="34" rx="4"/>
    <rect class="fig-box-green" x="562" y="218" width="122" height="30" rx="4"/>
    <rect class="fig-box" x="128" y="138" width="116" height="17" rx="3"/>
    <rect class="fig-box" x="472" y="138" width="116" height="17" rx="3"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">为什么不能简化为两报文握手：滞留失效 SYN 报文段的异常处理对比</text>
    <text class="fig-red" x="188" y="60" text-anchor="middle" font-size="11.5">① 假设仅两次握手（产生幽灵连接）</text>
    <text class="fig-muted" x="88" y="73" text-anchor="middle" font-size="10">客户 A</text>
    <text class="fig-muted" x="284" y="73" text-anchor="middle" font-size="10">服务器 B</text>
    <text class="fig-green" x="186" y="116" text-anchor="middle" font-size="9">重传建连→传完释放(CLOSED)</text>
    <text class="fig-orange" x="186" y="150" text-anchor="middle" font-size="9.5">旧滞留 SYN₁ 姗姗来迟</text>
    <text class="fig-red" x="186" y="182" text-anchor="middle" font-size="9.5">发确认并直接建连！</text>
    <text class="fig-muted" x="78" y="204" text-anchor="end" font-size="9.5">A 已关闭</text>
    <text class="fig-muted" x="78" y="217" text-anchor="end" font-size="9.5">直接丢弃</text>
    <text class="fig-red" x="279" y="219" text-anchor="middle" font-size="9.5">误入 ESTABLISHED</text>
    <text class="fig-muted" x="279" y="232" text-anchor="middle" font-size="8.5">白白分配缓存死等数据</text>
    <text class="fig-label" x="188" y="274" text-anchor="middle" font-size="10">后果：服务器单方面建立无效连接，浪费资源</text>
    <text class="fig-green" x="532" y="60" text-anchor="middle" font-size="11.5">② 标准三次握手（成功拦截失效请求）</text>
    <text class="fig-muted" x="432" y="73" text-anchor="middle" font-size="10">客户 A</text>
    <text class="fig-muted" x="628" y="73" text-anchor="middle" font-size="10">服务器 B</text>
    <text class="fig-orange" x="530" y="150" text-anchor="middle" font-size="9.5">旧滞留 SYN₁ 姗姗来迟</text>
    <text class="fig-purple" x="530" y="182" text-anchor="middle" font-size="9.5">回 SYN+ACK（停在 SYN-RCVD）</text>
    <text class="fig-blue" x="524" y="212" text-anchor="middle" font-size="9.5">不发第 ③ 握（或发 RST 复位）</text>
    <text class="fig-green" x="623" y="237" text-anchor="middle" font-size="9.5">收不到 ③ 握，销毁释放</text>
    <text class="fig-label" x="532" y="274" text-anchor="middle" font-size="10">结论：收齐第 ③ 次确认才进入 ESTABLISHED</text>
  </svg>
  <figcaption>图 5-10：两报文握手因网络滞留失效 SYN 请求导致服务器单侧资源死等，与三报文握手防误建连机制对比</figcaption>
</figure>

2011 年统考真题：主机甲发 `SYN=1，seq=11220`，主机乙回复的连接请求确认报文段中 `SYN=1，ACK=1，ack=11221`。<strong>确认号是 11221</strong>，而乙的 `seq` 是自己随意选择的初始序号，与甲的序号无关；正确选项中 `seq` 与 `ack` 恰好同为 11221 只是迷惑项。

### 5.7 TCP 连接的释放：四次挥手

数据传输结束后，通信双方都可释放连接。以客户主动关闭为例：

1. 客户发送连接释放报文段：`FIN=1，ACK=1，seq=u，ack=v`，进入 <strong>FIN-WAIT-1（终止等待 1）</strong>。`u` 等于客户之前已传送过的数据最后一个字节序号加 1；`v` 等于客户之前已收到数据最后一个字节序号加 1。
2. 服务器发送普通确认：`ACK=1，seq=v，ack=u+1`，进入 <strong>CLOSE-WAIT（关闭等待）</strong>，并通知高层客户要断开连接。此时<strong>客户→服务器方向已释放</strong>，服务器若还有数据仍可发送，连接处于<strong>半关闭状态</strong>。
3. 客户收到确认后进入 <strong>FIN-WAIT-2（终止等待 2）</strong>，等待服务器的释放报文段。
4. 服务器没有数据要发时，发送连接释放报文段：`FIN=1，ACK=1，seq=w，ack=u+1`，进入 <strong>LAST-ACK（最后确认）</strong>。半关闭期间服务器可能又发送过数据，所以序号为 w。
5. 客户必须对该报文段发送普通确认：`ACK=1，seq=u+1，ack=w+1`，进入 <strong>TIME-WAIT（时间等待）</strong>。
6. 服务器收到后进入 <strong>CLOSED</strong>；客户经过 <strong>2MSL</strong> 后才进入 <strong>CLOSED</strong>。

<figure class="cpd-figure not-content" id="cn-w5-fig-11">
  <svg viewBox="0 0 720 404" role="img" aria-label="TCP四次挥手连接释放过程、半关闭状态数据传输与TIME-WAIT等待2MSL状态变化">
    <defs>
      <marker id="cn-w5-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="40" width="672" height="304" rx="6"/>
    <rect class="fig-box-dashed" x="196" y="162" width="328" height="50" rx="4"/>
    <line class="fig-line" x1="184" y1="66" x2="184" y2="334" stroke-width="1.8"/>
    <line class="fig-line" x1="536" y1="66" x2="536" y2="334" stroke-width="1.8"/>
    <line class="fig-line" x1="184" y1="90" x2="536" y2="116" stroke="var(--sl-color-red)" stroke-width="1.8" marker-end="url(#cn-w5-arr-11)"/>
    <line class="fig-line" x1="536" y1="128" x2="184" y2="154" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w5-arr-11)"/>
    <line class="fig-line-dashed" x1="536" y1="184" x2="184" y2="202" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-end="url(#cn-w5-arr-11)"/>
    <line class="fig-line" x1="536" y1="224" x2="184" y2="250" stroke="var(--sl-color-red)" stroke-width="1.8" marker-end="url(#cn-w5-arr-11)"/>
    <line class="fig-line" x1="184" y1="264" x2="536" y2="290" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w5-arr-11)"/>
    <line class="fig-line-dashed" x1="154" y1="264" x2="184" y2="264" stroke="var(--sl-color-orange)"/>
    <line class="fig-line-dashed" x1="154" y1="322" x2="184" y2="322" stroke="var(--sl-color-orange)"/>
    <line class="fig-line" x1="160" y1="264" x2="160" y2="322" stroke="var(--sl-color-orange)" stroke-width="1.5" marker-start="url(#cn-w5-arr-11)" marker-end="url(#cn-w5-arr-11)"/>
    <rect class="fig-box-green" x="38" y="62" width="130" height="22" rx="4"/>
    <rect class="fig-box-blue" x="38" y="92" width="130" height="22" rx="4"/>
    <rect class="fig-box-purple" x="38" y="150" width="130" height="22" rx="4"/>
    <rect class="fig-box-orange" x="38" y="256" width="110" height="24" rx="4"/>
    <rect class="fig-box" x="38" y="312" width="110" height="22" rx="4"/>
    <rect class="fig-box-green" x="552" y="62" width="130" height="22" rx="4"/>
    <rect class="fig-box-purple" x="552" y="128" width="130" height="24" rx="4"/>
    <rect class="fig-box-blue" x="552" y="222" width="130" height="24" rx="4"/>
    <rect class="fig-box" x="552" y="284" width="130" height="22" rx="4"/>
    <rect class="fig-box" x="234" y="84" width="252" height="19" rx="3"/>
    <rect class="fig-box" x="242" y="126" width="236" height="19" rx="3"/>
    <rect class="fig-box" x="234" y="218" width="252" height="19" rx="3"/>
    <rect class="fig-box" x="238" y="260" width="244" height="19" rx="3"/>
    <rect class="fig-box" x="24" y="352" width="672" height="40" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TCP 连接释放：四次挥手（四报文挥手）、半关闭状态与 2MSL 计时</text>
    <text class="fig-label" x="184" y="56" text-anchor="middle" font-size="12">TCP 客户（主动关闭）</text>
    <text class="fig-label" x="536" y="56" text-anchor="middle" font-size="12">TCP 服务器（被动关闭）</text>
    <text class="fig-green" x="103" y="77" text-anchor="middle" font-size="10">ESTABLISHED</text>
    <text class="fig-blue" x="103" y="107" text-anchor="middle" font-size="10">FIN-WAIT-1</text>
    <text class="fig-purple" x="103" y="165" text-anchor="middle" font-size="10">FIN-WAIT-2</text>
    <text class="fig-orange" x="93" y="272" text-anchor="middle" font-size="10">TIME-WAIT</text>
    <text class="fig-muted" x="93" y="327" text-anchor="middle" font-size="10">CLOSED</text>
    <text class="fig-orange" x="172" y="297" text-anchor="middle" font-size="9">2MSL</text>
    <text class="fig-green" x="617" y="77" text-anchor="middle" font-size="10">ESTABLISHED</text>
    <text class="fig-purple" x="617" y="144" text-anchor="middle" font-size="10">CLOSE-WAIT（半关闭）</text>
    <text class="fig-blue" x="617" y="238" text-anchor="middle" font-size="10">LAST-ACK</text>
    <text class="fig-muted" x="617" y="299" text-anchor="middle" font-size="10">CLOSED（先关闭！）</text>
    <text class="fig-red" x="360" y="98" text-anchor="middle" font-size="10">① FIN=1, ACK=1, seq=u, ack=v（消耗 1 序号）</text>
    <text class="fig-green" x="360" y="140" text-anchor="middle" font-size="10">② ACK=1, seq=v, ack=u+1（普通确认）</text>
    <text class="fig-blue" x="360" y="178" text-anchor="middle" font-size="9.5">半关闭区间：A→B 已关，B→A 仍可发余留数据（seq=v…w−1）</text>
    <text class="fig-muted" x="360" y="206" text-anchor="middle" font-size="9">因 A 不再发新数据，故 B 后续报文的确认号始终保持 ack=u+1</text>
    <text class="fig-red" x="360" y="232" text-anchor="middle" font-size="10">③ FIN=1, ACK=1, seq=w, ack=u+1（消耗 1 序号）</text>
    <text class="fig-green" x="360" y="274" text-anchor="middle" font-size="10">④ ACK=1, seq=u+1, ack=w+1</text>
    <text class="fig-label" x="38" y="369" font-size="10.5">• 408 细节必考：被动关闭方（服务器）收到 ④ 后立即进入 CLOSED，比主动关闭方（客户等待 2MSL）更早结束连接！</text>
    <text class="fig-muted" x="38" y="385" font-size="10">• 若服务器收到 ① 时已无任何待发数据，也可将 ② 与 ③ 合并为一个 FIN+ACK 报文段（退化为三报文挥手）。</text>
  </svg>
  <figcaption>图 5-11：TCP 四报文挥手释放连接全过程、半关闭状态单向传数序号演进与两端关闭先后顺序</figcaption>
</figure>

### 5.8 为什么 TIME-WAIT 要等待 2MSL；保活计时器

<strong>MSL（最长报文段寿命）</strong>：RFC 793 建议为 <strong>2 分钟</strong>，因此 2MSL 为 4 分钟。现在的网络可允许实现采用更小的 MSL 值。

客户发送完最后一个确认报文段后不直接关闭，而要进入 TIME-WAIT 并等待 2MSL，有两个原因：

1. <strong>确保服务器能收到最后一个确认</strong>。若该确认丢失，服务器会超时重传连接释放报文段并停留在 LAST-ACK。客户仍处于 TIME-WAIT，可再次确认；若客户已关闭，就不会理睬重传的释放报文段，服务器将反复重传而无法关闭。
2. <strong>使本连接持续时间内产生的所有报文段都从网络中消失</strong>，避免旧连接的报文段干扰下一个新连接。

> <strong>★考点</strong>：TIME-WAIT 的两个作用（保证最后一个 ACK 到达、防止旧报文段干扰新连接）常以简答题出现；2MSL = 4 分钟（RFC 793 的 MSL 取 2 分钟）。

<strong>保活计时器</strong>：服务器每收到一次客户数据，就重新设置并启动保活计时器。若在定时周期内未收到客户数据，计时器到点后服务器向客户发送<strong>探测报文段</strong>，以后<strong>每隔 75 秒</strong>发送一次；若一连发送 <strong>10 个</strong>探测报文段仍无响应，就认为客户主机出了故障，关闭该连接。

<figure class="cpd-figure not-content" id="cn-w5-fig-12">
  <svg viewBox="0 0 720 322" role="img" aria-label="TIME-WAIT等待2MSL的双重作用图解与TCP保活计时器及四大计时器对比">
    <defs>
      <marker id="cn-w5-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="24" y="42" width="216" height="214" rx="6"/>
    <rect class="fig-box-subtle" x="252" y="42" width="216" height="214" rx="6"/>
    <rect class="fig-box-subtle" x="480" y="42" width="216" height="214" rx="6"/>
    <line class="fig-line" x1="68" y1="80" x2="68" y2="226"/>
    <line class="fig-line" x1="202" y1="80" x2="202" y2="226"/>
    <line class="fig-line-dashed" x1="68" y1="92" x2="152" y2="112" stroke="var(--sl-color-red)" stroke-width="1.5"/>
    <line class="fig-line" x1="154" y1="107" x2="164" y2="117" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <line class="fig-line" x1="164" y1="107" x2="154" y2="117" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <line class="fig-line" x1="202" y1="140" x2="68" y2="166" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-end="url(#cn-w5-arr-12)"/>
    <line class="fig-line" x1="68" y1="178" x2="202" y2="204" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w5-arr-12)"/>
    <line class="fig-line" x1="274" y1="136" x2="446" y2="136" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#cn-w5-arr-12)"/>
    <line class="fig-line-dashed" x1="358" y1="114" x2="358" y2="158" stroke="var(--sl-color-purple)"/>
    <line class="fig-line" x1="502" y1="126" x2="676" y2="126" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w5-arr-12)"/>
    <rect class="fig-box-purple" x="274" y="84" width="84" height="26" rx="4"/>
    <rect class="fig-box-green" x="362" y="84" width="84" height="26" rx="4"/>
    <rect class="fig-box-orange" x="274" y="174" width="172" height="62" rx="4"/>
    <rect class="fig-box-blue" x="502" y="84" width="78" height="28" rx="4"/>
    <rect class="fig-box-red" x="588" y="84" width="88" height="28" rx="4"/>
    <rect class="fig-box" x="502" y="160" width="174" height="76" rx="4"/>
    <rect class="fig-box" x="96" y="138" width="82" height="16" rx="3"/>
    <rect class="fig-box" x="96" y="182" width="82" height="16" rx="3"/>
    <rect class="fig-box" x="24" y="266" width="672" height="42" rx="5"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">TIME-WAIT 等待 2MSL（4 分钟）双重作用与 TCP 四大计时器汇总</text>
    <text class="fig-orange" x="132" y="60" text-anchor="middle" font-size="11">作用①：防末次 ④ ACK 丢失</text>
    <text class="fig-muted" x="68" y="75" text-anchor="middle" font-size="9.5">A(TIME-WAIT)</text>
    <text class="fig-muted" x="202" y="75" text-anchor="middle" font-size="9.5">B(LAST-ACK)</text>
    <text class="fig-red" x="118" y="94" text-anchor="middle" font-size="9">④ ACK 丢失</text>
    <text class="fig-orange" x="137" y="150" text-anchor="middle" font-size="9">超时重传 ③ FIN</text>
    <text class="fig-green" x="137" y="194" text-anchor="middle" font-size="9">重发 ④ + 重置2MSL</text>
    <text class="fig-muted" x="132" y="244" text-anchor="middle" font-size="9.5">去向 ACK + 来向重传 FIN ≤ 2MSL</text>
    <text class="fig-purple" x="360" y="60" text-anchor="middle" font-size="11">作用②：排空旧连接滞留报文</text>
    <text class="fig-purple" x="316" y="101" text-anchor="middle" font-size="9.5">第 1 个 MSL</text>
    <text class="fig-green" x="404" y="101" text-anchor="middle" font-size="9.5">第 2 个 MSL</text>
    <text class="fig-muted" x="274" y="152" font-size="9">0</text>
    <text class="fig-muted" x="358" y="170" text-anchor="middle" font-size="9">2 min</text>
    <text class="fig-muted" x="446" y="152" text-anchor="end" font-size="9">4 min</text>
    <text class="fig-orange" x="360" y="193" text-anchor="middle" font-size="9.5">本连接产生的所有旧报文段</text>
    <text class="fig-label" x="360" y="210" text-anchor="middle" font-size="9.5">在 2MSL 内彻底从网络消亡</text>
    <text class="fig-muted" x="360" y="226" text-anchor="middle" font-size="9">防复用相同四元组的新连接误收</text>
    <text class="fig-blue" x="588" y="60" text-anchor="middle" font-size="11">保活计时器（Keepalive）</text>
    <text class="fig-blue" x="541" y="102" text-anchor="middle" font-size="9.5">空闲 2 小时</text>
    <text class="fig-red" x="632" y="102" text-anchor="middle" font-size="9.5">75s × 10 次探测</text>
    <text class="fig-muted" x="588" y="144" text-anchor="middle" font-size="9">收到客户数据即重置 2h 计时器</text>
    <text class="fig-label" x="512" y="179" font-size="9.5">• 场景：客户主机突然断电/崩溃</text>
    <text class="fig-label" x="512" y="197" font-size="9.5">• 2h 无数据后每 75s 发 1 个探测段</text>
    <text class="fig-red" x="512" y="215" font-size="9.5">• 连发 10 个无响应 → 释放连接</text>
    <text class="fig-muted" x="512" y="230" font-size="8.5">防止服务器半开连接永久占用资源</text>
    <text class="fig-label" x="38" y="284" font-size="9">★ TCP 四大计时器：① 重传（RTO 超时） ② 持续（rwnd=0 探测） ③ 时间等待（2MSL） ④ 保活（2h+75s×10）。</text>
    <text class="fig-muted" x="38" y="300" font-size="10">★ RFC 793 建议 MSL = 2 分钟（2MSL = 4 分钟），纯粹是工程经验值，与 IP 首部 TTL 跳数虽同控存活但量纲不同。</text>
  </svg>
  <figcaption>图 5-12：TIME-WAIT 等待 2MSL 的两大核心作用、服务器保活计时器探测流程与 TCP 四大计时器对比</figcaption>
</figure>

## 第6章 应用层

### 6.1 应用层概述：体系结构的最顶层

计算机网络体系结构自下而上分为五层，每层只解决一个特定问题，层层叠加后由应用层完成最终目标：

- <strong>物理层</strong>：解决使用何种信号来传输比特的问题。
- <strong>数据链路层</strong>：解决分组在一个网络或一段链路上传输的问题。
- <strong>网络层</strong>：解决分组在多个网络上传输和路由的问题。
- <strong>运输层</strong>：解决进程之间基于网络的通信问题。
- <strong>应用层</strong>：享受下面各层提供的服务，解决<strong>通过应用进程的交互来实现特定网络应用</strong>的问题。

应用层的地位特殊：它位于体系结构的<strong>最顶层</strong>，其功能是<strong>设计和建立计算机网络的最终目的</strong>，也是计算机网络中<strong>发展最快的部分</strong>。下层所有工作——调制比特、封装成帧、选路转发、端到端传输——最终都是为了让两个应用进程能够交换报文。

<figure class="cpd-figure not-content" id="cn-w6-fig-1">
  <svg viewBox="0 0 720 316" role="img" aria-label="五层体系结构自底向上服务递进、各层PDU及应用层最顶层定位">
    <defs>
      <marker id="cn-w6-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="284" rx="8"/>
    <line class="fig-line-dashed" x1="190" y1="73" x2="212" y2="73"/>
    <line class="fig-line-dashed" x1="190" y1="119" x2="212" y2="119"/>
    <line class="fig-line-dashed" x1="190" y1="165" x2="212" y2="165"/>
    <line class="fig-line-dashed" x1="190" y1="211" x2="212" y2="211"/>
    <line class="fig-line-dashed" x1="190" y1="257" x2="212" y2="257"/>
    <line class="fig-line-dashed" x1="382" y1="73" x2="396" y2="73"/>
    <line class="fig-line-dashed" x1="382" y1="119" x2="396" y2="119"/>
    <line class="fig-line-dashed" x1="382" y1="165" x2="396" y2="165"/>
    <line class="fig-line-dashed" x1="382" y1="211" x2="396" y2="211"/>
    <line class="fig-line-dashed" x1="382" y1="257" x2="396" y2="257"/>
    <line class="fig-line-accent" x1="116" y1="240" x2="116" y2="230" marker-end="url(#cn-w6-arr-1)"/>
    <line class="fig-line-accent" x1="116" y1="194" x2="116" y2="184" marker-end="url(#cn-w6-arr-1)"/>
    <line class="fig-line-accent" x1="116" y1="148" x2="116" y2="138" marker-end="url(#cn-w6-arr-1)"/>
    <line class="fig-line-accent" x1="116" y1="102" x2="116" y2="92" marker-end="url(#cn-w6-arr-1)"/>
    <rect class="fig-box-blue" x="42" y="56" width="148" height="34" rx="5"/>
    <rect class="fig-box-purple" x="42" y="102" width="148" height="34" rx="5"/>
    <rect class="fig-box-green" x="42" y="148" width="148" height="34" rx="5"/>
    <rect class="fig-box-orange" x="42" y="194" width="148" height="34" rx="5"/>
    <rect class="fig-box" x="42" y="240" width="148" height="34" rx="5"/>
    <rect class="fig-box-blue" x="212" y="56" width="170" height="34" rx="4"/>
    <rect class="fig-box" x="212" y="102" width="170" height="34" rx="4"/>
    <rect class="fig-box" x="212" y="148" width="170" height="34" rx="4"/>
    <rect class="fig-box" x="212" y="194" width="170" height="34" rx="4"/>
    <rect class="fig-box" x="212" y="240" width="170" height="34" rx="4"/>
    <rect class="fig-box-blue" x="396" y="56" width="292" height="34" rx="4"/>
    <rect class="fig-box" x="396" y="102" width="292" height="34" rx="4"/>
    <rect class="fig-box" x="396" y="148" width="292" height="34" rx="4"/>
    <rect class="fig-box" x="396" y="194" width="292" height="34" rx="4"/>
    <rect class="fig-box" x="396" y="240" width="292" height="34" rx="4"/>
    <text class="fig-title" x="360" y="38" text-anchor="middle" font-size="13">五层体系结构逐层递进：每层解决的核心问题与 PDU 演进</text>
    <text class="fig-blue" x="116" y="77" text-anchor="middle" font-size="11.5">应用层（最顶层）</text>
    <text class="fig-purple" x="116" y="123" text-anchor="middle" font-size="11.5">运输层</text>
    <text class="fig-green" x="116" y="169" text-anchor="middle" font-size="11.5">网络层</text>
    <text class="fig-orange" x="116" y="215" text-anchor="middle" font-size="11.5">数据链路层</text>
    <text class="fig-label" x="116" y="261" text-anchor="middle" font-size="11.5">物理层</text>
    <text class="fig-blue" x="297" y="77" text-anchor="middle" font-size="10">应用报文 ｜ HTTP / DNS / SMTP</text>
    <text class="fig-label" x="297" y="123" text-anchor="middle" font-size="10">报文段 / 用户数据报 ｜ TCP / UDP</text>
    <text class="fig-label" x="297" y="169" text-anchor="middle" font-size="10">IP 数据报（分组）｜ IP / ICMP</text>
    <text class="fig-label" x="297" y="215" text-anchor="middle" font-size="10">帧（Frame）｜ 以太网 / PPP</text>
    <text class="fig-muted" x="297" y="261" text-anchor="middle" font-size="10">比特流（0/1 信号）｜ 媒体接口</text>
    <text class="fig-blue" x="542" y="77" text-anchor="middle" font-size="10.5">进程交互实现网络应用（不再向上提供服务）</text>
    <text class="fig-label" x="542" y="123" text-anchor="middle" font-size="10.5">端到端进程间通信（端口复用/分用、可靠或尽力）</text>
    <text class="fig-label" x="542" y="169" text-anchor="middle" font-size="10.5">主机到主机跨网传输与路由选择（IP 寻址）</text>
    <text class="fig-label" x="542" y="215" text-anchor="middle" font-size="10.5">相邻节点间单段链路的成帧与差错/接入控制</text>
    <text class="fig-muted" x="542" y="261" text-anchor="middle" font-size="10.5">屏蔽传输媒体差异，透明传输比特流信号</text>
    <text class="fig-muted" x="360" y="291" text-anchor="middle" font-size="10">★ 考点：应用层享受下四层服务，只规定应用进程间的报文类型、格式与交互规则，建立计算机网络的最终目的</text>
  </svg>
  <figcaption>图 6-1：五层体系结构自底向上服务递进、各层典型 PDU 及应用层处于最顶层的核心定位</figcaption>
</figure>

> <strong>★考点：应用层不解决“比特怎么传、分组怎么走、进程怎么找到进程”，它只回答一个问题：两个应用进程之间交换什么样的报文、按什么规则交互</strong>。

### 6.2 网络应用的演进与 C/S、P2P 模型

应用层是网络应用形态变化最快的地方，演进脉络大致为三段：

- <strong>早期基于文本的应用</strong>：电子邮件、远程登录、文件传送、新闻组。
- <strong>20 世纪 90 年代</strong>：<strong>万维网</strong>把因特网带入千家万户，是推动因特网飞速发展的关键应用。
- <strong>当今</strong>：<strong>即时通信</strong>、<strong>P2P 文件共享</strong>以及各种<strong>音视频应用</strong>，网络应用一直层出不穷。

推动应用不断涌现的外部条件有两个：<strong>计算设备的小型化和无处不在</strong>，以及<strong>宽带住宅接入和无线接入的日益普及和迅速发展</strong>，后者为未来更多新型网络应用提供了广阔的舞台。

从通信的组织方式看，网络应用有两种基本模型：

| 对比项 | 客户/服务器（C/S） | 对等（P2P） |
|---|---|---|
| 角色关系 | 服务器被动等待，客户主动发起请求 | 各节点地位对等，既可请求也可提供服务 |
| 服务与资源 | 集中在少数服务器上，服务器必须常开 | 分散在各对等节点上，节点越多资源越丰富 |
| 典型应用 | 万维网（HTTP）、域名系统（DNS）、电子邮件 | P2P 文件共享 |

<figure class="cpd-figure not-content" id="cn-w6-fig-2">
  <svg viewBox="0 0 720 318" role="img" aria-label="客户服务器CS模型与对等P2P模型通信组织方式及架构特点对比">
    <defs>
      <marker id="cn-w6-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="336" height="228" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="42" width="336" height="228" rx="6"/>
    <line class="fig-line" x1="76" y1="188" x2="132" y2="118" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-start="url(#cn-w6-arr-2)" marker-end="url(#cn-w6-arr-2)"/>
    <line class="fig-line" x1="184" y1="188" x2="184" y2="118" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-start="url(#cn-w6-arr-2)" marker-end="url(#cn-w6-arr-2)"/>
    <line class="fig-line" x1="292" y1="188" x2="236" y2="118" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-start="url(#cn-w6-arr-2)" marker-end="url(#cn-w6-arr-2)"/>
    <line class="fig-line" x1="466" y1="108" x2="606" y2="108" stroke="var(--sl-color-green)" stroke-width="1.5"/>
    <line class="fig-line" x1="466" y1="108" x2="466" y2="198" stroke="var(--sl-color-green)" stroke-width="1.5"/>
    <line class="fig-line" x1="606" y1="108" x2="606" y2="198" stroke="var(--sl-color-green)" stroke-width="1.5"/>
    <line class="fig-line" x1="466" y1="198" x2="606" y2="198" stroke="var(--sl-color-green)" stroke-width="1.5"/>
    <line class="fig-line" x1="466" y1="108" x2="606" y2="198" stroke="var(--sl-color-green)" stroke-width="1.4"/>
    <line class="fig-line" x1="606" y1="108" x2="466" y2="198" stroke="var(--sl-color-green)" stroke-width="1.4"/>
    <rect class="fig-box-blue" x="104" y="76" width="160" height="42" rx="5"/>
    <rect class="fig-box" x="32" y="188" width="88" height="36" rx="4"/>
    <rect class="fig-box" x="140" y="188" width="88" height="36" rx="4"/>
    <rect class="fig-box" x="248" y="188" width="88" height="36" rx="4"/>
    <rect class="fig-box" x="128" y="142" width="112" height="18" rx="3"/>
    <circle class="fig-box-green" cx="466" cy="108" r="24"/>
    <circle class="fig-box-green" cx="606" cy="108" r="24"/>
    <circle class="fig-box-green" cx="466" cy="198" r="24"/>
    <circle class="fig-box-green" cx="606" cy="198" r="24"/>
    <rect class="fig-box" x="486" y="143" width="100" height="20" rx="4"/>
    <rect class="fig-box" x="16" y="278" width="688" height="28" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">网络应用两大通信组织模型：客户/服务器（C/S）与对等（P2P）</text>
    <text class="fig-blue" x="184" y="62" text-anchor="middle" font-size="11.5">① 客户/服务器模型（C/S）</text>
    <text class="fig-blue" x="184" y="93" text-anchor="middle" font-size="10.5">服务器（常开、固定 IP）</text>
    <text class="fig-muted" x="184" y="109" text-anchor="middle" font-size="9">被动等待请求（如 Web、DNS、邮件）</text>
    <text class="fig-blue" x="184" y="155" text-anchor="middle" font-size="9.5">主动请求 / 被动响应</text>
    <text class="fig-label" x="76" y="204" text-anchor="middle" font-size="10">客户 1</text>
    <text class="fig-muted" x="76" y="217" text-anchor="middle" font-size="8.5">动态 IP</text>
    <text class="fig-label" x="184" y="204" text-anchor="middle" font-size="10">客户 2</text>
    <text class="fig-muted" x="184" y="217" text-anchor="middle" font-size="8.5">互不直连</text>
    <text class="fig-label" x="292" y="204" text-anchor="middle" font-size="10">客户 3</text>
    <text class="fig-muted" x="292" y="217" text-anchor="middle" font-size="8.5">按需上线</text>
    <text class="fig-muted" x="184" y="246" text-anchor="middle" font-size="9.5">特点：服务集中、易控制，但服务器易成单点瓶颈</text>
    <text class="fig-muted" x="184" y="261" text-anchor="middle" font-size="9">扩展方式：增加高性能主机、负载均衡或服务器集群</text>
    <text class="fig-green" x="536" y="62" text-anchor="middle" font-size="11.5">② 对等模型（P2P）</text>
    <text class="fig-green" x="466" y="112" text-anchor="middle" font-size="10.5">节点 A</text>
    <text class="fig-green" x="606" y="112" text-anchor="middle" font-size="10.5">节点 B</text>
    <text class="fig-green" x="466" y="202" text-anchor="middle" font-size="10.5">节点 C</text>
    <text class="fig-green" x="606" y="202" text-anchor="middle" font-size="10.5">节点 D</text>
    <text class="fig-green" x="536" y="157" text-anchor="middle" font-size="9.5">互为客户与服务器</text>
    <text class="fig-muted" x="536" y="246" text-anchor="middle" font-size="9.5">特点：文件分散存放，节点既是下载者又是上传者</text>
    <text class="fig-muted" x="536" y="261" text-anchor="middle" font-size="9">扩展方式：具有自扩展性，参与节点越多总带宽越大</text>
    <text class="fig-label" x="360" y="296" text-anchor="middle" font-size="10">★ 区分要点：P2P 并非“绝对没有服务器”（常需索引/Tracker），核心在于主体资源由对等方直接相互提供，无需常开中心服务器。</text>
  </svg>
  <figcaption>图 6-2：客户/服务器（C/S）集中式架构与对等（P2P）分布式自扩展架构对比</figcaption>
</figure>

> <strong>注意</strong>：P2P 的关键不在“没有服务器”这句话本身，而在于<strong>文件分散存放在众多对等节点上</strong>，请求者同时也可能是提供者，因此服务能力随参与节点数增长。

<figure class="cpd-figure not-content" id="cn-w6-fig-3">
  <svg viewBox="0 0 720 328" role="img" aria-label="CS与P2P文件分发最短时间公式推导及随客户数N变化的扩展性曲线对比">
    <defs>
      <marker id="cn-w6-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="336" height="228" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="42" width="336" height="228" rx="6"/>
    <rect class="fig-box-blue" x="28" y="72" width="312" height="88" rx="5"/>
    <rect class="fig-box-green" x="28" y="168" width="312" height="92" rx="5"/>
    <line class="fig-line" x1="416" y1="224" x2="682" y2="224" marker-end="url(#cn-w6-arr-3)"/>
    <line class="fig-line" x1="416" y1="224" x2="416" y2="72" marker-end="url(#cn-w6-arr-3)"/>
    <line class="fig-line-dashed" x1="416" y1="166" x2="672" y2="166"/>
    <path class="fig-line" d="M 416 214 L 664 82" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <path class="fig-line-green" d="M 416 214 C 460 192, 520 176, 666 170"/>
    <rect class="fig-box" x="424" y="232" width="268" height="28" rx="4"/>
    <rect class="fig-box" x="16" y="278" width="688" height="36" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">C/S 与 P2P 文件分发最短时间推导及随客户数 N 扩展性对比</text>
    <text class="fig-blue" x="184" y="60" text-anchor="middle" font-size="11">① 理论最短分发时间下界公式</text>
    <text class="fig-blue" x="40" y="90" font-size="10.5">C/S 模型：服务器必须逐一发送 N 份副本</text>
    <text class="fig-label" x="40" y="110" font-size="10.5">D_CS = max { N·F / u_s , F / d_min }</text>
    <text class="fig-muted" x="40" y="128" font-size="9">• u_s 为服务器上传速率，d_min 为最慢客户下载速率</text>
    <text class="fig-muted" x="40" y="144" font-size="9">• 当 N 较大时受 N·F / u_s 主导，呈 O(N) 线性增长</text>
    <text class="fig-green" x="40" y="186" font-size="10.5">P2P 模型：客户下载块后即参与协同上传</text>
    <text class="fig-label" x="40" y="206" font-size="10.5">D_P2P = max { F / u_s , F / d_min , N·F / (u_s + Σu_i) }</text>
    <text class="fig-muted" x="40" y="224" font-size="9">• 仅需服务器上传至少 1 份 F/u_s，总上载能力 Σu_i 随 N 放大</text>
    <text class="fig-muted" x="40" y="240" font-size="9">• 分子分母同含 N（当 u_i = u 时），时间趋于常数级上限</text>
    <text class="fig-green" x="536" y="60" text-anchor="middle" font-size="11">② 分发时间 D 随客户规模 N 变化曲线</text>
    <text class="fig-muted" x="424" y="82" font-size="9.5">分发时间 D</text>
    <text class="fig-muted" x="676" y="217" text-anchor="end" font-size="9.5">客户数 N</text>
    <text class="fig-blue" x="596" y="96" font-size="10">C/S：O(N) 线性上升</text>
    <text class="fig-green" x="556" y="158" font-size="10">P2P：有界自扩展平台</text>
    <text class="fig-muted" x="424" y="160" font-size="8.5">渐近下界 F / min(u, d_min)</text>
    <text class="fig-label" x="558" y="250" text-anchor="middle" font-size="8.5">例：F=15Gb, u_s=30M, u_i=2M, N=1000 → C/S 500000s vs P2P ≈7500s</text>
    <text class="fig-label" x="360" y="294" text-anchor="middle" font-size="9.5">★ 408 公式记忆：三项 max{} 分别代表【源头至少发 1 份】、【最慢接收方收完 1 份】、【系统总上载带宽发送 N 份】的物理极限。</text>
    <text class="fig-muted" x="360" y="309" text-anchor="middle" font-size="9">注：该模型假设链路瓶颈仅在接入网且客户下载率始终大于上传率，实际还受块调度策略（最稀缺优先）影响。</text>
  </svg>
  <figcaption>图 6-3：C/S 与 P2P 文件分发最短时间公式推导及随客户数量 N 变化的扩展性对比</figcaption>
</figure>

### 6.3 典型网络应用及其应用层协议

<strong>（1）万维网与 HTTP。</strong> 在浏览器的地址栏中输入某个网站的域名，就可以访问该网站的内容，这就是推动因特网飞速发展的<strong>万维网应用</strong>，其相关的应用层协议为<strong>超文本传送协议 HTTP</strong>。

<strong>（2）域名解析与 DNS。</strong> TCP/IP 体系的<strong>网际层用 IP 地址来标识目的主机，但用户只记得便于记忆的域名。从域名到 IP 地址的转换</strong>工作，由<strong>属于应用层范畴</strong>的<strong>域名系统 DNS</strong> 在后台帮用户<strong>自动完成</strong>，以方便用户的使用。

<figure class="cpd-figure not-content" id="cn-w6-fig-4">
  <svg viewBox="0 0 720 338" role="img" aria-label="输入域名即可访问网站背后的DNS域名解析、TCP三次握手与HTTP请求响应全流程">
    <defs>
      <marker id="cn-w6-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="688" height="244" rx="6"/>
    <line class="fig-line" x1="92" y1="84" x2="92" y2="276" stroke-width="1.6"/>
    <line class="fig-line" x1="272" y1="84" x2="272" y2="276" stroke-width="1.6"/>
    <line class="fig-line" x1="452" y1="84" x2="452" y2="276" stroke-width="1.6"/>
    <line class="fig-line" x1="628" y1="84" x2="628" y2="276" stroke-width="1.6"/>
    <line class="fig-line" x1="92" y1="104" x2="272" y2="104" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line" x1="272" y1="132" x2="452" y2="132" stroke="var(--sl-color-purple)" stroke-width="1.6" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line" x1="452" y1="160" x2="272" y2="160" stroke="var(--sl-color-purple)" stroke-width="1.6" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line" x1="272" y1="188" x2="92" y2="188" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line-dashed" x1="92" y1="216" x2="628" y2="216" stroke="var(--sl-color-orange)" stroke-width="1.6" marker-start="url(#cn-w6-arr-4)" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line" x1="92" y1="244" x2="628" y2="244" stroke="var(--sl-color-green)" stroke-width="1.7" marker-end="url(#cn-w6-arr-4)"/>
    <line class="fig-line" x1="628" y1="270" x2="92" y2="270" stroke="var(--sl-color-green)" stroke-width="1.7" marker-end="url(#cn-w6-arr-4)"/>
    <rect class="fig-box-blue" x="30" y="54" width="124" height="30" rx="4"/>
    <rect class="fig-box" x="210" y="54" width="124" height="30" rx="4"/>
    <rect class="fig-box-purple" x="390" y="54" width="124" height="30" rx="4"/>
    <rect class="fig-box-green" x="566" y="54" width="124" height="30" rx="4"/>
    <rect class="fig-box" x="114" y="92" width="136" height="17" rx="3"/>
    <rect class="fig-box" x="292" y="120" width="140" height="17" rx="3"/>
    <rect class="fig-box" x="292" y="148" width="140" height="17" rx="3"/>
    <rect class="fig-box" x="114" y="176" width="136" height="17" rx="3"/>
    <rect class="fig-box" x="252" y="204" width="216" height="17" rx="3"/>
    <rect class="fig-box" x="242" y="232" width="236" height="17" rx="3"/>
    <rect class="fig-box" x="242" y="258" width="236" height="17" rx="3"/>
    <rect class="fig-box" x="16" y="294" width="688" height="32" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">“输入域名即可访问网站”全流程：DNS 解析 + TCP 建连 + HTTP 交互</text>
    <text class="fig-blue" x="92" y="73" text-anchor="middle" font-size="10.5">浏览器（HTTP 客户）</text>
    <text class="fig-label" x="272" y="73" text-anchor="middle" font-size="10.5">本机 DNS 解析器</text>
    <text class="fig-purple" x="452" y="73" text-anchor="middle" font-size="10.5">域名服务器（DNS）</text>
    <text class="fig-green" x="628" y="73" text-anchor="middle" font-size="10.5">Web 服务器（HTTP）</text>
    <text class="fig-blue" x="182" y="104" text-anchor="middle" font-size="9">① 输入域名（缓存未命中）</text>
    <text class="fig-purple" x="362" y="132" text-anchor="middle" font-size="9">② DNS 查询报文（UDP:53）</text>
    <text class="fig-purple" x="362" y="160" text-anchor="middle" font-size="9">③ DNS 响应（返回目的 IP）</text>
    <text class="fig-blue" x="182" y="188" text-anchor="middle" font-size="9">④ 交付目的主机 IP 地址</text>
    <text class="fig-orange" x="360" y="216" text-anchor="middle" font-size="9">⑤ TCP 三次握手建立连接（目的端口 80 / 443）</text>
    <text class="fig-green" x="360" y="244" text-anchor="middle" font-size="9">⑥ HTTP GET 请求报文（请求网页与资源）</text>
    <text class="fig-green" x="360" y="270" text-anchor="middle" font-size="9">⑦ HTTP 200 OK 响应报文（返回 HTML 内容）</text>
    <text class="fig-label" x="360" y="314" text-anchor="middle" font-size="10">★ 408 跨界考点：DNS 解析得到的 IP 地址服务于【网际层】，但 DNS 自身靠应用进程间报文交互实现，属于【应用层协议】。</text>
  </svg>
  <figcaption>图 6-4：浏览器访问域名背后的 DNS 解析、TCP 建连与 HTTP 请求/响应时序（含 DNS 的应用层归属）</figcaption>
</figure>

> <strong>★考点</strong>：DNS 转换的正是网络层使用的 IP 地址，但它是<strong>应用层协议</strong>——转换靠应用进程之间的请求/应答报文完成，而不是网络层的路由或寻址功能。这个“跨界”关系是选择题的常客。

<strong>（3）其它常见应用。</strong> 除万维网与域名系统外，常见的应用还有<strong>动态主机配置</strong>、<strong>电子邮件</strong>、<strong>文件传送</strong>、<strong>P2P 文件共享</strong>、<strong>多媒体网络应用</strong>等，它们各自有对应的应用层协议与基本工作原理。

<figure class="cpd-figure not-content" id="cn-w6-fig-5">
  <svg viewBox="0 0 720 356" role="img" aria-label="应用层常见协议、运输层TCP或UDP承载方式、熟知端口号与408高频考点汇总">
    <rect class="fig-box-subtle" x="16" y="42" width="688" height="264" rx="6"/>
    <rect class="fig-box-accent" x="24" y="48" width="672" height="26" rx="4"/>
    <rect class="fig-box" x="24" y="78" width="672" height="26" rx="3"/>
    <rect class="fig-box" x="24" y="134" width="672" height="26" rx="3"/>
    <rect class="fig-box" x="24" y="190" width="672" height="26" rx="3"/>
    <rect class="fig-box" x="24" y="246" width="672" height="26" rx="3"/>
    <line class="fig-divider" x1="24" y1="106" x2="696" y2="106"/>
    <line class="fig-divider" x1="24" y1="134" x2="696" y2="134"/>
    <line class="fig-divider" x1="24" y1="162" x2="696" y2="162"/>
    <line class="fig-divider" x1="24" y1="190" x2="696" y2="190"/>
    <line class="fig-divider" x1="24" y1="218" x2="696" y2="218"/>
    <line class="fig-divider" x1="24" y1="246" x2="696" y2="246"/>
    <line class="fig-divider" x1="24" y1="274" x2="696" y2="274"/>
    <rect class="fig-box" x="16" y="314" width="688" height="30" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">应用层核心协议、运输层承载与熟知端口号（0～1023）全景对照</text>
    <text class="fig-accent" x="36" y="65" font-size="10.5">网络应用</text>
    <text class="fig-accent" x="148" y="65" font-size="10.5">应用层协议</text>
    <text class="fig-accent" x="272" y="65" font-size="10.5">运输层</text>
    <text class="fig-accent" x="352" y="65" font-size="10.5">熟知端口</text>
    <text class="fig-accent" x="466" y="65" font-size="10.5">核心机制与 408 高频考点</text>
    <text class="fig-label" x="36" y="95" font-size="10">万维网 ★</text>
    <text class="fig-blue" x="148" y="95" font-size="10">HTTP / HTTPS</text>
    <text class="fig-blue" x="272" y="95" font-size="10">TCP</text>
    <text class="fig-label" x="352" y="95" font-size="10">80 / 443</text>
    <text class="fig-muted" x="466" y="95" font-size="9.5">需可靠有序字节流；HTTP/3 改走 QUIC(UDP)</text>
    <text class="fig-label" x="36" y="123" font-size="10">域名系统 ★</text>
    <text class="fig-purple" x="148" y="123" font-size="10">DNS</text>
    <text class="fig-orange" x="272" y="123" font-size="10">UDP(主)/TCP</text>
    <text class="fig-label" x="352" y="123" font-size="10">53</text>
    <text class="fig-muted" x="466" y="123" font-size="9.5">请求短小求低时延用 UDP；区域传送用 TCP</text>
    <text class="fig-label" x="36" y="151" font-size="10">动态主机配置 ★</text>
    <text class="fig-purple" x="148" y="151" font-size="10">DHCP</text>
    <text class="fig-orange" x="272" y="151" font-size="10">UDP</text>
    <text class="fig-label" x="352" y="151" font-size="10">67(服) / 68(客)</text>
    <text class="fig-muted" x="466" y="151" font-size="9.5">客户尚无 IP 时发广播，故只能基于无连接 UDP</text>
    <text class="fig-label" x="36" y="179" font-size="10">文件传送</text>
    <text class="fig-green" x="148" y="179" font-size="10">FTP / TFTP</text>
    <text class="fig-blue" x="272" y="179" font-size="10">TCP / UDP</text>
    <text class="fig-label" x="352" y="179" font-size="10">21, 20 / 69</text>
    <text class="fig-muted" x="466" y="179" font-size="9.5">FTP 21 控制 + 20 数据分离；TFTP 轻量用 UDP</text>
    <text class="fig-label" x="36" y="207" font-size="10">邮件发送</text>
    <text class="fig-blue" x="148" y="207" font-size="10">SMTP</text>
    <text class="fig-blue" x="272" y="207" font-size="10">TCP</text>
    <text class="fig-label" x="352" y="207" font-size="10">25</text>
    <text class="fig-muted" x="466" y="207" font-size="9.5">推（Push）协议，仅传 7 位 ASCII，附件需 MIME</text>
    <text class="fig-label" x="36" y="235" font-size="10">邮件读取</text>
    <text class="fig-blue" x="148" y="235" font-size="10">POP3 / IMAP</text>
    <text class="fig-blue" x="272" y="235" font-size="10">TCP</text>
    <text class="fig-label" x="352" y="235" font-size="10">110 / 143</text>
    <text class="fig-muted" x="466" y="235" font-size="9.5">拉（Pull）协议；IMAP 支持多端同步目录</text>
    <text class="fig-label" x="36" y="263" font-size="10">远程登录</text>
    <text class="fig-blue" x="148" y="263" font-size="10">Telnet / SSH</text>
    <text class="fig-blue" x="272" y="263" font-size="10">TCP</text>
    <text class="fig-label" x="352" y="263" font-size="10">23 / 22</text>
    <text class="fig-muted" x="466" y="263" font-size="9.5">Telnet 明文不安全；SSH 加密远程交互会话</text>
    <text class="fig-label" x="36" y="291" font-size="10">网络管理</text>
    <text class="fig-orange" x="148" y="291" font-size="10">SNMP</text>
    <text class="fig-orange" x="272" y="291" font-size="10">UDP</text>
    <text class="fig-label" x="352" y="291" font-size="10">161 / 162(Trap)</text>
    <text class="fig-muted" x="466" y="291" font-size="9.5">轻量管理查询与告警，避免加重网络拥塞</text>
    <text class="fig-label" x="360" y="333" text-anchor="middle" font-size="9.5">★ 记忆口诀：文件/邮件/网页/终端需可靠交付均用 TCP；短请求（DNS）、无 IP 广播（DHCP）、轻管理（SNMP）主要用 UDP（★ 为课程点名）。</text>
  </svg>
  <figcaption>图 6-5：应用层常见协议、运输层承载协议（TCP/UDP）与 IANA 熟知端口号（0～1023）对照表</figcaption>
</figure>

> <strong>注意</strong>：端口号标识的是<strong>运输层的应用进程</strong>，协议名标识的是<strong>应用层的规则</strong>；两者一起才能定位“哪个应用、用哪种方式”通信。同一应用可能使用多个端口（如 FTP 的控制连接与数据连接）。

### 6.4 应用层与下层的关系

应用层是最顶层，它<strong>享受</strong>下面各层提供的服务，自己不再向上“提供”服务。逐层看，它为应用进程换来的是：

- 运输层提供<strong>进程到进程</strong>的通信（TCP 面向连接、UDP 无连接），应用层因此不必关心数据走哪条路；
- 网络层用 <strong>IP 地址</strong>标识目的主机，并负责分组在多个网络间的传输与路由；
- 数据链路层负责分组在一个网络或一段链路上的传输，物理层负责用信号传送比特。

因此应用层协议只规定<strong>应用进程之间</strong>交换的<strong>报文类型、格式与交互规则</strong>，报文真正的传送要逐层交给下面四层完成。用户看到的“输入域名就能访问网站”，是应用层的 HTTP 与 DNS、运输层的进程通信、网络层的 IP 寻址与路由共同完成的。

<figure class="cpd-figure not-content" id="cn-w6-fig-6">
  <svg viewBox="0 0 720 338" role="img" aria-label="应用层对等实体水平交互协议与经由下四层和中间路由器的垂直封装传送路径对比">
    <defs>
      <marker id="cn-w6-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="688" height="244" rx="6"/>
    <line class="fig-line-dashed" x1="192" y1="84" x2="528" y2="84" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-start="url(#cn-w6-arr-6)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line-dashed" x1="192" y1="124" x2="528" y2="124" stroke="var(--sl-color-purple)" stroke-width="1.5" marker-start="url(#cn-w6-arr-6)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="108" y1="99" x2="108" y2="109" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="108" y1="139" x2="108" y2="149" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="108" y1="179" x2="108" y2="189" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="108" y1="219" x2="108" y2="229" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="612" y1="109" x2="612" y2="99" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="612" y1="149" x2="612" y2="139" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="612" y1="189" x2="612" y2="179" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="612" y1="229" x2="612" y2="219" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="360" y1="229" x2="360" y2="219" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <line class="fig-line" x1="360" y1="189" x2="360" y2="179" stroke="var(--sl-color-gray-4)" marker-end="url(#cn-w6-arr-6)"/>
    <path class="fig-line-accent" d="M 108 259 L 108 272 L 352 272 L 352 259" marker-end="url(#cn-w6-arr-6)"/>
    <path class="fig-line-accent" d="M 368 259 L 368 272 L 612 272 L 612 259" marker-end="url(#cn-w6-arr-6)"/>
    <rect class="fig-box-blue" x="24" y="69" width="168" height="30" rx="4"/>
    <rect class="fig-box-purple" x="24" y="109" width="168" height="30" rx="4"/>
    <rect class="fig-box-green" x="24" y="149" width="168" height="30" rx="4"/>
    <rect class="fig-box-orange" x="24" y="189" width="168" height="30" rx="4"/>
    <rect class="fig-box" x="24" y="229" width="168" height="30" rx="4"/>
    <rect class="fig-box-green" x="286" y="149" width="148" height="30" rx="4"/>
    <rect class="fig-box-orange" x="286" y="189" width="148" height="30" rx="4"/>
    <rect class="fig-box" x="286" y="229" width="148" height="30" rx="4"/>
    <rect class="fig-box-blue" x="528" y="69" width="168" height="30" rx="4"/>
    <rect class="fig-box-purple" x="528" y="109" width="168" height="30" rx="4"/>
    <rect class="fig-box-green" x="528" y="149" width="168" height="30" rx="4"/>
    <rect class="fig-box-orange" x="528" y="189" width="168" height="30" rx="4"/>
    <rect class="fig-box" x="528" y="229" width="168" height="30" rx="4"/>
    <rect class="fig-box" x="224" y="72" width="272" height="22" rx="3"/>
    <rect class="fig-box" x="240" y="113" width="240" height="22" rx="3"/>
    <rect class="fig-box" x="16" y="294" width="688" height="32" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">应用层协议（水平对等规则）与下四层封装传送（垂直服务交付）</text>
    <text class="fig-muted" x="108" y="60" text-anchor="middle" font-size="10">主机 A（发送方应用进程）</text>
    <text class="fig-muted" x="360" y="141" text-anchor="middle" font-size="10">中间路由器 R（仅至网络层）</text>
    <text class="fig-muted" x="612" y="60" text-anchor="middle" font-size="10">主机 B（接收方应用进程）</text>
    <text class="fig-blue" x="108" y="88" text-anchor="middle" font-size="10.5">应用层：报文 M</text>
    <text class="fig-purple" x="108" y="128" text-anchor="middle" font-size="10">运输层：H4 + M（端口）</text>
    <text class="fig-green" x="108" y="168" text-anchor="middle" font-size="10">网络层：H3 + 段（IP）</text>
    <text class="fig-orange" x="108" y="208" text-anchor="middle" font-size="10">链路层：H2+包+T2（MAC）</text>
    <text class="fig-label" x="108" y="248" text-anchor="middle" font-size="10">物理层：比特流发送</text>
    <text class="fig-green" x="360" y="168" text-anchor="middle" font-size="10">网络层：查表选路</text>
    <text class="fig-orange" x="360" y="208" text-anchor="middle" font-size="10">链路层：拆帧再重封装</text>
    <text class="fig-label" x="360" y="248" text-anchor="middle" font-size="10">物理层：接收 / 转发</text>
    <text class="fig-blue" x="612" y="88" text-anchor="middle" font-size="10.5">应用层：交付报文 M</text>
    <text class="fig-purple" x="612" y="128" text-anchor="middle" font-size="10">运输层：按端口分用</text>
    <text class="fig-green" x="612" y="168" text-anchor="middle" font-size="10">网络层：核对目的 IP</text>
    <text class="fig-orange" x="612" y="208" text-anchor="middle" font-size="10">链路层：检错去帧头尾</text>
    <text class="fig-label" x="612" y="248" text-anchor="middle" font-size="10">物理层：比特流接收</text>
    <text class="fig-blue" x="360" y="87" text-anchor="middle" font-size="9.5">应用层逻辑协议：报文类型 · 语法格式 · 交互规则</text>
    <text class="fig-purple" x="360" y="128" text-anchor="middle" font-size="9.5">端到端逻辑通信：端点是【应用进程】而非主机</text>
    <text class="fig-accent" x="230" y="268" text-anchor="middle" font-size="9">物理链路 1</text>
    <text class="fig-accent" x="490" y="268" text-anchor="middle" font-size="9">物理链路 2</text>
    <text class="fig-label" x="360" y="314" text-anchor="middle" font-size="10">★ 考点：路由器只拆封并处理到网络层（IP），看不到运输层端口号与应用层报文内容，因此下层对应用层完全透明。</text>
  </svg>
  <figcaption>图 6-6：应用层对等实体间水平协议规则与经由下四层及中间路由器的垂直封装传送过程</figcaption>
</figure>

> <strong>注意</strong>：应用层协议是<strong>端到端</strong>的，但端点不是主机而是<strong>应用进程</strong>；同一主机上的不同应用进程由运输层的端口号区分。因此“用哪个协议”与“和哪个进程通信”是两个层次的问题。

### 6.5 应用层核心协议详解（408 重点图解）

在 408 考研与工程实践中，应用层的核心考点集中在五大具体协议的工作机制与报文交互：**DNS 域名解析**（层次树形结构、高速缓存、递归与迭代查询）、**FTP 文件传输**（带外控制双连接、主动 PORT 与被动 PASV 模式）、**电子邮件**（SMTP 推送、POP3/IMAP 拉取与 MIME 编码）、**万维网 HTTP**（非持久/持久/流水线 RTT 计算及 HTTP/2 多路复用）以及 **DHCP 动态主机配置**（DORA 四步广播/单播交互与租期续约）。

<figure class="cpd-figure not-content" id="cn-w6-fig-7">
  <svg viewBox="0 0 720 364" role="img" aria-label="DNS层次域名空间树状结构与主机本地DNS递归查询和根顶级权威迭代查询流程对比">
    <defs>
      <marker id="cn-w6-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="246" height="264" rx="6"/>
    <rect class="fig-box-subtle" x="274" y="42" width="430" height="264" rx="6"/>
    <line class="fig-line" x1="139" y1="98" x2="62" y2="124"/>
    <line class="fig-line" x1="139" y1="98" x2="139" y2="124"/>
    <line class="fig-line" x1="139" y1="98" x2="216" y2="124"/>
    <line class="fig-line" x1="139" y1="148" x2="100" y2="174"/>
    <line class="fig-line" x1="139" y1="148" x2="188" y2="174"/>
    <line class="fig-line" x1="100" y1="198" x2="66" y2="224"/>
    <line class="fig-line" x1="100" y1="198" x2="152" y2="224"/>
    <line class="fig-line" x1="66" y1="248" x2="66" y2="266"/>
    <line class="fig-line" x1="354" y1="158" x2="394" y2="158" stroke="var(--sl-color-blue)" stroke-width="1.6" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="394" y1="180" x2="354" y2="180" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="478" y1="154" x2="574" y2="82" stroke="var(--sl-color-purple)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="574" y1="96" x2="478" y2="162" stroke="var(--sl-color-orange)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="478" y1="168" x2="574" y2="162" stroke="var(--sl-color-purple)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="574" y1="176" x2="478" y2="176" stroke="var(--sl-color-orange)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="478" y1="184" x2="574" y2="246" stroke="var(--sl-color-purple)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <line class="fig-line" x1="574" y1="260" x2="478" y2="192" stroke="var(--sl-color-green)" stroke-width="1.5" marker-end="url(#cn-w6-arr-7)"/>
    <rect class="fig-box-accent" x="104" y="72" width="70" height="26" rx="4"/>
    <rect class="fig-box-blue" x="34" y="124" width="56" height="24" rx="4"/>
    <rect class="fig-box-blue" x="111" y="124" width="56" height="24" rx="4"/>
    <rect class="fig-box-blue" x="188" y="124" width="56" height="24" rx="4"/>
    <rect class="fig-box-purple" x="68" y="174" width="64" height="24" rx="4"/>
    <rect class="fig-box" x="154" y="174" width="68" height="24" rx="4"/>
    <rect class="fig-box-green" x="34" y="224" width="64" height="24" rx="4"/>
    <rect class="fig-box" x="114" y="224" width="76" height="24" rx="4"/>
    <rect class="fig-box-orange" x="24" y="266" width="112" height="24" rx="4"/>
    <rect class="fig-box-blue" x="284" y="144" width="70" height="48" rx="4"/>
    <rect class="fig-box-accent" x="394" y="144" width="84" height="52" rx="4"/>
    <rect class="fig-box-purple" x="574" y="70" width="118" height="34" rx="4"/>
    <rect class="fig-box-purple" x="574" y="152" width="118" height="34" rx="4"/>
    <rect class="fig-box-green" x="574" y="234" width="118" height="34" rx="4"/>
    <rect class="fig-box" x="492" y="96" width="68" height="17" rx="3"/>
    <rect class="fig-box" x="492" y="160" width="68" height="17" rx="3"/>
    <rect class="fig-box" x="492" y="226" width="68" height="17" rx="3"/>
    <rect class="fig-box" x="284" y="224" width="194" height="68" rx="4"/>
    <rect class="fig-box" x="16" y="314" width="688" height="36" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">DNS 层次域名空间树与递归查询 / 迭代查询完整解析步骤</text>
    <text class="fig-accent" x="139" y="59" text-anchor="middle" font-size="11">① 倒置层次树形域名空间</text>
    <text class="fig-accent" x="139" y="89" text-anchor="middle" font-size="10">根（.）</text>
    <text class="fig-blue" x="62" y="140" text-anchor="middle" font-size="9.5">.com</text>
    <text class="fig-blue" x="139" y="140" text-anchor="middle" font-size="9.5">.cn</text>
    <text class="fig-blue" x="216" y="140" text-anchor="middle" font-size="9.5">.org</text>
    <text class="fig-purple" x="100" y="190" text-anchor="middle" font-size="9.5">edu.cn</text>
    <text class="fig-label" x="188" y="190" text-anchor="middle" font-size="9.5">com.cn</text>
    <text class="fig-green" x="66" y="240" text-anchor="middle" font-size="9.5">pku</text>
    <text class="fig-label" x="152" y="240" text-anchor="middle" font-size="9.5">tsinghua</text>
    <text class="fig-orange" x="80" y="282" text-anchor="middle" font-size="9">www.pku.edu.cn</text>
    <text class="fig-muted" x="196" y="274" text-anchor="middle" font-size="8.5">单标号 ≤63B</text>
    <text class="fig-muted" x="196" y="288" text-anchor="middle" font-size="8.5">全路径 ≤255B</text>
    <text class="fig-purple" x="489" y="59" text-anchor="middle" font-size="11">② 递归（①⑧）与迭代（②～⑦）查询过程</text>
    <text class="fig-blue" x="319" y="165" text-anchor="middle" font-size="9.5">本机</text>
    <text class="fig-muted" x="319" y="179" text-anchor="middle" font-size="8.5">解析器</text>
    <text class="fig-accent" x="436" y="166" text-anchor="middle" font-size="10">本地 DNS</text>
    <text class="fig-muted" x="436" y="182" text-anchor="middle" font-size="8.5">代理+缓存</text>
    <text class="fig-blue" x="374" y="149" text-anchor="middle" font-size="8.5">① 递归</text>
    <text class="fig-green" x="374" y="193" text-anchor="middle" font-size="8.5">⑧ IP</text>
    <text class="fig-purple" x="633" y="91" text-anchor="middle" font-size="9.5">根域名服务器</text>
    <text class="fig-purple" x="633" y="173" text-anchor="middle" font-size="9.5">顶级域名 (.com)</text>
    <text class="fig-green" x="633" y="255" text-anchor="middle" font-size="9.5">权威域名服务器</text>
    <text class="fig-purple" x="526" y="108" text-anchor="middle" font-size="8.5">②问 / ③返TLD</text>
    <text class="fig-purple" x="526" y="172" text-anchor="middle" font-size="8.5">④问 / ⑤返权威</text>
    <text class="fig-green" x="526" y="238" text-anchor="middle" font-size="8.5">⑥问 / ⑦返 IP</text>
    <text class="fig-label" x="294" y="242" font-size="9">• 递归：本地 DNS 替客户查到底</text>
    <text class="fig-label" x="294" y="259" font-size="9">• 迭代：上级仅指明下一跳 DNS</text>
    <text class="fig-muted" x="294" y="276" font-size="8.5">• 缓存命中（TTL 内）直接返回，免查根</text>
    <text class="fig-label" x="360" y="330" text-anchor="middle" font-size="9.5">★ 408 常考计算：未命中本地缓存且依次向根、顶级、权威迭代查询时，共发出 4 个查询报文（①②④⑥）、收到 4 个响应报文（③⑤⑦⑧）。</text>
    <text class="fig-muted" x="360" y="344" text-anchor="middle" font-size="9">注：主机到本地域名服务器一般用【递归查询】，本地域名服务器向根/顶级/权威一般用【迭代查询】；DNS 主要使用 UDP 53 端口。</text>
  </svg>
  <figcaption>图 6-7：DNS 倒置树形域名空间结构与递归查询（主机→本地 DNS）及迭代查询（本地 DNS→根/顶级/权威）解析步骤</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w6-fig-8">
  <svg viewBox="0 0 720 342" role="img" aria-label="FTP双连接模型：带外控制连接端口21与主动PORT模式端口20及被动PASV模式端口N对比">
    <defs>
      <marker id="cn-w6-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="336" height="244" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="42" width="336" height="244" rx="6"/>
    <rect class="fig-box" x="28" y="72" width="96" height="168" rx="5"/>
    <rect class="fig-box" x="244" y="72" width="96" height="168" rx="5"/>
    <rect class="fig-box" x="380" y="72" width="96" height="168" rx="5"/>
    <rect class="fig-box" x="596" y="72" width="96" height="168" rx="5"/>
    <line class="fig-line" x1="124" y1="114" x2="244" y2="114" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w6-arr-8)"/>
    <line class="fig-line" x1="244" y1="198" x2="124" y2="198" stroke="var(--sl-color-orange)" stroke-width="1.8" marker-end="url(#cn-w6-arr-8)"/>
    <line class="fig-line" x1="476" y1="114" x2="596" y2="114" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w6-arr-8)"/>
    <line class="fig-line" x1="476" y1="198" x2="596" y2="198" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w6-arr-8)"/>
    <rect class="fig-box-blue" x="40" y="100" width="72" height="28" rx="3"/>
    <rect class="fig-box-blue" x="256" y="100" width="72" height="28" rx="3"/>
    <rect class="fig-box-orange" x="40" y="184" width="72" height="28" rx="3"/>
    <rect class="fig-box-orange" x="256" y="184" width="72" height="28" rx="3"/>
    <rect class="fig-box-blue" x="392" y="100" width="72" height="28" rx="3"/>
    <rect class="fig-box-blue" x="608" y="100" width="72" height="28" rx="3"/>
    <rect class="fig-box-green" x="392" y="184" width="72" height="28" rx="3"/>
    <rect class="fig-box-green" x="608" y="184" width="72" height="28" rx="3"/>
    <rect class="fig-box" x="132" y="122" width="104" height="32" rx="3"/>
    <rect class="fig-box" x="132" y="206" width="104" height="32" rx="3"/>
    <rect class="fig-box" x="484" y="122" width="104" height="32" rx="3"/>
    <rect class="fig-box" x="484" y="206" width="104" height="32" rx="3"/>
    <rect class="fig-box" x="16" y="294" width="688" height="34" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">FTP 双连接模型：控制连接（TCP 21）与主动 PORT / 被动 PASV 数据连接对比</text>
    <text class="fig-orange" x="184" y="60" text-anchor="middle" font-size="11">① 主动模式（PORT）：服务器 20 连客户</text>
    <text class="fig-label" x="76" y="90" text-anchor="middle" font-size="10">FTP 客户</text>
    <text class="fig-label" x="292" y="90" text-anchor="middle" font-size="10">FTP 服务器</text>
    <text class="fig-blue" x="76" y="118" text-anchor="middle" font-size="9.5">端口 X (&gt;1023)</text>
    <text class="fig-blue" x="292" y="118" text-anchor="middle" font-size="9.5">控制端口 21</text>
    <text class="fig-orange" x="76" y="202" text-anchor="middle" font-size="9.5">端口 P (&gt;1023)</text>
    <text class="fig-orange" x="292" y="202" text-anchor="middle" font-size="9.5">数据端口 20</text>
    <text class="fig-blue" x="184" y="136" text-anchor="middle" font-size="8.5">① 持久控制连接</text>
    <text class="fig-muted" x="184" y="149" text-anchor="middle" font-size="8">发 PORT P 告知端口</text>
    <text class="fig-orange" x="184" y="220" text-anchor="middle" font-size="8.5">② 服务器 20→P 建连</text>
    <text class="fig-muted" x="184" y="233" text-anchor="middle" font-size="8">易被客户防火墙拦截</text>
    <text class="fig-muted" x="184" y="274" text-anchor="middle" font-size="9">特点：服务器使用熟知端口 20 主动反连客户端</text>
    <text class="fig-green" x="536" y="60" text-anchor="middle" font-size="11">② 被动模式（PASV）：客户连服务器 N</text>
    <text class="fig-label" x="428" y="90" text-anchor="middle" font-size="10">FTP 客户</text>
    <text class="fig-label" x="644" y="90" text-anchor="middle" font-size="10">FTP 服务器</text>
    <text class="fig-blue" x="428" y="118" text-anchor="middle" font-size="9.5">端口 X (&gt;1023)</text>
    <text class="fig-blue" x="644" y="118" text-anchor="middle" font-size="9.5">控制端口 21</text>
    <text class="fig-green" x="428" y="202" text-anchor="middle" font-size="9.5">端口 P (&gt;1023)</text>
    <text class="fig-green" x="644" y="202" text-anchor="middle" font-size="9.5">临时端口 N</text>
    <text class="fig-blue" x="536" y="136" text-anchor="middle" font-size="8.5">① 持久控制连接</text>
    <text class="fig-muted" x="536" y="149" text-anchor="middle" font-size="8">发 PASV，回端口 N</text>
    <text class="fig-green" x="536" y="220" text-anchor="middle" font-size="8.5">② 客户 P→N 建连</text>
    <text class="fig-muted" x="536" y="233" text-anchor="middle" font-size="8">顺利穿透客户 NAT</text>
    <text class="fig-muted" x="536" y="274" text-anchor="middle" font-size="9">特点：两条 TCP 连接均由客户主动向服务器发起</text>
    <text class="fig-label" x="360" y="310" text-anchor="middle" font-size="9.5">★ 408 核心考点：FTP 使用两个并行 TCP 连接——控制连接（端口 21，全程保持）与数据连接（每传一个文件新建一次，用完关闭）。</text>
    <text class="fig-muted" x="360" y="323" text-anchor="middle" font-size="9">控制信息不随数据混传称为“带外控制”；主动模式下数据连接服务器端口固定为 20，被动模式下改为服务器临时端口 N（&gt;1023）。</text>
  </svg>
  <figcaption>图 6-8：FTP 带外控制双连接模型（TCP 21 控制连接 vs 主动 PORT 端口 20 / 被动 PASV 端口 N 数据连接）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w6-fig-9">
  <svg viewBox="0 0 720 342" role="img" aria-label="电子邮件系统完整传输链路：SMTP推协议、POP3与IMAP拉协议及MIME多用途扩展格式">
    <defs>
      <marker id="cn-w6-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="688" height="244" rx="6"/>
    <line class="fig-line" x1="132" y1="94" x2="184" y2="94" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w6-arr-9)"/>
    <line class="fig-line" x1="312" y1="94" x2="404" y2="94" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w6-arr-9)"/>
    <line class="fig-line" x1="532" y1="94" x2="588" y2="94" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w6-arr-9)"/>
    <rect class="fig-box" x="26" y="64" width="106" height="60" rx="5"/>
    <rect class="fig-box-blue" x="184" y="64" width="128" height="60" rx="5"/>
    <rect class="fig-box-purple" x="404" y="64" width="128" height="60" rx="5"/>
    <rect class="fig-box-green" x="588" y="64" width="106" height="60" rx="5"/>
    <rect class="fig-box" x="318" y="102" width="80" height="17" rx="3"/>
    <rect class="fig-box-orange" x="26" y="150" width="334" height="124" rx="5"/>
    <rect class="fig-box" x="372" y="150" width="322" height="124" rx="5"/>
    <rect class="fig-box" x="16" y="294" width="688" height="34" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">电子邮件系统架构：SMTP 推送链路、POP3 / IMAP 读取与 MIME 扩展</text>
    <text class="fig-label" x="79" y="88" text-anchor="middle" font-size="10">发送方用户代理</text>
    <text class="fig-muted" x="79" y="104" text-anchor="middle" font-size="8.5">（UA 客户端）</text>
    <text class="fig-blue" x="248" y="86" text-anchor="middle" font-size="10">发送方邮件服务器</text>
    <text class="fig-muted" x="248" y="102" text-anchor="middle" font-size="8.5">含外发队列缓存</text>
    <text class="fig-muted" x="248" y="116" text-anchor="middle" font-size="8">（SMTP 客户+服务）</text>
    <text class="fig-purple" x="468" y="86" text-anchor="middle" font-size="10">接收方邮件服务器</text>
    <text class="fig-muted" x="468" y="102" text-anchor="middle" font-size="8.5">按用户邮箱存放</text>
    <text class="fig-muted" x="468" y="116" text-anchor="middle" font-size="8">（SMTP 服务+邮箱）</text>
    <text class="fig-green" x="641" y="88" text-anchor="middle" font-size="10">接收方用户代理</text>
    <text class="fig-muted" x="641" y="104" text-anchor="middle" font-size="8.5">（UA 客户端）</text>
    <text class="fig-blue" x="158" y="84" text-anchor="middle" font-size="9">SMTP</text>
    <text class="fig-muted" x="158" y="112" text-anchor="middle" font-size="8">TCP 25</text>
    <text class="fig-blue" x="358" y="84" text-anchor="middle" font-size="9">SMTP 推送</text>
    <text class="fig-blue" x="358" y="114" text-anchor="middle" font-size="8">TCP 25(直推)</text>
    <text class="fig-green" x="560" y="84" text-anchor="middle" font-size="9">拉取</text>
    <text class="fig-muted" x="560" y="112" text-anchor="middle" font-size="8">110/143</text>
    <text class="fig-orange" x="38" y="170" font-size="10.5">① MIME 扩展（突破 SMTP 7 位 ASCII 限制）</text>
    <text class="fig-label" x="38" y="190" font-size="9.5">• 背景：SMTP 仅支持 7 位 ASCII 文本，不能直接传中文/图片/附件</text>
    <text class="fig-label" x="38" y="209" font-size="9.5">• 机制：发送前将非 ASCII 数据按 Base64 / QP 编码为 ASCII 字符</text>
    <text class="fig-muted" x="38" y="228" font-size="9">• 三大首部：MIME-Version、Content-Type、Content-Transfer-Encoding</text>
    <text class="fig-muted" x="38" y="246" font-size="9">• 注意：MIME 未改动 SMTP 协议本身，只是增加了邮件首部与编码</text>
    <text class="fig-muted" x="38" y="263" font-size="9">• 路径：发方非 ASCII → MIME 编码 → SMTP 传输 → 收方 MIME 解码</text>
    <text class="fig-blue" x="384" y="170" font-size="10.5">② 邮件读取协议对比：POP3 vs IMAP vs WebMail</text>
    <text class="fig-label" x="384" y="190" font-size="9.5">• SMTP（TCP 25）：推（Push）协议，用于 UA→发方与发方→收方</text>
    <text class="fig-label" x="384" y="209" font-size="9.5">• POP3（TCP 110）：拉（Pull）协议，默认下载后删除服务器邮件</text>
    <text class="fig-label" x="384" y="228" font-size="9.5">• IMAP（TCP 143）：拉（Pull）协议，支持服务器文件夹与多端同步</text>
    <text class="fig-muted" x="384" y="246" font-size="9">• WebMail（网页邮箱）：UA ↔ 邮件服务器改用 HTTP / HTTPS 协议</text>
    <text class="fig-muted" x="384" y="263" font-size="9">• 底层统一：SMTP / POP3 / IMAP 全部基于可靠的 TCP 连接</text>
    <text class="fig-label" x="360" y="310" text-anchor="middle" font-size="9.5">★ 高频陷阱：接收方用户代理（UA）从邮箱取信绝不使用 SMTP（SMTP 只能推不能拉），而必须使用 POP3、IMAP 或 HTTP。</text>
    <text class="fig-muted" x="360" y="323" text-anchor="middle" font-size="9">另外，发送方邮件服务器会直接向接收方邮件服务器建立 TCP 25 连接推送邮件，不经过多余的第三方中继。</text>
  </svg>
  <figcaption>图 6-9：电子邮件系统架构（SMTP 推送、POP3/IMAP/HTTP 拉取）与 MIME 非 ASCII 编码扩展机制</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w6-fig-10">
  <svg viewBox="0 0 720 368" role="img" aria-label="HTTP1.0非持久连接、HTTP1.1持久非流水线、流水线与HTTP2多路复用RTT时延对比">
    <defs>
      <marker id="cn-w6-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="42" width="162" height="258" rx="6"/>
    <rect class="fig-box-subtle" x="192" y="42" width="162" height="258" rx="6"/>
    <rect class="fig-box-subtle" x="366" y="42" width="162" height="258" rx="6"/>
    <rect class="fig-box-subtle" x="540" y="42" width="162" height="258" rx="6"/>
    <line class="fig-line" x1="42" y1="84" x2="42" y2="250"/>
    <line class="fig-line" x1="156" y1="84" x2="156" y2="250"/>
    <line class="fig-line" x1="216" y1="84" x2="216" y2="250"/>
    <line class="fig-line" x1="330" y1="84" x2="330" y2="250"/>
    <line class="fig-line" x1="390" y1="84" x2="390" y2="250"/>
    <line class="fig-line" x1="504" y1="84" x2="504" y2="250"/>
    <line class="fig-line" x1="564" y1="84" x2="564" y2="250"/>
    <line class="fig-line" x1="678" y1="84" x2="678" y2="250"/>
    <line class="fig-line-dashed" x1="42" y1="96" x2="156" y2="112" stroke="var(--sl-color-red)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="42" y1="118" x2="156" y2="134" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line-dashed" x1="42" y1="148" x2="156" y2="164" stroke="var(--sl-color-red)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="42" y1="170" x2="156" y2="186" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line-dashed" x1="42" y1="200" x2="156" y2="216" stroke="var(--sl-color-red)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="42" y1="222" x2="156" y2="238" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line-dashed" x1="216" y1="96" x2="330" y2="114" stroke="var(--sl-color-orange)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="216" y1="124" x2="330" y2="146" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="216" y1="160" x2="330" y2="182" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="216" y1="196" x2="330" y2="218" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line-dashed" x1="390" y1="96" x2="504" y2="114" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="390" y1="124" x2="504" y2="146" stroke="var(--sl-color-blue)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="390" y1="164" x2="504" y2="184" stroke="var(--sl-color-purple)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="390" y1="176" x2="504" y2="196" stroke="var(--sl-color-purple)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line-dashed" x1="564" y1="96" x2="678" y2="114" stroke="var(--sl-color-green)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="564" y1="124" x2="678" y2="146" stroke="var(--sl-color-green)" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="564" y1="164" x2="678" y2="184" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#cn-w6-arr-10)"/>
    <line class="fig-line" x1="678" y1="192" x2="564" y2="212" stroke="var(--sl-color-accent)" stroke-width="2" marker-end="url(#cn-w6-arr-10)"/>
    <rect class="fig-box" x="58" y="96" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="58" y="120" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="58" y="148" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="58" y="172" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="58" y="200" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="58" y="224" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="232" y="98" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="232" y="128" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="232" y="164" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="232" y="200" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="406" y="98" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="406" y="128" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="402" y="170" width="90" height="22" rx="2"/>
    <rect class="fig-box" x="580" y="98" width="82" height="16" rx="2"/>
    <rect class="fig-box" x="580" y="128" width="82" height="16" rx="2"/>
    <rect class="fig-box-green" x="572" y="174" width="98" height="30" rx="3"/>
    <rect class="fig-box-red" x="28" y="256" width="142" height="36" rx="4"/>
    <rect class="fig-box-orange" x="202" y="256" width="142" height="36" rx="4"/>
    <rect class="fig-box-blue" x="376" y="256" width="142" height="36" rx="4"/>
    <rect class="fig-box-green" x="550" y="256" width="142" height="36" rx="4"/>
    <rect class="fig-box" x="18" y="308" width="684" height="44" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">HTTP 连接方式演进与 RTT 时延对比（以 1 个 HTML + 2 个内嵌对象为例）</text>
    <text class="fig-red" x="99" y="60" text-anchor="middle" font-size="10">HTTP/1.0 非持久</text>
    <text class="fig-muted" x="99" y="74" text-anchor="middle" font-size="8.5">C ←────→ S</text>
    <text class="fig-red" x="99" y="107" text-anchor="middle" font-size="8">TCP 握手₁ (1 RTT)</text>
    <text class="fig-blue" x="99" y="131" text-anchor="middle" font-size="8">HTML (1 RTT)</text>
    <text class="fig-red" x="99" y="159" text-anchor="middle" font-size="8">TCP 握手₂ (1 RTT)</text>
    <text class="fig-blue" x="99" y="183" text-anchor="middle" font-size="8">Obj1 (1 RTT)</text>
    <text class="fig-red" x="99" y="211" text-anchor="middle" font-size="8">TCP 握手₃ (1 RTT)</text>
    <text class="fig-blue" x="99" y="235" text-anchor="middle" font-size="8">Obj2 (1 RTT)</text>
    <text class="fig-red" x="99" y="271" text-anchor="middle" font-size="9.5">总耗时：6 RTT</text>
    <text class="fig-muted" x="99" y="285" text-anchor="middle" font-size="8">每次请求都重连 TCP</text>
    <text class="fig-orange" x="273" y="60" text-anchor="middle" font-size="10">HTTP/1.1 非流水线</text>
    <text class="fig-muted" x="273" y="74" text-anchor="middle" font-size="8.5">C ←────→ S</text>
    <text class="fig-orange" x="273" y="109" text-anchor="middle" font-size="8">TCP 握手 (1 RTT)</text>
    <text class="fig-blue" x="273" y="139" text-anchor="middle" font-size="8">HTML (1 RTT)</text>
    <text class="fig-blue" x="273" y="175" text-anchor="middle" font-size="8">Obj1 (1 RTT)</text>
    <text class="fig-blue" x="273" y="211" text-anchor="middle" font-size="8">Obj2 (1 RTT)</text>
    <text class="fig-orange" x="273" y="271" text-anchor="middle" font-size="9.5">总耗时：4 RTT</text>
    <text class="fig-muted" x="273" y="285" text-anchor="middle" font-size="8">复用 TCP，串行等响应</text>
    <text class="fig-blue" x="447" y="60" text-anchor="middle" font-size="10">HTTP/1.1 流水线</text>
    <text class="fig-muted" x="447" y="74" text-anchor="middle" font-size="8.5">C ←────→ S</text>
    <text class="fig-blue" x="447" y="109" text-anchor="middle" font-size="8">TCP 握手 (1 RTT)</text>
    <text class="fig-blue" x="447" y="139" text-anchor="middle" font-size="8">HTML (1 RTT)</text>
    <text class="fig-purple" x="447" y="184" text-anchor="middle" font-size="8">连发 Obj1+Obj2 (1RTT)</text>
    <text class="fig-muted" x="447" y="220" text-anchor="middle" font-size="8">须按请求顺序返回</text>
    <text class="fig-blue" x="447" y="271" text-anchor="middle" font-size="9.5">总耗时：3 RTT</text>
    <text class="fig-muted" x="447" y="285" text-anchor="middle" font-size="8">存在应用层队头阻塞</text>
    <text class="fig-green" x="621" y="60" text-anchor="middle" font-size="10">HTTP/2 多路复用</text>
    <text class="fig-muted" x="621" y="74" text-anchor="middle" font-size="8.5">C ←────→ S</text>
    <text class="fig-green" x="621" y="109" text-anchor="middle" font-size="8">TCP 握手 (1 RTT)</text>
    <text class="fig-green" x="621" y="139" text-anchor="middle" font-size="8">Stream 1: HTML</text>
    <text class="fig-green" x="621" y="187" text-anchor="middle" font-size="8">Stream 3 &amp; 5 帧交错</text>
    <text class="fig-muted" x="621" y="199" text-anchor="middle" font-size="7.5">乱序到达再按流组装</text>
    <text class="fig-green" x="621" y="271" text-anchor="middle" font-size="9.5">总耗时：3 RTT(无阻塞)</text>
    <text class="fig-muted" x="621" y="285" text-anchor="middle" font-size="8">二进制分帧+HPACK压缩</text>
    <text class="fig-label" x="30" y="325" font-size="9.5">• 408 公式（1 个 HTML + M 个内嵌对象，忽略传输时延）：非持久串行 = 2(1+M) RTT；持久非流水线 = 2+M RTT；流水线 = 3 RTT。</text>
    <text class="fig-muted" x="30" y="342" font-size="9">• HTTP/1.x 本身无状态（用 Cookie/Session 识别用户）；HTTP/2 消除应用层 HoL；HTTP/3 改用基于 UDP 的 QUIC 消除传输层 HoL。</text>
  </svg>
  <figcaption>图 6-10：HTTP/1.0 非持久连接、HTTP/1.1 持久非流水线与流水线、HTTP/2 二进制多路复用的 RTT 时延对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cn-w6-fig-11">
  <svg viewBox="0 0 720 348" role="img" aria-label="DHCP动态主机配置协议DORA四步报文交互广播单播细节与T1T2租期续约机制">
    <defs>
      <marker id="cn-w6-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="42" width="470" height="244" rx="6"/>
    <rect class="fig-box-subtle" x="498" y="42" width="206" height="244" rx="6"/>
    <line class="fig-line" x1="94" y1="88" x2="94" y2="274" stroke-width="1.8"/>
    <line class="fig-line" x1="408" y1="88" x2="408" y2="274" stroke-width="1.8"/>
    <line class="fig-line" x1="94" y1="112" x2="408" y2="124" stroke="var(--sl-color-orange)" stroke-width="1.8" marker-end="url(#cn-w6-arr-11)"/>
    <line class="fig-line" x1="408" y1="154" x2="94" y2="166" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cn-w6-arr-11)"/>
    <line class="fig-line" x1="94" y1="196" x2="408" y2="208" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#cn-w6-arr-11)"/>
    <line class="fig-line" x1="408" y1="238" x2="94" y2="250" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cn-w6-arr-11)"/>
    <line class="fig-line" x1="601" y1="116" x2="601" y2="128" marker-end="url(#cn-w6-arr-11)"/>
    <line class="fig-line" x1="601" y1="176" x2="601" y2="188" marker-end="url(#cn-w6-arr-11)"/>
    <rect class="fig-box" x="34" y="54" width="120" height="34" rx="4"/>
    <rect class="fig-box-blue" x="348" y="54" width="120" height="34" rx="4"/>
    <rect class="fig-box" x="122" y="102" width="258" height="24" rx="3"/>
    <rect class="fig-box" x="122" y="144" width="258" height="24" rx="3"/>
    <rect class="fig-box" x="122" y="186" width="258" height="24" rx="3"/>
    <rect class="fig-box" x="122" y="228" width="258" height="24" rx="3"/>
    <rect class="fig-box-green" x="512" y="76" width="178" height="40" rx="4"/>
    <rect class="fig-box-blue" x="512" y="128" width="178" height="48" rx="4"/>
    <rect class="fig-box-orange" x="512" y="188" width="178" height="48" rx="4"/>
    <rect class="fig-box-red" x="512" y="248" width="178" height="28" rx="4"/>
    <rect class="fig-box" x="16" y="294" width="688" height="38" rx="4"/>
    <text class="fig-title" x="360" y="26" text-anchor="middle" font-size="13">DHCP 动态主机配置协议：DORA 四步交互（UDP 67/68）与租期续约机制</text>
    <text class="fig-label" x="94" y="69" text-anchor="middle" font-size="10">DHCP 客户</text>
    <text class="fig-muted" x="94" y="82" text-anchor="middle" font-size="8.5">0.0.0.0 : 68</text>
    <text class="fig-blue" x="408" y="69" text-anchor="middle" font-size="10">DHCP 服务器</text>
    <text class="fig-muted" x="408" y="82" text-anchor="middle" font-size="8.5">IP_S : 67（含地址池）</text>
    <text class="fig-orange" x="251" y="117" text-anchor="middle" font-size="9">① DHCP DISCOVER（发现）：广播 255.255.255.255，xid</text>
    <text class="fig-blue" x="251" y="159" text-anchor="middle" font-size="9">② DHCP OFFER（提供）：广播/单播，候选 IP + 掩码 + 租期 T</text>
    <text class="fig-purple" x="251" y="201" text-anchor="middle" font-size="9">③ DHCP REQUEST（请求）：仍广播！确认选中并通知其余放弃</text>
    <text class="fig-green" x="251" y="243" text-anchor="middle" font-size="9">④ DHCP ACK（确认）：广播/单播，正式启用 IP+网关+DNS</text>
    <text class="fig-purple" x="601" y="62" text-anchor="middle" font-size="10.5">IP 租期 T 与两级续约</text>
    <text class="fig-green" x="601" y="93" text-anchor="middle" font-size="9.5">0 ～ 0.5T（T1 之前）</text>
    <text class="fig-muted" x="601" y="108" text-anchor="middle" font-size="8.5">正常使用已分配 IP 地址</text>
    <text class="fig-blue" x="601" y="146" text-anchor="middle" font-size="9.5">0.5T（T1 计时器到期）</text>
    <text class="fig-muted" x="601" y="162" text-anchor="middle" font-size="8.5">单播发 REQUEST 向原服务器续约</text>
    <text class="fig-orange" x="601" y="206" text-anchor="middle" font-size="9.5">0.875T（T2 计时器到期）</text>
    <text class="fig-muted" x="601" y="222" text-anchor="middle" font-size="8.5">若 T1 未成，广播向任意服务器重绑</text>
    <text class="fig-red" x="601" y="266" text-anchor="middle" font-size="9">T 到期仍无应答 → 停用，重走 DORA</text>
    <text class="fig-label" x="30" y="311" font-size="9.5">• 408 必考细节：第 ③ 步 REQUEST 必须用广播，因为可能有多个 DHCP 服务器发回了 OFFER，广播可通知未中选服务器释放预留地址。</text>
    <text class="fig-muted" x="30" y="325" font-size="9">• 跨子网分配：路由器默认隔离广播，需开启 DHCP 中继代理（Relay Agent），将客户端广播包转为单播转发给跨网段 DHCP 服务器。</text>
  </svg>
  <figcaption>图 6-11：DHCP 动态主机配置四步 DORA 报文交互流程（UDP 67/68）、广播原因与 T1/T2 租期续约机制</figcaption>
</figure>
