Why Protocols Are Needed
A-Level Computer Science Topic 14 11:17 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Two computers want to talk, but one sends its data one way and the other expects it completely differently.
两台计算机想要通话,但一台用一种方式发送数据,另一台却期待完全不同的格式。
The result is gibberish — like two people speaking different languages, with no translator.
结果就是一堆乱码——就像两个说着不同语言的人,中间没有翻译。
The fix is a protocol: a shared set of rules for how devices communicate.
解决办法是协议: 设备如何通信的一套共同规则。
Both ends must follow the same rules, or one side's signals are meaningless to the other — the meaning is lost.
两端必须遵循同一套规则,否则一方的信号对另一方毫无意义——意思就丢了。
It fixes the format of the data, the order of messages, the timing, and what to do when something goes wrong.
它规定数据的格式、消息的顺序、时序,以及出错时该怎么办。
Agree on the rules, and communication just works.
在规则上达成一致,通信自然就通了。
The internet works because every device follows the same rules, split neatly into layers.
互联网之所以运转,是因为每台设备都遵循同一套规则,并整齐地分成若干层。
Today: what a protocol is, the four-layer TCP/IP stack, TCP versus UDP, the everyday protocols you use, and how data actually crosses the network.
今天我们讲: 什么是协议、四层的 TCP/IP 协议栈、TCP 与 UDP、你每天用到的各种协议, 以及数据究竟如何穿越网络。
Let's begin.
让我们开始吧。
So what does a protocol pin down?
那么,一个协议到底规定了什么?
Four things.
四样东西。
The format — where the address ends and the data begins.
格式——地址在哪里结束、数据从哪里开始。
The order — who speaks first, and when to acknowledge.
顺序——谁先说话、什么时候要确认。
The timing — how long to wait before giving up and resending.
时序——等多久还没回应就放弃并重发。
And what to do on an error.
以及出错时该怎么做。
Networking is complex, so we split it into layers, each with one job.
联网很复杂,所以我们把它分成一层层,每一层只做一件事。
Swap one layer — say, wired for wireless — and the others never notice.
换掉其中一层——比如把有线换成无线—— 其他各层根本不会察觉。
The internet is built on the TCP/IP stack — four layers, each handling one part.
互联网建立在 TCP/IP 协议栈之上——四层,每一层负责其中一部分。
At the top, the application layer: the protocols your programs speak, like HTTP for the web.
最上面是应用层: 你的程序所说的协议,比如网页用的 HTTP。
Below it, the transport layer delivers data between programs — that is TCP and UDP.
往下是传输层,在程序之间传递数据——也就是 TCP 和 UDP。
Next, the internet layer routes packets across networks using IP addresses.
再往下,网络层用 IP 地址在各个网络之间为数据包选路。
And at the bottom, the link layer sends the raw bits over the wire or the air.
最底下是链路层, 把原始的比特通过电缆或空中发送出去。
Data flows down the stack to send, and up the stack to receive.
发送时数据顺着栈往下流,接收时顺着栈往上流。
Networking is too complex to design in one piece, so it is split into layers, each with one focused job, talking only to the layer directly above and below.
网络太复杂,没法当成一整块来设计,所以它被切成若干层,每一层只干一件事, 而且只和紧挨着它的上一层和下一层打交道。
Three benefits, and a question will ask for them.
有三个好处,题目会问到。
Modularity: you can replace one layer — swap Ethernet for Wi-Fi — without touching any of the others, because the layer above only knows the interface.
模块化:你可以替换其中一层——把以太网换成无线——而完全不必动其他各层, 因为上面那一层只认得接口。
Standardisation: different vendors' equipment interoperates because they implement the same layer boundaries.
标准化:不同厂商的设备之所以能互通, 是因为它们实现的是同样的层间边界。
And abstraction: when you write a web application you simply ignore how the bits cross the wire.
以及抽象:当你写一个网页应用时, 你完全不必去管这些比特是怎么在线缆上跑的。
The internet uses the TCP/IP protocol suite, and it has four layers.
互联网使用的是 TCP/IP 协议栈,它有四层。
Take them one at a time.
我们一层一层地看。
The application layer gives services to user programs and defines the protocols they speak — HTTP for the web, SMTP for email.
应用层为用户程序提供服务,并规定它们所讲的协议—— 网页用 HTTP,电子邮件用 SMTP。
This is where a programmer usually works.
程序员通常就工作在这一层。
The transport layer delivers data end-to-end between processes, and the processes are identified by port numbers — that is what distinguishes your browser from your email client on the same machine.
传输层在进程之间端到端地传送数据,而各个进程是用端口号来区分的—— 正是它把同一台机器上的浏览器和邮件客户端区分开来。
The internet layer carries packets between hosts using IP: each packet has a source and destination IP address, and routers forward it onward.
网络层用 IP 在主机之间搬运数据包:每个包都有源 IP 地址和目的 IP 地址, 由路由器一跳一跳地转发。
Before the router reaches the wider internet, a modem connects the home to the provider.
在路由器到达更广的互联网之前,调制解调器把家庭接到运营商。
Note that it does NOT guarantee delivery — that is the transport layer's job.
注意它并不保证送达——那是传输层的工作。
And the link layer sends bits over one physical link — copper, radio, or a fibre-optic cable — adding a frame header with MAC addresses and handling medium access, such as CSMA slash CD on Ethernet.
而链路层在一条物理链路上发送比特——铜线、无线电或光纤——加上带有 MAC 地址的帧头, 并处理介质访问,比如以太网上的载波侦听多路访问与冲突检测。
A satellite dish sends and receives radio signals over a much longer radio link.
卫星天线则通过更远的无线电链路收发信号。
Here is what the link layer actually builds.
这就是链路层实际组装出来的东西。
A preamble to let the receiver's clock lock on, a start-of-frame marker, then the frame itself, then an interpacket gap.
先是前导码,让接收方的时钟锁定; 然后是帧起始定界符;接着是帧本身;最后是帧间隔。
Inside the frame: the destination MAC address, the source MAC address, a type-or-length field, the payload — which is the packet handed down from the internet layer — and finally a frame check sequence for error detection.
帧的内部有: 目的 MAC 地址、源 MAC 地址、类型或长度字段、有效载荷—— 也就是从网络层交下来的那个数据包——最后是用于检错的帧校验序列。
Two things to take from it.
从中要记住两件事。
The addresses here are MAC addresses, not IP addresses: MAC identifies a device on this one link, IP identifies a host across networks.
这里的地址是 MAC 地址,不是 IP 地址: MAC 标识的是这一条链路上的某台设备,IP 标识的是跨网络的某台主机。
And the higher layer's data is simply carried as the payload — each layer wraps the one above.
而上层的数据只是被当作有效载荷带着走——每一层都把上面那一层包起来。
The transport layer offers a choice.
传输层给了你一个选择。
TCP is connection-oriented: it sets up a link, numbers every packet, and makes sure they all arrive, in order, resending any that get lost.
TCP 是面向连接的:它建立一条连接,给每个数据包编号, 确保它们全部按顺序到达,丢了的还会重发。
Reliable, but with overhead.
可靠,但有额外开销。
UDP is connectionless: it just fires the data off and forgets — no setup, no checking.
UDP 是无连接的: 它只管把数据一发了事——不建立连接,也不检查。
It can lose packets, but it is fast.
它可能丢包,但很快。
So the web and email use TCP; live video, gaming, and name lookups use UDP, where speed beats perfection.
所以网页和电子邮件用 TCP;直播视频、游戏和域名查询用 UDP,在那里速度胜过完美。
At the top of the stack, each job has its own protocol.
在协议栈的顶端,每一种任务都有它自己的协议。
HTTP, and its secure version HTTPS, fetch web pages.
HTTP,以及它的加密版 HTTPS,用来获取网页。
SMTP sends your email; IMAP and POP3 fetch it back.
SMTP 发送你的邮件;IMAP 和 POP3 把邮件取回来。
FTP transfers files.
FTP 传输文件。
And BitTorrent shares a file peer-to-peer, in pieces from many computers at once — so no single server carries the load.
而 BitTorrent 以对等方式分享文件, 从许多台计算机上一块一块地下载——这样就没有哪一台服务器要独自扛下所有负载。
Learn which protocol does which job.
记住哪个协议干哪件事。
Now the application protocols by name, because questions ask which one does what.
现在按名字来看应用层协议,因为题目会问哪一个做什么。
HTTP and HTTPS: browsers fetching web pages, over TCP, port eighty for HTTP and four four three for HTTPS, which is simply HTTP running inside TLS encryption.
HTTP 和 HTTPS: 浏览器获取网页用的,跑在 TCP 上,HTTP 用八十端口,HTTPS 用四四三端口, 而 HTTPS 不过就是跑在 TLS 加密里面的 HTTP。
FTP transfers files between client and server.
FTP 在客户端和服务器之间传输文件。
Then the email trio, and the distinction matters.
然后是邮件三兄弟,而它们的区别很重要。
SMTP sends email — from client to server, and between servers.
SMTP 负责发送邮件—— 从客户端到服务器,以及服务器之间。
Receiving is a different protocol.
接收用的是别的协议。
POP3 downloads and deletes it from the server, so your mail lives on one machine.
POP3 把邮件下载下来并从服务器上删掉,所以你的邮件只存在一台机器上。
IMAP leaves it on the server and syncs, so the same inbox appears on your phone and your laptop.
IMAP 把邮件留在服务器上并同步,所以你的手机和笔记本上看到的是同一个收件箱。
If you can only remember one thing: SMTP is for sending, POP3 and IMAP are for receiving.
如果只能记住一件事:SMTP 管发送,POP3 和 IMAP 管接收。
BitTorrent is different in kind from the others, and that is why it is on the syllabus.
BitTorrent 在性质上与其他协议不同,这正是它出现在考纲上的原因。
It is a peer-to-peer protocol: instead of every client pulling from one server, the file is split into pieces and each peer downloads different pieces from many other peers in parallel.
它是一种对等网络协议:不是每个客户端都去同一台服务器上拉数据, 而是把文件切成很多片,每个对等端并行地从许多其他对等端那里下载不同的片。
A tracker in the middle simply helps peers find each other; it does not carry the file.
中间的追踪器只是帮助各个对等端互相找到对方;它并不传输文件本身。
Peers that have the complete file are seeds; peers still downloading are leeches, and they upload the pieces they already have while they fetch the rest.
拥有完整文件的对等端叫种子;还在下载的叫吸血者, 它们一边取回剩下的片,一边把已经有的片上传出去。
The consequence worth stating: no single server carries all the load, so the more popular a file is, the more sources there are — which is the opposite of how a client-server system behaves under demand.
值得说出来的结论是:没有哪一台服务器承担全部负载, 所以一个文件越受欢迎,来源就越多——这与客户端服务器系统在高需求下的表现正好相反。
How does data actually cross the network?
数据究竟是怎么穿越网络的?
Two ways.
有两种方式。
Circuit switching reserves one dedicated path for the whole conversation — like an old telephone call.
电路交换为整场通话预留一条专用路径—— 就像老式的电话通话。
It guarantees bandwidth, but wastes it during silences.
它保证了带宽,但在沉默的间隙里白白浪费了它。
Packet switching instead chops the message into packets, each finding its own route, and reassembled at the far end.
分组交换则把消息切成一个个数据包,每个各自寻路,最后在另一端重新拼装起来。
It shares the network efficiently and reroutes around failures.
它高效地共享网络,还能绕开故障重新选路。
The modern internet uses packet switching.
现代互联网用的就是分组交换。
In circuit switching a dedicated path is set up between the two ends before any data flows, reserved for the whole conversation, and released at the end.
在电路交换中,在任何数据流动之前,先在两端之间建立一条专用通路, 整个通话期间都为它保留,结束时再释放。
The classic example is the traditional telephone network.
最经典的例子是传统电话网。
Two strengths: reserved bandwidth, so the rate never dips, and delivery strictly in order along one path.
它有两个优点:带宽被预留,所以速率永远不会掉下来; 而且数据沿着同一条路径严格按顺序到达。
Two weaknesses, and they are the ones exams want.
它也有两个缺点,而考试要的正是这两个。
It is inefficient, because during every silence in a conversation the reserved capacity sits unused and nobody else may have it.
它效率低,因为通话中每一次沉默,被预留的容量都空在那里没人用, 别人还不能用。
And it is slow to set up, because the whole path must be established before the first bit moves.
而且它建立得慢,因为第一个比特动起来之前,整条通路都必须先搭好。
In packet switching the data is split into packets, each sent independently.
在分组交换中,数据被切成一个个数据包,每个包独立发送。
Every packet carries the destination address, and routers make a fresh decision per packet — so packets may take different routes and arrive out of order, and the destination reassembles them using their sequence numbers.
每个包都带着目的地址,路由器对每一个包都重新做一次决定—— 所以这些包可能走不同的路线、乱序到达,由目的地用它们的序号重新组装起来。
Its strengths are efficiency, because one link is multiplexed across many conversations rather than reserved for one, and robustness, because a failure is simply routed around.
它的优点是效率高,因为一条链路是被许多通信复用的,而不是为某一次通信独占; 以及健壮,因为出了故障绕开就是了。
Its costs are variable latency, since different packets take different paths, and possible loss — which TCP, one layer up, is there to handle.
它的代价是延迟不稳定, 因为不同的包走不同的路;还有可能丢包——而这正是上面一层的 TCP 要处理的事。
Side by side, six rows.
并排来看,一共六行。
The path: dedicated and reserved, versus shared and decided per packet.
通路:专用并预留,对比共享且逐包决定。
Setup time: slow, versus none at all.
建立时间:慢,对比完全不需要。
Bandwidth use: inefficient, versus efficient.
带宽利用:低效,对比高效。
Order: strictly in order, versus possibly out of order.
顺序:严格有序,对比可能乱序。
Robustness: one failure cuts the circuit, versus rerouting around failures.
健壮性:一处故障就切断电路, 对比绕开故障重新路由。
And what each suits: constant-rate flows like voice for circuit switching, bursty flows like web and email for packet switching.
以及各自适合什么:电路交换适合语音这类速率恒定的业务, 分组交换适合网页和邮件这类突发性的业务。
Modern networks use packet switching, for exactly the two reasons in the middle of that table — efficiency and resilience.
现代网络用的是分组交换, 理由恰恰就是那张表中间的两条——效率和韧性。
A phone call and a large file download share a network.
一通电话和一次大文件下载共用一个网络。
Which switching method suits each, and why?
哪一种交换方式分别适合它们,为什么?
The phone call needs a steady stream with low delay, and it would suffer badly if pieces arrived late or out of order — so circuit switching suits it, with a dedicated path and its capacity reserved for the whole call.
电话需要平稳的数据流和低延迟,如果片段迟到或者乱序到达,通话质量会很糟—— 所以电路交换适合它,用一条专用通路,把容量在整个通话期间预留下来。
The file download does not care about timing or arrival order, because the receiver reassembles it from sequence numbers, and it benefits from using whatever capacity happens to be spare — so packet switching suits it.
文件下载并不在乎时间和到达顺序,因为接收方会按序号把它重新组装起来, 而且它还能用上任何碰巧空闲的容量——所以分组交换适合它。
And here is what earns the marks: name the property of the TRAFFIC that decides it.
而下面这一点才是拿分的地方:说出是流量的哪一个性质决定了这个选择。
Reserved capacity and low delay for the call; efficiency and resilience for the download.
对通话来说是预留容量和低延迟;对下载来说是效率和韧性。
Naming the method without naming the property is only half an answer.
只说出方法而不说出性质,只答对了一半。
Three marks to lock in.
三个要拿稳的分。
First, say why we use protocols and layers — each layer has one job and can change on its own.
第一,说清楚我们为什么用协议和分层——每一层只做一件事,而且能各自独立地改变。
Second, place the common protocols in the stack — HTTP at the application layer, TCP and UDP at transport, IP at the internet layer.
第二,把常见协议放进协议栈的正确层——HTTP 在应用层,TCP 和 UDP 在传输层,IP 在网络层。
Third, compare circuit and packet switching — a reserved path versus independent packets — with a use for each.
第三,比较电路交换和分组交换——一条预留的路径对独立的数据包——并各给一个用途。
Nail these, and this topic is yours.
掌握这些,这个专题就是你的了。