目录
[1.1套接字 Sockets](#1.1套接字 Sockets)
[1.2寻址进程 Addressing processes](#1.2寻址进程 Addressing processes)
[2. 架构](#2. 架构)
[2.1客户端-服务器架构 Client-server architecture](#2.1客户端-服务器架构 Client-server architecture)
[2.2 P2P 架构 P2P architecture](#2.2 P2P 架构 P2P architecture)
[3.2网络缓存 Web caches](#3.2网络缓存 Web caches)
[3.3条件 GET Conditional GET](#3.3条件 GET Conditional GET)
[4.1传输服务要求 Transport service requirements](#4.1传输服务要求 Transport service requirements)
[4.2.1 TCP 服务:](#4.2.1 TCP 服务:)
[4.2.2安全套接字层(SSL) Secure Sockets Layer](#4.2.2安全套接字层(SSL) Secure Sockets Layer)
[4.2.3 UDP 服务: UDP service](#4.2.3 UDP 服务: UDP service)
[5.1.1开放协议: Open protocols](#5.1.1开放协议: Open protocols)
[5.1.2私有协议: Private protocols](#5.1.2私有协议: Private protocols)
[5.2 具体协议](#5.2 具体协议)
[6.1.2使用 TCP:](#6.1.2使用 TCP:)
[6.1.3 HTTP 是 "无状态 "的 HTTP is "stateless"](#6.1.3 HTTP 是 “无状态 ”的 HTTP is “stateless”)
[6.2 请求信息格式](#6.2 请求信息格式)
[6.2.1(HTTP1.1)请求方式request method](#6.2.1(HTTP1.1)请求方式request method)
[6.2.2(HTTP1.1)请求标头request headers](#6.2.2(HTTP1.1)请求标头request headers)
[6.2.2.1连接类型 ->非持久HTTP Non-persistent HTTP](#6.2.2.1连接类型 ->非持久HTTP Non-persistent HTTP)
[6.2.2.2持久 HTTP Persistent HTTP](#6.2.2.2持久 HTTP Persistent HTTP)
[6.4.1 HTTP/2](#6.4.1 HTTP/2)
[6.4.2 HTTP/3](#6.4.2 HTTP/3)
[7.域名系统Domain Name System (DNS)](#7.域名系统Domain Name System (DNS))
[7.1 DNS:服务与结构](#7.1 DNS:服务与结构)
[7.2 DNS:层级结构](#7.2 DNS:层级结构)
[7.2.1根域名服务器 root name servers](#7.2.1根域名服务器 root name servers)
[7.2.2顶级域名(TLD)服务器:Top-level domain (TLD) servers](#7.2.2顶级域名(TLD)服务器:Top-level domain (TLD) servers)
[7.2.3权威DNS服务器: Authoritative DNS servers](#7.2.3权威DNS服务器: Authoritative DNS servers)
[7.2.4本地DNS名称服务器 Local DNS name server](#7.2.4本地DNS名称服务器 Local DNS name server)
[7.3 DNS名称查询/解析方式](#7.3 DNS名称查询/解析方式)
[7.4 DNS:缓存 caching](#7.4 DNS:缓存 caching)
[7.5 DNS资源记录 DNS Resource records](#7.5 DNS资源记录 DNS Resource records)
[7.6 查找与获取域名信息](#7.6 查找与获取域名信息)
[7.7 DNS协议的消息格式](#7.7 DNS协议的消息格式)
[7.8 将记录插入DNS Inserting records into DNS](#7.8 将记录插入DNS Inserting records into DNS)
[8.P2P应用程序 P2P Applications](#8.P2P应用程序 P2P Applications)
[8.1纯P2P架构 Pure P2P architecture](#8.1纯P2P架构 Pure P2P architecture)
[8.2文件分发:客户端-服务器 vs P2P File distribution: client-server vs P2P](#8.2文件分发:客户端-服务器 vs P2P File distribution: client-server vs P2P)
[8.2.1文件分发时间:客户端-服务器 File distribution time: client-server](#8.2.1文件分发时间:客户端-服务器 File distribution time: client-server)
[8.2.2文件分发时间:P2P File distribution time: P2P](#8.2.2文件分发时间:P2P File distribution time: P2P)
[8.2.3 图示:](#8.2.3 图示:)
[8.3 BitTorrent ---> P2P的文件共享协议](#8.3 BitTorrent ---> P2P的文件共享协议)
[8.3.1 流程](#8.3.1 流程)
[8.3.2片段选择Piece selection](#8.3.2片段选择Piece selection)
[8.3.2.2宏观视角 Piece selection - Macro view](#8.3.2.2宏观视角 Piece selection - Macro view)
[8.3.2.3 策略](#8.3.2.3 策略)
[8.3.3 BitTorrent -- 内部机制Internal Mechanism](#8.3.3 BitTorrent – 内部机制Internal Mechanism)
[8.3.3.2乐观解封 Optimistic unchoking](#8.3.3.2乐观解封 Optimistic unchoking)
[8.3.4仅上传模式 Upload-Only mode](#8.3.4仅上传模式 Upload-Only mode)
[9.套接字编程Socket programming](#9.套接字编程Socket programming)
[9.1使用UDP的套接字编程 Socket programming with UDP](#9.1使用UDP的套接字编程 Socket programming with UDP)
[9.2使用TCP的套接字编程 Socket programming with TCP](#9.2使用TCP的套接字编程 Socket programming with TCP)
1.通讯原理
1.1套接字 Sockets

-
进程向/从其套接字发送/接收信息 Process sends/receives messages to/from its socket
-
插口类似于门 Socket analogous to door
·发送进程将信息 "推出 "门外 Sending process "push" message out of the door
·发送进程依靠门另一侧的传输基础设施向接收进程的套接字传送信息Sending process relies on transport infrastructure on other side of door to deliver message to socket at receiving process

1.2寻址进程 Addressing processes
-
要接收信息,进程必须有标识符 To receive messages, process must have identifier
-
主机设备具有唯一的 32 位 IPv4 和/或 128 位 IPv6 Host device has unique 32-bit IPv4 and/or 128-bit IPv6
-
进程网络标识符:Process network identifier
·IPv4:port 192.168.1.100:80
·[IPv6]:port [240e:3a1:4cb1:69d0:f40c:4269:74a2:7ea3]:80
2. 架构
- 客户端-服务器 Client-server - 点对点 Peer-to-peer
2.1客户端-服务器架构 Client-server architecture
- 服务器 Server
·永远在线的主机 Always-on host
·永久 IP 地址 Permanent IP address
·高性能/分布式计算 High performance / Distributed computing
·服务器进程:等待联系(监听) Server process: waits to be contacted (Listen)
- 客户端 Clients
·链接到服务器以获得服务 Link to the server for service
·可能间歇性连接互联网 May be intermittently connect to the internet
·动态 IP 地址 Dynamic IP address
·彼此不直接通信 Do not communicate directly with each other
·客户端进程:启动通信 Client process: initiates communication
2.2 P2P 架构 P2P architecture
-
无需始终在线的服务器 No always-on server is needed
-
终端系统直接交换数据 End systems directly exchange data
-
同一主机上的客户端进程/服务器进程 Client process / server process on the same host
-
对等系统向其他对等系统请求服务,向其他对等系统提供服务作为回报
Peers request service from other peers, provide service in return to other peers
·自扩展能力--新的对等点带来新的服务能力和新的服务需求 Self scalability -- new peers bring new service capacity, as well as new service demands
-
对等网络间歇性连接 Peers are intermittently connected
-
动态 IP 地址 Dynamic IP addresses
3.客户端,服务器端技术(cookie,缓存,条件get)
3.1cookie
-
由服务器发送并存储在客户端(通常是浏览器)中的一小段文本数据,用于在客户端和服务器之间保持状态和传递信息
-
由四个部分组成
·HTTP 响应信息中的 cookie 标头行 cookie header line of HTTP response message
·下一个 HTTP 请求信息中的 Cookie 标头行 cookie header line in next HTTP request message
·保存在用户主机上、由用户浏览器管理的 cookie 文件 cookie file kept on user's host, managed by user's browser
·网站的后端数据库 back-end database at Web site
- 示例:

- Cookie 的用途
·授权 Authorization
·推荐 Recommendations
·用户会话状态 User session state
- Cookie 和隐私: Cookies and privacy
·Cookie 允许网站了解您的很多信息 Cookies permit sites to learn a lot about you
·记得清理 cookie
3.2网络缓存 Web caches

- 缓存既是客户端也是服务器 Cache acts as both client and server
·原始请求客户端的服务器 server for original requesting client
·客户端到原始服务器 client to origin server
-
缓存通常由互联网服务提供商(大学、公司、住宅互联网服务提供商)安装 Typically cache is installed by ISP (university, company, residential ISP)
-
网络缓存的作用
·缩短客户端请求的响应时间 reduce response time for client request
·减少机构访问链路的流量 reduce traffic on an institution's access link
·互联网上缓存密集:使 "贫乏 "的内容提供商能够有效地提供内容(P2P 文件共享也是如此)Internet dense with caches: enables " poor " content providers to effectively deliver content (so too does P2P file sharing)
3.3条件 GET Conditional GET
- 目标:如果缓存中有最新缓存版本,则不发送对象 Goal: don't send object if cache has up-to-date cached version
·无对象传输延迟 no object transmission delay
·降低链接利用率 lower link utilization
- 缓存:在 HTTP 请求中指定缓存副本的日期 cache: specify date of cached copy in HTTP request
If-modified-since: <date>
- 服务器:如果缓存副本是最新的,则响应不包含对象: server: response contains no object if cached copy is up-to-date
HTTP/1.0 304 Not Modified

4.互联网传输服务
4.1传输服务要求 Transport service requirements
- 数据完整性 Data Integrity
·100% 可靠的数据传输或容忍一定程度的丢失100% reliable data transfer or Tolerate some loss
- 时间 Timing
·某些应用程序要求低延迟 Some apps require low delay
- 吞吐量 Throughput
·有些应用程序需要最低吞吐量才能 "有效 "运行Some apps require minimum amount of throughput to be "effective"
·其他应用程序不需要 Others do not require
- 安全性 Security
·有些应用程序需要加密 Some apps require encryption
·数据完整性检查 Data integrity check
4.2传输服务协议/模型
4.2.1 TCP 服务:
-
发送和接收进程之间的可靠传输 Reliable transport between sending and receiving process
-
流量控制:发送方不会压倒接收方 Flow control: sender won't overwhelm receiver
-
拥塞控制:当网络超载时对发送方进行节流 Congestion control: throttle sender when network overloaded
-
不提供:定时、最小吞吐量保证、安全性 Does not offer: timing, minimum throughput guarantee, security
-
面向连接:客户端和服务器进程之间需要设置Connection-oriented: setup required between client and server processes
4.2.2安全套接字层(SSL) Secure Sockets Layer
- TCP 和 UDP
·无加密 No encryption
·明文密码 -> 互联网 Cleartext psws -> Internet
- SSL
·提供加密 TCP 连接 Provides encrypted TCP connection
·数据完整性 Data integrity
·端点验证 End-point authentication
- SSL 位于应用程序层 SSL is at app layer
·应用程序使用与 TCP "对话 "的 SSL 库 Apps use SSL libraries, that "talk" to TCP
- SSL 套接字 API SSL socket API
·明文密码 -> 加密密码 -> 互联网 Cleartext psw -> encrypted psw -> Internet
4.2.3 UDP 服务: UDP service
- 发送和接收进程之间的数据传输不可靠
Unreliable data transfer between sending and receiving process
-
不提供:可靠性、流量控制、拥塞控制、定时、吞吐量保证、安全性或连接设置、Does not offer: reliability, flow control, congestion control, timing, throughput guarantee, security, or connection setup
-
简单且快速Simple and fast!
5.应用层协议:
5.1协议定义
-
架构Architecture:CS 或/和 P2P CS or/and P2P
-
交换信息的类型Types of messages exchanged:请求、响应 request, response
-
信息语法:信息中有哪些字段以及如何划分字段Message syntax: what fields in messages & how fields are delineated
-
信息语义:字段中信息的含义 Message semantics: meaning of information in fields
-
信息时间:何时以及如何Message timing: when and how
5.1.1开放协议: Open protocols
-
在 RFC(征求意见稿)中定义 Defined in RFCs
-
允许互操作 Allows for interoperability
-
例如 HTTP、SMTP、FTP
5.1.2私有协议: Private protocols
- 如 Skype、游戏、自己的协议... e.g., Skype, Games, you own protocols
5.2 具体协议
-
HTTP
-
DNS
-
P2P:BitTorrent
6.超文本传输协议(HTTP)
6.1(HTTP1.1)特征
6.1.1基于请求响应模型
-
应用层协议 Application layer protocol
-
客户端/服务器模型 Client/server model
·客户端:请求、接收(使用 HTTP 协议)和显示 Web 对象的浏览器(渲染)Client: browser that requests, receives, (using HTTP protocol) and show Web objects (Render)
·服务器: 网络服务器(使用 HTTP 协议)根据请求发送对象Server: Web server sends (using HTTP protocol) objects in response to requests

6.1.2使用 TCP:
-
客户端启动与服务器的TCP 连接(创建套接字),端口为 80(https 为 443)Client initiates TCP connection (creates socket) to server, port 80(443 for https)
-
服务器接受客户端的 TCP 连接 Server accepts TCP connection from client
-
浏览器(HTTP 客户端)和网络服务器(HTTP 服务器)之间交换 HTTP 信息(应用层协议信息) HTTP messages (application-layer protocol messages) exchanged between browser (HTTP client) and Web server (HTTP server)
-
关闭 TCP 连接TCP connection closed
6.1.3 HTTP 是 "无状态 "的 HTTP is "stateless"
-
服务器不保留客户过去请求的任何信息server maintains no information about past client requests
-
维护 "状态 "的协议非常复杂!Protocols that maintain "state" are complex!
·必须维护过去的历史(状态)past history (state) must be maintained
·如果服务器/客户端崩溃,它们对 "状态 "的看法可能不一致,必须加以协调 if server/client crashes, their views of "state" may be inconsistent, must be reconciled
6.2 请求信息格式

6.2.1(HTTP1.1)请求方式request method


- 额外的:GET, POST, PUT, DELETE, PATCH, HEAD, TRACE, OPTIONS, CONNECT
6.2.2(HTTP1.1)请求标头request headers
-
是在HTTP请求中包含的键值对,用于提供关于请求和客户端的额外信息,帮助服务器正确处理请求。
-
示例:
·主机名 Host name
·身份验证 Authentication
·内容类型 Content types
·用户代理信息 User-agent information
·缓存/Cookie Caching / Cookies
·连接类型 Types of connections
·...
6.2.2.1连接类型 ->非持久HTTP Non-persistent HTTP
- 定义
·通过 TCP 连接最多发送一个对象 At most one object sent over TCP connection
> 然后关闭连接 connection then closed
·下载多个对象需要多个连接Downloading multiple objects required multiple connections
- 响应时间
·RTT(往返时间):一个小数据包从客户端到服务器的往返时间 RTT (Round Trip Time): time for a small packet to travel from client to server and back round trip time
·HTTP 响应时间: HTTP response time:
>启动 TCP 连接的一个 RTT One RTT to initiate TCP connection
>返回 HTTP 请求和 HTTP 响应前几个字节的一个 RTT One RTT for HTTP request and first few bytes of HTTP response to return
>文件传输时间 File transmission time
>非持久 HTTP 响应时间 = 2RTT+ 文件传输时间Non-persistent HTTP response time = 2RTT+ file transmission time
- 问题: Non-persistent HTTP issues
·每个对象需要 2 个 RTT Requires 2 RTTs per object
·每个 TCP 连接的操作系统开销 OS overhead for each TCP connection
·浏览器经常打开并行 TCP 连接以获取引用对象Browsers often open parallel TCP connections to fetch referenced objects

6.2.2.2持久 HTTP Persistent HTTP
- 定义
·可通过客户端和服务器之间的单个 TCP 连接发送多个对象 Multiple objects can be sent over single TCP connection between client, server
- 问题: Persistent HTTP issues
·服务器在发送响应后仍打开连接Server leaves connection open after sending response
·同一客户端/服务器之间的后续 HTTP 消息通过打开的连接发送 Subsequent HTTP messages between same client/server sent over open connection
·客户端在遇到引用对象时立即发送请求 Client sends requests as soon as it encounters a referenced object
·所有被引用对象只需一个 RTT As little as one RTT for all the referenced objects
6.3响应信息格式:响应状态代码 HTTP response status codes

-
状态代码出现在服务器到客户端响应信息的第一行。Status code appears in 1st line in server-to-client response message.
-
一些示例代码:
·200 确定 OK
·301 永久移动 Move Permanently
·400 错误请求 Bad Request
·404 未找到 Not Found
·505 HTTP 版本不支持 HTTP Version Not Supported
- 种类
·1xx: 告知 - 已收到请求,继续处理Informational - Request received, continuing process
·2xx:v成功 - 已成功接收、理解并接受操作Success - The action was successfully received, understood, and accepted
·3xx:v重定向 - 必须采取进一步行动才能完成请求Redirection - Further action must be taken in order to complete the request
·4xx: 客户端错误 - 请求包含错误语法或无法完成Client Error - The request contains bad syntax or cannot be fulfilled
·5xx: 服务器错误 - 服务器未能满足显然有效的请求Server Error - The server failed to fulfill an apparently valid request
6.4迭代升级:
6.4.1 HTTP/2
- 复用(Multiplexing):
·可以在单个连接上同时发送多个请求,从而减少延迟。Multiple requests can be sent over a single connection simultaneously, reducing latency
- 头部压缩(Header Compression):
·压缩 HTTP 头部以减少开销并改善加载时间。 Compresses HTTP headers to reduce overhead and improve load times
- 服务器推送(Server Push):
·允许服务器在客户端请求之前发送资源,从而提高页面加载速度。Allows the server to send resources to the client before they are requested, improving page load speed
- 二进制帧(Binary Framing):
·使用二进制格式而非文本格式,使其解析更高效且更不容易出错。
Uses binary format instead of text, making it more efficient to parse and less error-prone.
- 基于 QUIC 协议(Based on QUIC Protocol):
·使用 UDP 而不是 TCP,减少连接建立时间,并在高延迟网络中提供更好的性能。
Uses UDP instead of TCP, reducing connection setup time and providing better performance in high-latency networks
6.4.2 HTTP/3
- 改进的复用(Improved Multiplexing):
·消除了 HTTP/2 中出现的头部阻塞问题,进一步增强性能。 Eliminates head-of-line blocking issues seen in HTTP/2, further enhancing performance.
- 更好的连接恢复(Better Connection Resumption):
·即使在网络变化(例如,Wi-Fi 和移动数据之间切换)发生时,连接的恢复和继续也更快。Faster connection recovery and continuation even if network changes occur (e.g., switching between Wi-Fi and mobile data).
- 内置安全性(Built-in Security):
·默认集成 TLS(传输层安全性)以实现加密和安全的数据传输。 Integrates TLS (Transport Layer Security) for encrypted and secure data transmission by default.
7.域名系统Domain Name System (DNS)
-
主要作用是将易于记忆的域名转换为机器可识别的IP地址
-
应用层协议(Application-layer protocol):
·客户端/服务器架构(C/S architecture)
·使用UDP(port 53)
·主机和名称服务器之间进行通信以解析名称hosts, name servers communicate to resolve names
- 分布式数据库在多个名称服务器的层次结构中实现。 Distributed database implemented in hierarchy of many name servers
7.1 DNS:服务与结构
- DNS服务(DNS services)
·主机名到IP地址的转换(Hostname to IP address translation):通过A记录实现。
·主机别名(Host aliasing):通过CNAME记录实现。
规范名称和别名(Canonical and alias names):区分主机的正式名称和别名。
·邮件服务器别名(Mail server aliasing):通过MX记录实现。
·负载分布(Load distribution):在多个服务器之间分配请求负载。
·复制的Web服务器(Replicated Web servers):一个域名对应多个IP地址
***
不集中化DNS的原因
·单点故障(Single point of failure):
·流量负荷(Traffic volume):
·远距离的集中数据库(Distant centralized database)
·维护(Maintenance):。
- DNS结构:一个分布式、分层的数据库 a distributed, hierarchical database

·客户端想要获取 www.amazon.com 的 IP 地址:Client wants IP for www.amazon.com:
>客户端查询根服务器以找到 .com 的 DNS 服务器Client queries root server to find com DNS server
>客户端查询 .com 的 DNS 服务器以获取 amazon.com 的 DNS 服务器Client queries .com DNS server to get amazon.com DNS server
>客户端查询 amazon.com 的 DNS 服务器以获取 www.amazon.com 的 IP 地址Client queries amazon.com DNS server to get IP address for www.amazon.com
7.2 DNS:层级结构
7.2.1根域名服务器 root name servers
-
当本地名称服务器无法解析名称时,与之联系 Contacted by local name server that can not resolve name
-
根域名服务器: Root name server
• 如果名称映射未知,它会联系权威名称服务器 Contacts authoritative name server if name mapping not known
• 获取映射 Gets mapping
• 将映射返回给本地名称服务器 Returns mapping to local name server

7.2.2顶级域名(TLD)服务器:Top-level domain (TLD) servers
-
负责管理所有顶级域名,例如:com、org、net、edu、aero、jobs、museum 以及所有顶级国家域名,例如:cn、uk、fr、ca、jp Responsible for com, org, net, edu, aero, jobs, museums, and all Top-level country domains, e.g.: cn, uk, fr, ca, jp
-
例如:
·网络解决方案(Network Solutions)维护 .com TLD 服务器
·教育促进基金会(Educause)维护 .edu TLD 服务器(https://net.educause.edu/)
7.2.3权威DNS服务器: Authoritative DNS servers
-
组织自己的DNS服务器,提供组织内部主机名的权威映射至IP地址 Organization's own DNS server(s), providing authoritative hostname to IP mappings for organization's named hosts
-
可以由组织自身或服务提供商维护Can be maintained by organization or service provider
7.2.4本地DNS名称服务器 Local DNS name server
- 并不严格属于层级结构 Does not strictly belong to hierarchy
(注:这里的"层级结构"指的是DNS的层级式命名空间结构,由根域名服务器、顶级域名服务器和权威DNS服务器等组成。)
- 每个ISP(住宅ISP、公司、大学)都有一个 Each ISP (residential ISP, company, university) has one
• 也被称为"默认名称服务器" Also called "default name server"
- 当主机发起DNS查询时,查询会被发送到其本地DNS服务器 When host makes DNS query, query is sent to its local DNS server
• 拥有最近名称到地址转换对的本地缓存(但可能已过期!) Has local cache of recent name-to-address translation pairs (but may be out of date!)
• 充当代理,将查询转发到层级结构中 Acts as proxy, forwards query into hierarchy
7.3 DNS名称查询/解析方式
- DNS查询可以分为迭代查询和递归查询两种类型。
·迭代查询中,服务器会告诉客户端下一步应该查询哪个服务器;
·递归查询中,服务器会替客户端进行完整的查询过程,直到找到最终结果或确定名称不存在。
- DNS名称解析示例 :西交利物浦大学(XJTLU)的一台主机想要获取www.feimax.com的IP地址 Host at XJTLU wants IP address for www.feimax.com
·迭代查询Iterated query(左下)
>联系服务器,服务器回复要联系的下一个服务器名称 contacted server replies with name of server to contact
>"我不知道这个名称,但你可以问这个服务器" "I don't know this name, but ask this server"
- 递归查询 Recursive query:(右下)
• 将名称解析的负担放在被联系的名称服务器上 Puts burden of name resolution on contacted name server
• 在层级结构的上层造成较大负载 Heavy load at upper levels of hierarchy
7.4 DNS:缓存 caching
- 一旦(任何)名称服务器学习到映射关系,它就会缓存这个映射Once (any) name server learns mapping, it caches mapping
• 缓存条目在一段时间后(TTL,生存时间)会超时(消失)Cache entries timeout (disappear) after some time (TTL)
• TLD服务器通常会被缓存在本地名称服务器中TLD servers typically cached in local name servers
> 因此根名称服务器不常被访问 thus root name servers not often visited
- 缓存的条目可能会过时 Cached entries may be out-of-date
• 如果名称主机更改了IP地址,可能直到所有TTL过期后才会在整个互联网上被知晓If name host changes IP address, may not be known Internet-wide until all TTLs expire
7.5 DNS资源记录 DNS Resource records
- DNS:分布式数据库,存储资源记录(Resource Records,RR)distributed database storing resource records (RR

·type = A
>名称是主机名 name is hostname
>值是IP地址 value is IP address
·type =CNAME
>名称是某个"规范"(即真实)名称的别名 name is alias name for some "canonical" (the real) name
>例如,www.taobao.com实际上是www.taobao.com.danuoyi.tbcache.com
>值是规范名称 value is canonical name
·类型=NS
>名称是域名name is domain (e.g., foo.com)
>值是该域名的权威名称服务器的主机名 value is hostname of authoritative name server for this domain
·类型=MX
>值是与该名称关联的邮件服务器的名称 value is name of mailserver associated with name
7.6 查找与获取域名信息
- 查找域名 Loop up a domain name
• 使用nslookup命令 Use nslookup command:


·当互联网接入点发生变化时,来自DNS的IP地址可能会改变。When the internet access point changes, the IP address from DNS may change
- 获取域名信息 Get information of a domain name
• 使用whois命令Use whois command:whois feimax.com

7.7 DNS协议的消息格式
-
查询和回复消息,都使用相同的消息格式 Query and reply messages, both with same message format
-
消息头 Message header
▪ 标识:16位编号,用于查询,回复查询使用相同的编号 identification: 16 bit # for query, reply to query uses same #
▪ 标志:flags
> 查询或回复 query or reply
> 递归请求 recursion desired
> 递归可用 recursion available
> 回复是权威的 reply is authoritative
查询的名称、类型字段 Name, type fields for a query
查询响应中的资源记录 RRs in response to query
权威服务器的记录 Records for authoritative servers
可能使用的其他"有帮助"的信息Additional"helpful" info that may be used

7.8 将记录插入DNS Inserting records into DNS
- 示例:新创企业"feimax.com" new startup "feimax.com "
·在DNS注册商(例如,net.cn)注册名称feimax.com Register name feimax.com at DNS registrar (e.g., net.cn)
• 通常,你不需要设置NS记录 Normally, you don't need to set up the NS record
• 插入用于主机的IP地址的A记录 Insert A record for the IP address of your host
• 插入用于电子邮件的MX记录Insert MX record for email
• 域名是一种稀缺资源。Domain name is a scarce resource.
8.P2P应用程序 P2P Applications
8.1纯P2P架构 Pure P2P architecture
-
没有始终在线的服务器 No always-on server
-
任意终端直接通信 Arbitrary end systems directly communicate
-
点对点节点IP地址可变 Peers change IP addresses
-
示例:
• file distribution (BitTorrent)
• Streaming (KanKan)
• VoIP(Skype)
8.2文件分发:客户端-服务器 vs P2P File distribution: client-server vs P2P
8.2.1文件分发时间:客户端-服务器 File distribution time: client-server
- 服务器传输:必须按顺序发送(上传)N个文件副本:server transmission: must sequentially send (upload) N file copies:
• 发送一个副本的时间time to send one copy:F/us
• 发送N个副本的时间time to send N copies:NF/us
- 客户端:每个客户端必须下载文件副本 client: each client must download file copy
• dmin = 最小的客户端下载速率 min client download rate
• 客户端最大下载时间max client download time:F/dmin
- 使用客户端-服务器方法将文件F分发给N个客户端的时间 time to distribute F to N clients using client-server approach


8.2.2文件分发时间:P2P File distribution time: P2P
- 服务器传输:必须顺序发送(上传)至少一份文件副本。 server transmission: must sequentially send (upload) at least one file copies
• 发送一份副本所需时间time to send one copy:F/us
- 客户端:每个客户端必须下载文件副本。client: each client must download file copy
• 最小客户端下载时间:min client download time: F/dmin
- 客户端:总共必须下载 N 份文件的 NF 位。 clients: as total must download NF bits
• 最大上传速率(限制最大下载速率)为 us + Σui max upload rate (limiting max download rate) is us + Σui
- 使用 P2P 方法将 F 分发到 N 个客户端所需的时间。 time to distribute F to N clients using P2P approach

8.2.3 图示:
- client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us

8.3 BitTorrent ---> P2P的文件共享协议
8.3.1 流程
- 为了共享一个文件或一组文件,发起者首先创建一个 .torrent 文件, 这个小文件包含:
To share a file or group of files, the initiator first creates a .torrent file, a small file that contains
• 要共享的文件的元数据 Metadata about the files to be shared
• tracker 的信息,即协调文件分发的计算机 Information about the tracker, the computer that coordinates the file distribution
-
下载者首先获取 .torrent 文件,然后连接到指定的 tracker,tracker 告诉他们从其他对等端(peers)下载文件的哪些部分。Downloaders first obtain a .torrent file, and then connect to the specified tracker, which tells them from which other peers to download the pieces of the file
-
在下载的同时,对等端会上传片段给其他对等端 While downloading, peer uploads pieces to other peers
-
对等端可能会改变与其交换片段的对等端Peer may change peers with whom it exchanges pieces
-
对等端可能加入或离开 Peers may come and go
-
一旦对等端拥有整个文件,它可以(自私地)离开或继续留在 torrent 中 Once peer has entire file, it may (selfishly) leave or remain in torrent
-
图示:

·Seeder = 提供完整文件的对等端。 a peer that provides the complete file.
·Initial seeder = 提供初始副本的对等端。a peer that provides the initial copy
·一旦一个 lecher 拥有完整的片段,它就可以潜在地与其他下载者共享该片段。 As soon as a leecher has a complete piece, it can potentially share it with other downloaders
·最终每个 leecher 会通过获取所有片段并组合文件而成为 seeder,并验证该文件的"校验和"。Eventually each leecher becomes a seeder by obtaining all the pieces, and assembles the file. Verifies the "checksum" of the file.

****** 专业术语解释:
-
Tracker:一个中心服务器,维护着所有对等体的列表,跟踪参与种子(torrent)的对等体。
-
对等端加入torrent: Peer joining torrent
• 没有任何片段,但会随着时间从其他对等端积累它们 has no pieces, but will accumulate them over time from other peers
• 向 tracker 注册以获取对等端列表,连接到部分对等端("邻居") registers with tracker to get list of peers, connects to subset of peers ("neighbors")
- 种子/群组Torrent/Swarm:一组交换文件块的对等体。
· .torrent 文件的元数据 Metadata of .torrent
> 所有片段的 SHA-1 哈希值 SHA-1 hashes of all pieces
>片段与文件的映射 A mapping of the pieces to files
>片段大小 Piece size
>文件长度 Length of the file
>tracker 的引用 A tracker reference
8.3.2片段选择Piece selection
8.3.2.1面临的问题
-
不同对等端选择片段的顺序对于良好的性能至关重要。The order in which pieces are selected by different peers is critical for good performance
-
如果采用低效的策略,那么对等端可能会陷入一种情况,每个对等端都有所有相同的一组容易获取的片段,但没有缺失的片段。 If an inefficient policy is used, then peers may end up in a situation where each has all identical set of easily available pieces, and none of the missing ones
-
如果原始 seeder 过早下线,那么文件就无法完全下载!If the original seed is prematurely taken down, then the file cannot be completely downloaded!
8.3.2.2宏观视角 Piece selection - Macro view

小重叠 - 良好 大重叠 - 浪费带宽 无重叠 - 危险
Small Overlap Good Large Overlap Waste Bandwidth No Overlap Dangerous
8.3.2.3 策略
- 最稀有优先Rarest First:一般规则General rule
• 确定在你的对等端中最稀有的片段,并优先下载这些片段。 Determine the pieces that are most rare among your peers, and download those first.
• 这样可以确保最常见的可用片段留到最后再下载。This ensures that the most commonly available pieces are left till the end to download.
- 随机首片Random First Piece:特殊情况,开始时Special case, at the beginning
• 最初,节点没有任何可交换的内容 Initially, a peer has nothing to trade
• 尽快获取完整片段非常重要 Important to get a complete piece ASAP
• 随机选择文件中的一个片段并下载它Select a random piece of the file and download it
- 结束模式Endgame Mode:特殊情况Special case
• 临近结束时,从每个包含缺失片段的对等端请求这些片段。 Near the end, missing pieces are requested from every peer containing them.
• 这样可以确保下载不会因某一个传输速率较慢的对等端而无法完成。This ensures that a download is not prevented from completion due to a single peer with a slow transfer rate
• 尽管会浪费一些带宽,但实际上这并不算太多。 Some bandwidth is wasted, but in practice, this is not too much.
8.3.3 BitTorrent -- 内部机制Internal Mechanism
- 内置的激励机制(所有魔法发生的地方): Built-in incentive mechanism (where all the magic happens):
• 拒绝算法 Choking Algorithm
• 乐观解封 Optimistic Unchoking
8.3.3.1拒绝Choking
- 拒绝是一种暂时性的拒绝上传。它是 BT 应对免费搭车者(那些只下载但从不上传的人)的最强大的想法之一。 Choking is a temporary refusal to upload. It is one of BT's most powerful idea to deal with free riders (those who only download but never upload).
• 为了防止免费搭车者和避免网络拥塞 For avoiding free riders and avoiding network congestion
- 针锋相对策略基于博弈论概念。Tit-for-tat strategy is based on game-theoretic concepts.
8.3.3.2乐观解封 Optimistic unchoking
- 一个对等端向那些当前以最高速率向她发送数据块的四个对等端发送数据块A peer sends pieces to those four peers currently sending her chunks at highest rate
• 其他对等端被 Alice 拒绝(不从她那里接收数据块) other peers are choked by Alice (do not receive chunks from her)
• 每 10 秒重新评估前 4 名 re-evaluate top 4 every10 secs
- 每 30 秒:随机选择另一个对等端,开始发送数据块 Every 30 secs: randomly select another peer, starts sending chunks
• "乐观地解封"这个对等端 optimistically unchoke" this peer
• 新选择的对手可能会加入前 4 名 newly chosen peer may join top 4
8.3.4仅上传模式 Upload-Only mode
-
一旦下载完成,对等端只能进行上传。问题是,应该上传给哪些节点?Once download is complete, a peer can only upload. The question is, which nodes to upload to
-
策略:将数据上传给上传速率最佳的节点。这确保了数据块能够更快地复制,并且快速生成新的种子。Policy: Upload to those with the best upload rate. This ensures that pieces get replicated faster, and new seeders are created fast
9.套接字编程Socket programming
- 目标:学习如何构建使用套接字进行通信的客户端/服务器应用程序
Goal: learn how to build client/server applications that communicate using sockets
- 套接字:应用程序进程与端到端传输协议之间的门
Socket: door between application process and end-endtransport protocol

- 两种套接字类型对应于两种传输服务:Two socket types for two transport services
• UDP:不可靠的报文 unreliable datagram
• TCP:可靠的字节流导向 reliable, byte stream-oriented
- 应用示例:
·客户端从键盘读取一行字符(数据)并将数据发送给服务器client reads a line of characters (data) from its keyboard and sends data to server
·服务器接收数据并将字符转换为大写 server receives the data and converts characters to uppercase
·服务器将修改后的数据发送回客户端 server sends modified data to client
·客户端接收修改后的数据并在屏幕上显示这行内容client receives modified data and displays line on its screen
9.1使用UDP的套接字编程 Socket programming with UDP
- UDP:客户端与服务器之间没有"连接" UDP: no "connection" between client & server
• 发送数据前没有握手过程 No handshaking before sending data
• 发送者显式地将目标IP地址和端口号附加到每个数据包上 Sender explicitly attaches IP destination address and port # to each packet
• 接收者从接收到的数据包中提取发送者的IP地址和端口号 Receiver extracts sender IP address and port# from received packet
-
UDP:传输的数据可能会丢失或接收顺序混乱 UDP: transmitted data may be lost or received out-of-order
-
应用程序视角: Application viewpoint:
• UDP提供了客户端和服务器之间字节组("数据报")的不可靠传输UDP provides unreliable transfer of groups of bytes ("datagrams") between client and server

9.2使用TCP的套接字编程 Socket programming with TCP
- 客户端必须联系服务器 Client must contact server
• 服务器进程必须首先处于运行状态 Server process must first be running
• 服务器必须创建一个欢迎客户端联系的套接字(门) Server must have created socket (door) that welcomes client' s contact
- 客户端通过以下步骤联系服务器: client contacts server by
• 创建TCP套接字,指定服务器进程的IP地址和端口号 Creating TCP socket, specifying IP address, port number of server process
• 当客户端创建套接字时,客户端的TCP与服务器的TCP建立连接 When client creates socket: client TCP establishes connection to server TCP
- 当服务器被客户端联系时,服务器的TCP为与该特定客户端通信创建一个新的套接字When contacted by client, server TCP creates new socket for server process to communicate with that particular client
• 允许服务器与多个客户端进行交互 Allows server to talk with multiple clients
• 使用源端口号来区分不同的客户端(更多内容见第3章) Source port numbers used to distinguish clients (more in Chap 3)
- 应用程序视角: Application viewpoint:
TCP提供了客户端和服务器之间可靠的、按顺序的字节流传输("管道") TCP provides reliable, in-order byte-stream transfer ("pipe") between client and server
