Presentation is loading. Please wait.

Presentation is loading. Please wait.

计算机网络 贺小伟 xwhe@hotmail.com.

Similar presentations


Presentation on theme: "计算机网络 贺小伟 xwhe@hotmail.com."— Presentation transcript:

1 计算机网络 贺小伟

2 第2章 应用层(Application Layer)
本章学习目标 网络应用层的概念和实现 服务模型 客户端-服务器范例 P2P范例 对常用应用层协议的探讨和分析来学习网络协议 深层次目标-特定协议 http ftp smtp pop dns 2018/9/16 主讲教师:西北大学 贺小伟

3 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

4 Ethereal Introduction
What is Ethereal? Getting started with Ethereal Exploring the Main Windows Other Window Components Exploring the Menus Writing Capture Filters Writing Display Filters A Simple Example 2018/9/16 主讲教师:西北大学 贺小伟

5 What is Ethereal? Ethereal is a free and feature rich network analyzer that rivals commercial counterparts. Ethereal can decode more than 480 protocols. Ethereal is compatible with more than 20 other sniffers and capture utilities. Display and capture filters can be used to sort through network traffic. Ethereal mailing lists are a great resource for information and support. 2018/9/16 主讲教师:西北大学 贺小伟

6 Getting started with Ethereal
Binary Ethereal packages for Windows, Linux, and various UNIX flavors can be downloaded from Source code can be downloaded and compiled from if the binary packages available don’t meet your needs. Ethereal can be launched by typing ethereal at the command line. 2018/9/16 主讲教师:西北大学 贺小伟

7 Exploring the Main Windows
The Summary Window provides a one-line summary for each packet. The Protocol Tree Window provides a detailed decode of the packet selected in the Summary Window. The Data View Window provides the hexadecimal (or hex) dump of the packets’ actual bytes. 2018/9/16 主讲教师:西北大学 贺小伟

8 Other Window Components
The filter bar provides a quick mechanism for filtering the packets displayed in the Summary Window. Clicking the filter bar’s Filter: button will display the Display Filter dialog box to help you construct a display filter string. 2018/9/16 主讲教师:西北大学 贺小伟

9 Exploring the Menus Most preferences can be set in the Preferences dialog box. There are context-sensitive pop-up menus available by right-clicking on the Summary Window, Protocol Tree Window, or Data View Window. Packets in the Summary Window can be color-coded for easy reading by using the Apply Color Filters dialog box. 2018/9/16 主讲教师:西北大学 贺小伟

10 Writing Capture Filters
Capture filters operate quickly and are good for limiting the number of packets captured by Ethereal. The capture filter language has keywords for comparing host names and addresses, hardware addresses, ports, and protocols. To test individual fields in a bit-field correctly, you must use the bitwise AND operator: &. 2018/9/16 主讲教师:西北大学 贺小伟

11 Writing Display Filters
Display filters are slower than capture filters, but allow you to test almost any field or protocol that Ethereal knows how to dissect. Display filter fields are typed; each type of field can hold only certain types of values. The contains operator searches for text; the matches operator searches using regular expressions. Take care when testing fields that occur multiple times in a packet; the way you might think to test these fields may be the wrong way. The Capture Filter and Display Filter dialog boxes let you save your filters. The Filter Expression dialog box lets you create display filters by pointing and clicking. 2018/9/16 主讲教师:西北大学 贺小伟

12 Writing Filter Compare expression Combination expression and && or ||
eq  ==    ip.addr== ne    !=    ip.addr!= gt  >    frame.pkt_len>10 lt   <    frame.pkt_len<10 ge   >=    frame.pkt_len>=10 le  <=    frame.pkt_len<=10 Combination expression and   && or   ||  not   ! 2018/9/16 主讲教师:西北大学 贺小伟

13 A Simple Example Run Ethereal Capture-> Options Capture-> Start
Stop Display Filter Statistics 2018/9/16 主讲教师:西北大学 贺小伟

14 典型的网络应用(Some network apps)
电子邮件( ) Web 即时信息(Instant messaging) 远程登录(Remote login) P2P文件共享(P2P file sharing) 网络游戏(Multi-user network games) 流视频(Streaming stored video clips) 因特网电话(Internet telephone) 实时视频会议(Real-time video conference) 并行计算(Massive parallel computing) 2018/9/16 主讲教师:西北大学 贺小伟

15 网络应用程序: 一些术语 进程(Process): 主机中运行中的程序.
同一主机中的进程通过进程间通信(OS)来通信 在不同主机上的进程则使用应用层协议进行通信 用户代理(User agent): 软件进程, 是介于用户(above)和网络(below)之间的接口 实现应用级协议 Web: 浏览器 OE、Foxmail 流媒体: media player 2018/9/16 主讲教师:西北大学 贺小伟

16 应用程序和应用层协议 (Applications and application-layer protocols)
应用程序: 相互通信,分布式的进程 运行在网络主机中的 “用户空间” 在应用程序间交换信息 e.g., , ftp, Web 应用层协议: 网络应用的一个“组成部分” 定义应用程序需交换的报文和所需采取的动作 使用较低层协议所提供的通信服务 (TCP, UDP) application transport network data link physical 2018/9/16 主讲教师:西北大学 贺小伟

17 应用程序体系结构(Application architectures)
客户/服务器模式(Client-server) 对等模式(Peer-to-peer (P2P)) 客户/服务器模式与对等模式相结合(Hybrid of client-server and P2P) 2018/9/16 主讲教师:西北大学 贺小伟

18 客户/服务器模式 客户端: 服务器: 由两个部分组成: 客户 和 服务器 发起与服务器的联系
客户 和 服务器 application transport network data link physical reply request 客户端: 发起与服务器的联系 一般都从服务器请求服务, 可能间歇性的与服务器相连 彼此之间并不直接通信 服务器: 向客户端提供所请求的服务 一般拥有固定的IP地址 通过集群服务提供良好的响应能力 2018/9/16 主讲教师:西北大学 贺小伟

19 对等模式(Pure P2P architecture)
同时具有客户机和服务器的两种状态 任意的终端系统可以相互通信 通信双方间歇性地通信,拥有的IP地址可能改变 例子: Bt 规模较大 管理困难 2018/9/16 主讲教师:西北大学 贺小伟

20 客户/服务器与对等混合模式 (Hybrid of client-server and P2P)
即时信息(Instant messaging) 两个用户之间聊天使用对等模式 在线侦测/集中定位: 当用户在线时在中枢服务器注册其IP地址 用户通过与中枢服务器来查找好友的IP地址 2018/9/16 主讲教师:西北大学 贺小伟

21 套接字(Sockets)与应用程序接口(API)
通过套接字发送/接收消息彼此通信 套接字的作用类似于门 发送进程把消息推出门口 发送进程依赖传输设施把消息传送到消息接收方的套接字,通过套接字把消息传送给接收进程 process TCP with buffers, variables socket host or server process TCP with buffers, variables socket host or server controlled by app developer Internet controlled by OS 应用程序接口(application programming interface, API:) 选择传输协议 确定参数 2018/9/16 主讲教师:西北大学 贺小伟

22 寻址处理(Addressing processes)
接收信息的进程必须有一个标识符 每个主机拥有一个唯一的32位的IP地址 Q: 通过主机的IP地址是否能确定相应进程? Answer: 不能, 因为在同一主机上可以同时运行许多进程. 可以通过主机的IP地址和与进程相关连的端口号来作为进程的标识符. Example port numbers: HTTP server: 80 Mail server: 25 2018/9/16 主讲教师:西北大学 贺小伟

23 应用进程需要怎样的传输服务? 可靠性 (Data loss) 带宽(Bandwidth) 实时性(Timing)
某些应用(e.g., audio)可以容忍某种程度上的数据丢失 其他应用 (e.g., 文件传输, telnet) 要求100%可靠的数据传输 带宽(Bandwidth) 某些应用(e.g., 多媒体) 对最低带宽有要求 其他应用(“弹性应用”) 则可灵活应用所能得到的带宽 实时性(Timing) 某些应用(e.g., IP 电话, 交互式游戏) 要求较低的时延 2018/9/16 主讲教师:西北大学 贺小伟

24 常用应用程序对传输功能的要求 应用程序 数据丢失 带宽 实时性 文件传输 e-mail Web 网页 实时音频/视频 存储音频/视频
交互式游戏 金融应用 数据丢失 不丢失 允许丢失 带宽 弹性 音频: 5Kb-1Mb 视频:10Kb-5Mb 同上 几 Kb/s 以上 实时性 100’s msec few secs yes and no 2018/9/16 主讲教师:西北大学 贺小伟

25 Internet的传输协议服务 UDP服务: TCP 服务: 在客户端和服务器进程之间实现“不可靠的”数据传输
面向连接: 在客户端和服务器进程之间需要建立连接 可靠传输 : 在发送和接受进程之间 流量控制: 发送数据的速度决不超过接收的速度 拥塞控制: 当网络超负荷时,束紧发送端口,减缓发送速度 不提供: 实时性, 最小带宽承诺 UDP服务: 在客户端和服务器进程之间实现“不可靠的”数据传输 不提供:连接建立, 可靠性保证,流量控制,拥塞控制,实时性, 最小带宽承诺 Q: 既生喻,何生亮? Why is there a UDP? 2018/9/16 主讲教师:西北大学 贺小伟

26 Internet应用: 应用, 传输协议 应用协议 应用 所依赖的传输协议 smtp [RFC 821] telnet [RFC 854]
http [RFC 2068] ftp [RFC 959] 专有协议 (e.g. RealNetworks) NSF (e.g., Vocaltec) 应用 远程终端访问 Web 文件传输 流媒体 远程文件服务器 IP电话 所依赖的传输协议 TCP TCP or UDP typically UDP 2018/9/16 主讲教师:西北大学 贺小伟

27 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

28 Web 和 HTTP 一些专业术语 Web页面是由对象组成的
对象可以是HTML(Hypertext Markup Language)文件,JPEG图像,Java程序,音频文件等 Web页面是由几个包含所引用文件的基本HTML(base HTML-file)文件组成 每个对象可以通过URL(Uniform Resource Locator)来编址 Example URL: host name path name 2018/9/16 主讲教师:西北大学 贺小伟

29 Web: http 协议 超文本传输协议(http: hypertext transfer protocol) 万维网应用协议
客户端/服务器模式 客户端: 浏览器请求、接收、展示 Web对象(objects) 服务器: Web 服务器发送对象对请求进行响应 http1.0: RFC 1945 http1.1: RFC 2616 http request PC running Explorer http response http request Server running NCSA Web server http response Mac running Navigator 2018/9/16 主讲教师:西北大学 贺小伟

30 http 协议 http 是 “无状态”的 http:TCP 传输服务: 客户端启动TCP连接(套接字) 到服务器, 端口 80
服务器不保留任何访问过的请求信息 传输服务: 客户端启动TCP连接(套接字) 到服务器, 端口 80 服务器接受来自客户端的 TCP 连接 http 报文(应用层协议报文) 在浏览器 (http client) 和Web服务器(http server)之间进行交换 关闭TCP 连接 小评论 保留状态的协议很复杂哟! 过去的历史 (状态) 需要保留 一旦浏览器/服务器崩溃, 它们各自的状态视图就会发生分歧,还需要重新核对 2018/9/16 主讲教师:西北大学 贺小伟

31 (该网页包含文本并引用了10个jpeg 图片)
非持续连接http举例 假设用户键入的 URL (该网页包含文本并引用了10个jpeg 图片) 1a. http 客户端启动 TCP 连接到 服务器 (进程). Port 80 是 http服务器的默认端口. 1b. 在 上的http 服务器在 port 80 等待 TCP 的连接请求. “接受” 连接并通知客户端 2. http客户端发送 http 请求报文 (包括URL) 进入 TCP 连接套接字(socket) 3. http 服务器接收到请求报文, 形成响应报文 (包含了所请求的对象, nic/index.html), 将报文送入套接字(socket) time 2018/9/16 主讲教师:西北大学 贺小伟

32 非持续连接http举例(续) 4. http 服务器关闭 TCP 连接.
5. http 客户端接收到了包含html文件的响应报文.分析 html 文件, 发现 10 个引用的 jpeg 对象 time 6. 对10 jpeg objects 逐个重复1-5 步 2018/9/16 主讲教师:西北大学 贺小伟

33 响应时间模型(Response time modeling)
往返时间 (round-trip time, RRT): 一个分组从客户机到服务器,再回到客户机所花费的时间. 响应时间: 发起TCP连接(“三次握手”前两步)耗费一个RTT http请求(“三次握手”后一步)响应耗费一个 RTT 文件传输时间(transmit time) total = 2RTT+transmit time time to transmit file initiate TCP connection RTT request received time 2018/9/16 主讲教师:西北大学 贺小伟

34 持久连接http 非持久连接的问题: 无流水线的持久连接: 每个对象需要2 RTTs的延迟 客户端只有当先前的响应收到后才能产生一个新的请求
对于每个TCP连接OS必须分配主机的资源 浏览器通常使用并行的TCP连接获取引用对象 持久连接 server leaves connection open after sending response subsequent HTTP messages between same client/server are sent over connection 无流水线的持久连接: 客户端只有当先前的响应收到后才能产生一个新的请求 对于每个引用对象需要1个RTT的延迟 有流水线的持久连接: 在HTTP/1.1中定义 客户端可以同时发送请求和接收引用对象 对于所有的引用对象只需一个RTT的延迟 2018/9/16 主讲教师:西北大学 贺小伟

35 http连接(非持续和持续连接) 持续连接 非持续连接 (Persistent) (Non-persistent)
http/1.1的默认设置 在同一TCP 连接上: 服务器分析请求、响应请求,分析新的请求、.. 客户端一旦下载到了基本的html文件( base HTML )马上发送对所有引用对象的请求. 较少的 RTTs, 较少的慢启动. 非持续连接 (Non-persistent) http/1.0: 服务器分析请求、响应、关闭 TCP 连接 取对象需要2 RTTs TCP 连接 对象请求/传送 每次传送都要受到TCP连接初始化时的慢启动影响 许多浏览器同时打开多个并行的连接来改善性能 2018/9/16 主讲教师:西北大学 贺小伟

36 http 报文格式: request(请求)
http报文的两种类型: request, response http 请求报文: ASCII (可读格式) 请求行 (GET, POST, HEAD 命令) GET /photo/bg.gif HTTP/1.1 Host: User-agent: Mozilla/4.0 Accept: */* Accept-language:zh-cn (额外的 carriage return, line feed) 首部 诸行 回车、换行表示 报文结束 2018/9/16 主讲教师:西北大学 贺小伟

37 2018/9/16 主讲教师:西北大学 贺小伟

38 http 请求报文: 一般格式 2018/9/16 主讲教师:西北大学 贺小伟

39 方法类型(Method types) HTTP/1.0 GET POST HEAD HTTP/1.1 GET, POST, HEAD PUT
asks server to leave requested object out of response HTTP/1.1 GET, POST, HEAD PUT uploads file in entity body to path specified in URL field DELETE deletes file specified in the URL field 2018/9/16 主讲教师:西北大学 贺小伟

40 http 报文格式: response(响应)
状态行 (协议状态码 状态短语) HTTP/ Not Modified Date: Thu, 02 Mar :00:15 GMT Server: Apache/ (Unix) Last-Modified: Mon, 22 Jun 2004 …... Content-Length: 6821 Content-Type: text/html data data data data data ... 首部 诸行 数据, e.g., 被请求的html文件 2018/9/16 主讲教师:西北大学 贺小伟

41 2018/9/16 主讲教师:西北大学 贺小伟

42 http 响应状态码 位于(服务器->客户端)响应报文的第一行. 样例: 200 OK 请求成功, 被请求的对象在报文中
301 Moved Permanently 被请求的对象被移动过, 新的位置在报文中有说明 (Location:) 400 Bad Request 服务器不懂请求报文 404 Not Found 服务器上找不到请求的对象 505 HTTP Version Not Supported 2018/9/16 主讲教师:西北大学 贺小伟

43 2018/9/16 主讲教师:西北大学 贺小伟

44 2018/9/16 主讲教师:西北大学 贺小伟

45 2018/9/16 主讲教师:西北大学 贺小伟

46 2018/9/16 主讲教师:西北大学 贺小伟

47 GET /photo/bg.gif HTTP/1.0
1. 用Telnet 连接测试用的服务器: $telnet 打开 TCP 连接到 port 80 (默认的http 服务器端口) 位于 后续键入的内容将发送到 的 80 号端口 2. 键入一条 http请求报文: 将该指令键入后 (按两次回车键), 就将此最短之 (但是完整的) GET 请求发到了 http 服务器 GET /photo/bg.gif HTTP/1.0 3. 请注意观察http服务器发回的响应报文! 2018/9/16 主讲教师:西北大学 贺小伟

48 用户-服务器的交互: 认证(authentication)
client server 认证:控制对服务器内容的访问 一般通过用户名, 口令进行 无状态: 客户端必须在每次请求前进行认证 authorization: 就是要求在每个请求报文中提交认证的首部行 如果客户端没有提交 authorization: 首部行, 服务器将拒绝访问, 只是在响应报文首部中发送 WWW authenticate: 普通 http 请求报文 401: 认证要求 WWW authenticate: 普通 http 请求报文 + Authorization: <cred> 普通 http响应报文 普通 http 请求报文 + Authorization: <cred> time 普通 http响应报文 2018/9/16 主讲教师:西北大学 贺小伟

49 用户-服务器的交互: Cookies 服务器产生一个 # , 服务器认识这个 #, 以备不时之需:
client server 服务器产生一个 # , 服务器认识这个 #, 以备不时之需: 认证 记忆用户的前序访问, 先前的选择 服务器在响应报文中发送 “cookie” 给客户端 Set-cookie: 客户端可以在后继的请求中发送“cookie” cookie: 普通 http 请求报文 普通 http响应报文+ Set-cookie: # 普通 http 请求报文 cookie: # cookie- 特定的 普通 http响应报文 普通 http 请求报文 cookie: # cookie- 特定的 普通 http响应报文 2018/9/16 主讲教师:西北大学 贺小伟

50 User-server state: cookies
Many major Web sites use cookies Four components: 1) cookie header line in the HTTP response message 2) cookie header line in HTTP request message 3) cookie file kept on user’s host and managed by user’s browser 4) back-end database at Web site Example: Susan access Internet always from same PC She visits a specific e-commerce site for first time When initial HTTP requests arrives at site, site creates a unique ID and creates an entry in backend database for ID 2018/9/16 主讲教师:西北大学 贺小伟

51 Cookies: keeping “state” (cont.)
client server usual http request msg usual http response + Set-cookie: 1678 cookie: 1678 usual http response msg cookie- specific action spectific Cookie file ebay: 8734 server creates ID 1678 for user entry in backend database Cookie file amazon: 1678 ebay: 8734 access access one week later: Cookie file amazon: 1678 ebay: 8734 2018/9/16 主讲教师:西北大学 贺小伟

52 Cookies (continued) What cookies can bring: authorization
aside What cookies can bring: authorization shopping carts recommendations user session state (Web ) Cookies and privacy: cookies permit sites to learn a lot about you you may supply name and to sites search engines use redirection & cookies to learn yet more advertising companies obtain info across sites 2018/9/16 主讲教师:西北大学 贺小伟

53 Conditional GET: 客户端缓存机制
目的: 如果客户端缓存了最新的请求对象,则服务器不必重复发送 客户端: 在http请求报文中声明所缓存拷贝的生成日期 If-modified-since: <date> 服务器: 如果客户端缓存的拷贝是最新的,则在响应报文中不发请求的对象: HTTP/ Not Modified client server http请求报文 If-modified-since: <date> 对象未经修改 http响应报文 HTTP/1.0 304 Not Modified http请求报文 If-modified-since: <date> 对象已 经修改 http响应报文 HTTP/ OK <data> 2018/9/16 主讲教师:西北大学 贺小伟

54 Web 缓存:代理服务器 (proxy server)
目的: 满足客户端的请求而无需烦扰原始服务器 用户设置浏览器: Web访问经由代理服务器 客户端发送所有的http请求到代理服务器 代理服务器保存了请求的对象: 代理服务器返回请求的对象 否则代理服务器从原始服务器请求对象,再将其返回给客户端 origin server Proxy server http request http request client http response http response http request http response client origin server 2018/9/16 主讲教师:西北大学 贺小伟

55 More about Web caching Cache acts as both client and server
Typically cache is installed by ISP (university, company, residential ISP) Why Web caching? Reduce response time for client request. Reduce traffic on an institution’s access link. Internet dense with caches enables “poor” content providers to effectively deliver content (but so does P2P file sharing) 2018/9/16 主讲教师:西北大学 贺小伟

56 Caching example Assumptions Consequences origin servers
average object size = 100,000 bits avg. request rate from institution’s browsers to origin servers = 15/sec delay from institutional router to any origin server and back to router = 2 sec Consequences utilization on LAN = 15% utilization on access link = 100% total delay = Internet delay + access delay + LAN delay = 2 sec + minutes + milliseconds origin servers public Internet 1.5 Mbps access link institutional network 10 Mbps LAN institutional cache 2018/9/16 主讲教师:西北大学 贺小伟

57 Caching example (cont)
origin servers Possible solution increase bandwidth of access link to, say, 10 Mbps Consequences utilization on LAN = 15% utilization on access link = 15% Total delay = Internet delay + access delay + LAN delay = 2 sec + msecs + msecs often a costly upgrade public Internet 10 Mbps access link institutional network 10 Mbps LAN institutional cache 2018/9/16 主讲教师:西北大学 贺小伟

58 Caching example (cont)
origin servers Install cache suppose hit rate is .4 Consequence 40% requests will be satisfied almost immediately 60% requests satisfied by origin server utilization of access link reduced to 60%, resulting in negligible delays (say 10 msec) total avg delay = Internet delay + access delay + LAN delay = .6*(2.01) secs + milliseconds < 1.4 secs public Internet 1.5 Mbps access link institutional network 10 Mbps LAN institutional cache 2018/9/16 主讲教师:西北大学 贺小伟

59 Proxy Server : Function
共享网络(Sharing Network) 访问代理(Access Agent) 攻击防卫(Defense Attack) 突破限制(Break Restrict) 掩藏身份(Hide Status) 提高速度(Improve Speed) 2018/9/16 主讲教师:西北大学 贺小伟

60 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

61 ftp: 文件传输协议(file transfer protocol)
server user interface client local file system remote file at host 传输文件往来与远程主机 客户端/服务器模式 客户端: 启动传输 (无论与往来远程主机) 服务器: 远程主机 ftp: RFC 959 ftp 服务器: 端口 21 2018/9/16 主讲教师:西北大学 贺小伟

62 FTP Model 2018/9/16 主讲教师:西北大学 贺小伟

63 ftp: 控制与数据信息分离的连接 (separate control, data connections)
client server TCP control connection port 21 TCP data connection port 20 ftp客户端与服务器的21端口联系, 使用TCP作为传输协议发起一个连接(ftp client contacts ftp server at port 21, specifying TCP as transport protocol) 在此控制连接上客户端获得授权(Client obtains authorization over control connection) 在此控制连接上客户端通过发送命令来浏览远程主机的目录(Client browses remote directory by sending commands over control connection.) 服务器接收到文件传输的命令时,就会打开一个与客户端的TCP数据连接(When server receives a command for a file transfer, the server opens a TCP data connection to client) 每个文件传输结束后服务器就会关闭连接(After transferring one file, server closes connection.) 再传输另外的文件就要打开第二个数据连接(Server opens a second TCP data connection to transfer another file.) 控制连接:”带外”(Control connection: “out of band”) ftp服务器维持”状态”: 当前目录,先前的认证信息等(FTP server maintains “state”: current directory, earlier authentication) 2018/9/16 主讲教师:西北大学 贺小伟

64 Overview of an FTP Transfer
2018/9/16 主讲教师:西北大学 贺小伟

65 ftp 命令, 响应(commands, responses)
样例命令: 在控制通道上传送的ASCII文本 USER username(登录) PASS password(登录) LIST (返回当前目录中的文件列表) RETR filename (取 (gets) 文件) STOR filename (存(puts) 文件到远程主机) 返回码样例 状态码和短语 (同 http) 331 Username OK, password required 125 data connection already open; transfer starting 425 Can’t open data connection 452 Error writing file 2018/9/16 主讲教师:西北大学 贺小伟

66 Options FTP assumes files are objects in mass storage
Share some properties regardless of machine Files uniquely identified by symbolic names Files have owners and protection against unauthorized access Files may be created, read from (copied from), written into, or deleted (within protection rules) To support specific computers and operating systems, FTP can negotiate options in three dimensions Datatype, file type, and transfer mode Systems programmer on each system determines How particular file can be mapped to standard file type Using one of standard data types Transferred using standard mode 2018/9/16 主讲教师:西北大学 贺小伟

67 Data Types ASCII, EBCDIC(Extended Binary Coded Decimal Interchange Code扩充的二进制编码的十进制交换码:使用8个二进制数字来代表多达256种中的每一种字母数字特征的标准码 ), image, and logical byte size Text files normally stored as character string 8-bit ASCII on most machines If ASCII option used, no character code conversion required at either end in most cases EBCDIC appropriate if both machines IBM hosts ASCII or EBCDIC files may have further line or page printer specification Nonprint: Suitable for files not destined for a line printer Telnet formatting: Embedded control characters Character control formatting: Formatting conventions from FORTRAN  Image transfer is bit-by-bit replication of file from the source machine on the target machine Logical byte size type used when data unit size must be preserved Specifies byte size (need not be 8 bits) 2018/9/16 主讲教师:西北大学 贺小伟

68 File Types File structure, record structure, and page structure
To promote convenient, efficient interface to file system Not possible to address idiosyncrasies of all operating systems  File structure String of bytes (defined by data type option) that terminates in an end of file marker Most transfers use this type Record structure Sequence of records Causes transmission of individual records, separated by standard End of Record marker for specified data type Page structure For files not stored contiguously on disk Page structure needs to be preserved 2018/9/16 主讲教师:西北大学 贺小伟

69 FTP File Types 2018/9/16 主讲教师:西北大学 贺小伟

70 Transmission Modes–Stream Mode
Optimise use of network Stream mode (default) Raw data sent Least computational burden on user and server systems No restriction on file type Record-structure files, 2-byte control code for EOR and EOF 2018/9/16 主讲教师:西北大学 贺小伟

71 Transmission Modes–Block Mode
Provides for restarting failed or interrupted transfer Source encapsulates data into blocks Block begins with two field header Descriptor may indicate zero of more of: Last block in record: If record structure used each record consists of one or more blocks Last block in file Suspect data: data may contain errors Not intended for error control within FTP Allows sites to exchanging data (e.g., seismic or weather) to send and receive all data despite local errors (such as "magnetic tape read errors"), but showcertain portions are suspect Restart marker: marks checkpoint in data stream Receiver marks corresponding position and returns this May restart from last correctly received marker Count field indicates total length of data block in bytes 2018/9/16 主讲教师:西北大学 贺小伟

72 Transmission Modes–Compressed Mode
Allows source to squeeze sequences of same character into a shorter coded sequence Uncompressed data Replicated byte: Up to 63 of specified bytes Filler string: Up to 63 of filler characters inserted at destination Escape sequence: Byte of all zeros followed by descriptor code byte, as in block mode 2018/9/16 主讲教师:西北大学 贺小伟

73 FTP Transmission Mode Formats
2018/9/16 主讲教师:西北大学 贺小伟

74 2018/9/16 主讲教师:西北大学 贺小伟

75 2018/9/16 主讲教师:西北大学 贺小伟

76 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

77 电子邮件(Electronic Mail)
外发报文队列 用户邮箱 mail server user agent SMTP 四个重要组件: 用户代理(user agents) 邮件服务器(mail servers) 简单邮件传输协议: smtp(simple message transfer protocol) 邮局协议:pop(post office protocol)、因特网邮件访 问协议:IMAP(Internet Mail Access Protocol) 写作, 编辑, 阅读邮件报文 e.g., Foxmail, OE, elm, Netscape Messenger 外发, 接收的报文存储在邮件服务器中 2018/9/16 主讲教师:西北大学 贺小伟

78 电子邮件:邮件服务器(Mail Servers)
邮箱 包含了收到的用户邮件 (尚未被阅读) 报文队列包含了外发的邮件报文 smtp协议用在邮件服务器之间发送邮件 客户端: 将邮件发送到邮件服务器 “服务器”: 接收和转发邮件 mail server user agent SMTP 2018/9/16 主讲教师:西北大学 贺小伟

79 电子邮件: smtp [RFC 821] 使用 tcp 可靠的传送邮件报文, 端口25 直接传输: 发送服务器到接收服务器 传输的三个阶段
握手(打招呼) 报文传输 结束 命令/响应交互 命令: ASCII文本 响应: 状态码和短语 邮件报文必须使用7-bit ASCII表示 2018/9/16 主讲教师:西北大学 贺小伟

80 Scenario: Alice sends message to Bob
1) Alice uses UA to compose message and “to” 2) Alice’s UA sends message to her mail server; message placed in message queue 3) Client side of SMTP opens TCP connection with Bob’s mail server 4) SMTP client sends Alice’s message over the TCP connection 5) Bob’s mail server places the message in Bob’s mailbox 6) Bob invokes his user agent to read message mail server mail server 1 user agent user agent 2 3 6 4 5 2018/9/16 主讲教师:西北大学 贺小伟

81 SMTP Sender Takes message from queue
Transmits to proper destination host Via SMTP transaction Over one or more TCP connections to port 25 Host may have multiple senders active Host should be able to create receivers on demand When delivery complete, sender deletes destination from list for that message When all destinations processed, message is deleted 2018/9/16 主讲教师:西北大学 贺小伟

82 Optimization If message destined for multiple users on a given host, it is sent only once Delivery to users handled at destination host If multiple messages ready for given host, a single TCP connection can be used Saves overhead of setting up and dropping connection 2018/9/16 主讲教师:西北大学 贺小伟

83 Possible Errors Host unreachable Host out of operation
TCP connection fail during transfer Sender can re-queue mail Give up after a period Faulty destination address User error Target user changed address Redirect if possible Inform user if not 2018/9/16 主讲教师:西北大学 贺小伟

84 SMTP Protocol - Reliability
Used to transfer messages from sender to receiver over TCP connection Attempts to provide reliable service No guarantee to recover lost messages No end to end acknowledgement to originator Error indication delivery not guaranteed Generally considered reliable 2018/9/16 主讲教师:西北大学 贺小伟

85 SMTP Receiver Accepts arriving message
Places in user mailbox or copies to outgoing queue for forwarding Receiver must: Verify local mail destinations Deal with errors Transmission Lack of disk space Sender responsible for message until receiver confirm complete transfer Indicates mail has arrived at host, not user 2018/9/16 主讲教师:西北大学 贺小伟

86 SMTP Forwarding Mostly direct transfer from sender host to receiver host May go through intermediate machine via forwarding capability Sender can specify route Target user may have moved 2018/9/16 主讲教师:西北大学 贺小伟

87 Conversation SMTP limited to conversation between sender and receiver
Main function is to transfer messages Rest of mail handling beyond scope of SMTP May differ between systems 2018/9/16 主讲教师:西北大学 贺小伟

88 SMTP Mail Flow 2018/9/16 主讲教师:西北大学 贺小伟

89 SMTP System Overview Commands and responses between sender and receiver Initiative with sender Establishes TCP connection Sender sends commands to receiver e.g. HELO<SP><domain><CRLF> Each command generates exactly one reply e.g. 250 requested mail action ok; completed 2018/9/16 主讲教师:西北大学 贺小伟

90 SMTP Replies Leading digit indicates category
Positive completion reply (2xx) Positive intermediate reply (3xx) Transient negative completion reply (4xx) Permanent negative completion reply (5xx) 2018/9/16 主讲教师:西北大学 贺小伟

91 Operation Phases Connection setup Exchange of command-response pairs
Connection termination 2018/9/16 主讲教师:西北大学 贺小伟

92 Connection Setup Sender opens TCP connection with receiver
Once connected, receiver identifies itself 220 <domain> service ready Sender identifies itself HELO Receiver accepts sender’s identification 250 OK If mail service not available, step 2 above becomes: 421 service not available 2018/9/16 主讲教师:西北大学 贺小伟

93 Mail Transfer Sender may send one or more messages to receiver
MAIL command identifies originator Gives reverse path to used for error reporting Receiver returns 250 OK or appropriate fail/error message One or more RCPT commands identifies recipients for the message Separate reply for each recipient DATA command transfers message text End of message indicated by line containing just period (.) 2018/9/16 主讲教师:西北大学 贺小伟

94 Closing Connection Two steps Sender sends QUIT and waits for reply
Then initiate TCP close operation Receiver initiates TCP close after sending reply to QUIT 2018/9/16 主讲教师:西北大学 贺小伟

95 smtp交互样例(用telnet) telnet 127.0.0.1 25 S: 220 test.com C: HELO test.com
S: 250 Welcome , pleased to meet you C: MAIL FROM: S: 250 ok C: RCPT TO: > S: 250 ok its for C: DATA S: 354 ok, send it; end with <CRLF>.<CRLF> C: Hi, I am in NWU now, Where are you? C: . S: 250 Message queued C: QUIT S: 221 Goodbye 2018/9/16 主讲教师:西北大学 贺小伟

96 smtp: 评述 与 http的比较: smtp 使用持续连接 smtp 要求报文 (首部 & 信体) 全部使用 7-bit ASCII码
某些代码组合不允许出现在报文中 (e.g., CRLF.CRLF). 此类数据必须进行编码 (通常使用 base-64 或 quoted printable) smtp 服务器用 CRLF.CRLF 表示邮件报文的结束 与 http的比较: http: pull(拉) push(推) 都使用 ASCII 命令/响应交互, 状态码 http: 每个对象分装在各自的响应报文中 smtp:多个对象在一个多分部的报文中传送 2018/9/16 主讲教师:西北大学 贺小伟

97 2018/9/16 主讲教师:西北大学 贺小伟

98 邮件报文格式(Mail message format)
SMTP: 交换邮件报文的协议 RFC 822: 文本报文格式标准: Message viewed as having envelope and contents Envelope contains information required to transmit and deliver message Message is sequence of lines of text Uses general memo framework Header usually keyword followed by colon followed by arguments header 空行 body 2018/9/16 主讲教师:西北大学 贺小伟

99 Example Message Date:Tue, 16 Jan 1996 10:37:17 (EST)
From: “William Stallings” Subject:The syntax of RFC 822 To: Cc: This is the main text, delimited from the header by a blank line. 2018/9/16 主讲教师:西北大学 贺小伟

100 Multipurpose Internet Mail Extension (MIME)
Extension to RFC822 SMTP can not transmit executables Uuencode and other schemes are available Not standardized Can not transmit text including international characters (e.g. â, å, ä, è, é, ê, ë) Need 8 bit ASCII Servers may reject mail over certain size Some SMTP implementations do not adhere to standard CRLF, truncate or wrap long lines, removal of white space, etc. 2018/9/16 主讲教师:西北大学 贺小伟

101 邮件消息格式:多用途因特网邮件扩展(Message format: Multipurpose Internet Mail Extensions)
MIME: 多用途因特网邮件扩展(Multipurpose Internet Mail Extensions), RFC 2045, 2056 在报文首部附加额外的信息声明 MIME 内容类型 From: To: Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data ..... ......base64 encoded data 多媒体类型,子类型, 参数声明 数据编码方法 MIME 版本 编码后的数据 2018/9/16 主讲教师:西北大学 贺小伟

102 MIME 类型声明 Content-Type: type/subtype; parameters
Video 子类型样例: mpeg, quicktime Application 需使用其他阅读器的数据 子类型样例: msword, octet-stream Text 子类型样例: plain, html Image 子类型样例: jpeg, gif Audio 子类型样例: basic (8-bit mu-law encoded), 32kadpcm (32 kbps coding) 2018/9/16 主讲教师:西北大学 贺小伟

103 MIME Multipart Type From: alice@crepes.fr To: bob@hamburger.edu
Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Type: multipart/mixed; boundary= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain Dear Bob, Please find a picture of a crepe. Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data ..... ......base64 encoded data 2018/9/16 主讲教师:西北大学 贺小伟

104 2018/9/16 主讲教师:西北大学 贺小伟

105 The Received Message Received: from giant.minigui.org (unknown [ ]) by mx17 (Cor ) with SMTP id wKjR4hOAUQYLRhxEaUL+BQ==.28448S2; Sun, 19 Mar :40: (CST) Received: from giant.minigui.org (localhost [ ]) by giant.minigui.org (Postfix) with ESMTP id D ; Fri, 17 Mar :33: (CST) X-Original-To: Delivered-To: Received: from bzbook16 (unknown [ ]) by giant.minigui.org (Postfix) with ESMTP id C17AC18FAC for Fri, 17 Mar :32: (CST) Message-ID: From: =?UTF-8?B?5p6X5qCp?= To: =?UTF-8?B?6YKu5Lu25YiX6KGo?= Date: Fri, 17 Mar :39: MIME-Version: 1.0 2018/9/16 主讲教师:西北大学 贺小伟

106 Mail access protocols SMTP: 发送/存储 到接收方的服务器 邮件访问协议: 从服务器中取信 SMTP SMTP
POP3 or IMAP user agent user agent sender’s mail server receiver’s mail server SMTP: 发送/存储 到接收方的服务器 邮件访问协议: 从服务器中取信 POP: Post Office Protocol [RFC 1939] 认证 (agent <-->server) 和下载 IMAP: Internet Mail Access Protocol [RFC 1730] 更多功能(更为复杂) 在服务器中操作存储在那里的报文 HTTP: Hotmail , Yahoo! Mail, 163.com,etc. 2018/9/16 主讲教师:西北大学 贺小伟

107 POP3 协议 认证阶段 交互阶段, 客户端: 客户端命令: user: 用户名 pass: 口令 服务器响应 +OK C: list
S: +OK POP3 server ready C: user alice S: +OK C: pass hungry S: +OK user successfully logged on 认证阶段 客户端命令: user: 用户名 pass: 口令 服务器响应 +OK -ERR 交互阶段, 客户端: list: 列出报文号码 retr: 用报文号码取信 dele:用报文号码删信 quit C: list S: 1 498 S: 2 912 S: . C: retr 1 S: <message 1 contents> C: dele 1 C: retr 2 C: dele 2 C: quit S: +OK POP3 server signing off 2018/9/16 主讲教师:西北大学 贺小伟

108 POP3 (more) and IMAP More about POP3
Previous example uses “download and delete” mode. Bob cannot re-read if he changes client “Download-and-keep”: copies of messages on different clients POP3 is stateless across sessions IMAP(Internet Mail Access Protocol, Internet邮件访问协议) Keep all messages in one place: the server Allows user to organize messages in folders IMAP keeps user state across sessions: names of folders and mappings between message IDs and folder name 2018/9/16 主讲教师:西北大学 贺小伟

109 自测 pop3交互: $telnet 127.0.0.1 110 见到+OK POP3 server ready 响应后
键入 user, pass, list, retr, quit 命令 上述过程可以不使用用户代理,就能察看邮箱中的信件。 2018/9/16 主讲教师:西北大学 贺小伟

110 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

111 DNS: 域名系统(Domain Name System)
分布式数据库:由许多域名服务器按层次构成 应用层协议: 主机、路由器、域名服务器互相通信进行域名解析 (地址/域名翻译) 注意: 因特网之核心功能, 应用层之协议 网络“边缘”上之复杂实体 自然人: 诸多定义: 身份证, 姓名, 护照 # 因特网主机, 路由器: IP 地址 (32 bit) – 用于数据报寻址 “域名”, e.g., – 帮助记忆 Q: IP 地址和域名之间如何映射(转换) ? 2018/9/16 主讲教师:西北大学 贺小伟

112 DNS name servers 没有服务器能够保存所有 Name-to-IP 地址的映射 为什么不搞集中的DNS? 单点失败的问题
数据的流通量 远程集中式的数据库 维护问题 难以与时俱进,跟不上发展! 没有服务器能够保存所有 Name-to-IP 地址的映射 本地域名服务器: 每个 ISP, 企业可拥有 本地(默认) 域名服务器 主机的 DNS 查询首先发往本地域名服务器 授权域名服务器: 每台主机必须在授权服务器上注册登记 可完成域名/地址的转换 2018/9/16 主讲教师:西北大学 贺小伟

113 DNS: 根域名服务器 当本地域名服务器不能解析时,向根域名服务器查询 根域名服务器: 如果域名映射未知,则向授权域名服务器查询 取得映射
将映射返回本地域名服务器 b USC-ISI Marina del Rey, CA l ICANN Marina del Rey, CA e NASA Mt View, CA f Internet Software C. Palo Alto, CA i NORDUnet Stockholm k RIPE London m WIDE Tokyo a NSI Herndon, VA c PSInet Herndon, VA d U Maryland College Park, MD g DISA Vienna, VA h ARL Aberdeen, MD j NSI (TBD) Herndon, VA 遍布世界各地的13个根域名服务器 2018/9/16 主讲教师:西北大学 贺小伟

114 authorititive name server
简单 DNS 举例 root name server 主机 要求 gaia.cs.umass.edu的IP地址 1. 联系本地域名服务器, 2.如有必要 会联系根域名服务器 3.如有必要根域名服务器会联系授权域名服务器, dns.umass.edu 2 4 3 5 local name server authorititive name server dns.umass.edu 1 6 requesting host gaia.cs.umass.edu 2018/9/16 主讲教师:西北大学 贺小伟

115 DNS 举例 根域名服务器: 可能不知道授权域名服务器的地址 可能知道中介域名服务器: 由它负责联系授权域名服务器
root name server 2 6 根域名服务器: 可能不知道授权域名服务器的地址 可能知道中介域名服务器: 由它负责联系授权域名服务器 7 3 local name server intermediate name server dns.umass.edu 4 5 1 8 authoritative name server dns.cs.umass.edu requesting host ctec.xjtu.edu.cn gaia.cs.umass.edu 2018/9/16 主讲教师:西北大学 贺小伟

116 DNS: 迭代查询 递归查询: 迭代查询: 对根域名服务器造成工作负担 如何减负? 被查询的服务器直接把可查询的服务器地址报回
root name server 递归查询: 对根域名服务器造成工作负担 如何减负? 迭代查询: 被查询的服务器直接把可查询的服务器地址报回 “不懂这个域名, 但可以从这个服务器查到” iterated query 2 3 4 7 local name server dns.eurecom.fr intermediate name server dns.umass.edu 5 6 1 8 authoritative name server dns.cs.umass.edu requesting host surf.eurecom.fr gaia.cs.umass.edu 2018/9/16 主讲教师:西北大学 贺小伟

117 authoritative DNS server
Example root DNS server 2 Host at cis.poly.edu wants IP address for gaia.cs.umass.edu 3 TLD DNS server 4 5 local DNS server dns.poly.edu 7 6 1 8 authoritative DNS server dns.cs.umass.edu requesting host cis.poly.edu gaia.cs.umass.edu 2018/9/16 主讲教师:西北大学 贺小伟

118 DNS: 缓存和更新纪录 一旦 (任何) 域名服务器得知了某个映射, 就将其 缓存 在一定的时间间隔后缓存的条目将会过期(自动消除)
更新/通知 机制由 IETF负责设计 RFC 2136 2018/9/16 主讲教师:西北大学 贺小伟

119 DNS Why not centralize DNS? DNS services
single point of failure traffic volume distant centralized database maintenance doesn’t scale! DNS services Hostname to IP address translation Host aliasing Canonical and alias names Mail server aliasing Load distribution Replicated Web servers: set of IP addresses for one canonical name 2018/9/16 主讲教师:西北大学 贺小伟

120 Distributed, Hierarchical Database
Root DNS Servers com DNS servers org DNS servers edu DNS servers poly.edu DNS servers umass.edu yahoo.com amazon.com pbs.org Client wants IP for 1st approx: Client queries a root server to find com DNS server Client queries com DNS server to get amazon.com DNS server Client queries amazon.com DNS server to get IP address for 2018/9/16 主讲教师:西北大学 贺小伟

121 TLD and Authoritative Servers
Top-level domain (TLD) servers: responsible for com, org, net, edu, etc, and all top-level country domains uk, fr, ca, jp. Network solutions maintains servers for com TLD Educause for edu TLD Authoritative DNS servers: organization’s DNS servers, providing authoritative hostname to IP mappings for organization’s servers (e.g., Web and mail). Can be maintained by organization or service provider 2018/9/16 主讲教师:西北大学 贺小伟

122 Local Name Server Does not strictly belong to hierarchy
Each ISP (residential ISP, company, university) has one. Also called “default name server” When a host makes a DNS query, query is sent to its local DNS server Acts as a proxy, forwards query into hierarchy. 2018/9/16 主讲教师:西北大学 贺小伟

123 RR format: (name, value, type, ttl)
DNS records DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) Type=A name is hostname value is IP address Type=CNAME name is alias name for some “cannonical” (the real) name is really servereast.backup2.ibm.com value is cannonical name Type=NS name is domain (e.g. foo.com) value is IP address of authoritative name server for this domain Type=MX value is name of mailserver associated with name 2018/9/16 主讲教师:西北大学 贺小伟

124 DNS 协议, 报文(protocol, message)
报文首部 identification: 16 bit # 用于查询, 应答报文使用同样的 # flags: 查询 或 应答 希望递归 可以递归 授权应答 2018/9/16 主讲教师:西北大学 贺小伟

125 DNS 协议, 报文(protocol, message)
Name, type fields 查询报文 RRs 响应 来自授权服务器的纪录 其他“帮助”信息 2018/9/16 主讲教师:西北大学 贺小伟

126 DNS: caching and updating records
once (any) name server learns mapping, it caches mapping cache entries timeout (disappear) after some time TLD servers typically cached in local name servers Thus root name servers not often visited update/notify mechanisms under design by IETF RFC 2136 2018/9/16 主讲教师:西北大学 贺小伟

127 Inserting records into DNS
Example: just created startup “Network Utopia” Register name networkuptopia.com at a registrar (e.g., Network Solutions) Need to provide registrar with names and IP addresses of your authoritative name server (primary and secondary) Registrar inserts two RRs into the com TLD server: (networkutopia.com, dns1.networkutopia.com, NS) (dns1.networkutopia.com, , A) Put in authoritative server Type A record for and Type MX record for networkutopia.com How do people get the IP address of your Web site? 2018/9/16 主讲教师:西北大学 贺小伟

128 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

129 P2P file sharing: Example
Alice runs P2P client application on her computer Intermittently connects to Internet; gets new IP address for each connection Asks for “Hey Jude” App. displays other peers that have copy of Hey Jude. Alice chooses one of the peers, Bob. File is copied from Bob’s PC to Alice’s notebook: HTTP While Alice downloads, other users upload from Alice. Alice’s peer is both a client and a transient server. All peers are servers = highly scalable! 2018/9/16 主讲教师:西北大学 贺小伟

130 P2P: centralized directory
directory server peers Alice Bob 1 2 3 original “Napster” design 1) when peer connects, it informs central server: IP address content 2) Alice queries for “Hey Jude” 3) Alice requests file from Bob 2018/9/16 主讲教师:西北大学 贺小伟

131 problems with centralized directory
file transfer is decentralized, but locating content is highly decentralized Single point of failure Performance bottleneck Copyright infringement 2018/9/16 主讲教师:西北大学 贺小伟

132 Query flooding fully distributed no central server Scalability:
File transfer: HTTP Query QueryHit fully distributed no central server Scalability: limited scope flooding 2018/9/16 主讲教师:西北大学 贺小伟

133 Exploiting heterogeneity
Each peer is either a group leader or assigned to a group leader. TCP connection between peer and its group leader. TCP connections between some pairs of group leaders. Group leader tracks the content in all its children. 2018/9/16 主讲教师:西北大学 贺小伟

134 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

135 Socket programming Goal: learn how to build client/server application that communicate using sockets a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented 2018/9/16 主讲教师:西北大学 贺小伟

136 Socket-programming using TCP
Socket: a door between application process and end-end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by application developer process TCP with buffers, variables socket process TCP with buffers, variables socket controlled by operating system controlled by operating system internet host or server host or server 2018/9/16 主讲教师:西北大学 贺小伟

137 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: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk with multiple clients source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint 2018/9/16 主讲教师:西北大学 贺小伟

138 Stream jargon A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, eg, keyboard or socket. An output stream is attached to an output source, eg, monitor or socket. 2018/9/16 主讲教师:西北大学 贺小伟

139 Socket programming with TCP
Example client-server app: 1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket (inFromServer stream) Client process client TCP socket 2018/9/16 主讲教师:西北大学 贺小伟

140 Client/server socket interaction: TCP
Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP connection setup close connectionSocket read reply from clientSocket create socket, connect to hostid, port=x clientSocket = Socket() wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to 2018/9/16 主讲教师:西北大学 贺小伟

141 Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket 2018/9/16 主讲教师:西北大学 贺小伟

142 Example: Java client (TCP), cont.
Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } Send line to server Read line from server 2018/9/16 主讲教师:西北大学 贺小伟

143 Example: Java server (TCP)
import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 2018/9/16 主讲教师:西北大学 贺小伟

144 Example: Java server (TCP), cont
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection 2018/9/16 主讲教师:西北大学 贺小伟

145 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

146 Socket programming with UDP
UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server 2018/9/16 主讲教师:西北大学 贺小伟

147 Client/server socket interaction: UDP
Server (running on hostid) create socket, clientSocket = DatagramSocket() Client Create, address (hostid, port=x, send datagram request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket close clientSocket read reply from clientSocket write reply to serverSocket specifying client host address, port number 2018/9/16 主讲教师:西北大学 贺小伟

148 Example: Java client (UDP)
process Input: receives packet (TCP received “byte stream”) Output: sends packet (TCP sent “byte stream”) client UDP socket 2018/9/16 主讲教师:西北大学 贺小伟

149 Example: Java client (UDP)
import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS 2018/9/16 主讲教师:西北大学 贺小伟

150 Example: Java client (UDP), cont.
Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Send datagram to server Read datagram from server 2018/9/16 主讲教师:西北大学 贺小伟

151 Example: Java server (UDP)
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram 2018/9/16 主讲教师:西北大学 贺小伟

152 Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram 2018/9/16 主讲教师:西北大学 贺小伟

153 第2章 应用层(Application Layer)
本章学习要点 应用层协议原理(Principles of Application Layer Protocols) Web应用和HTTP协议(The World Wide Web: HTTP) 文件传输协议: FTP (File Transfer: FTP) 因特网中的电子邮件(Electronic Mail in the Internet) DNS-因特网的目录服务(The Internet’s Directory Service) P2P文件共享(Peer to Peer File Sharing) TCP套接字编程(Socket programming with TCP) UDP套接字编程(Socket programming with UDP) 构造一个简单的Web服务器(Building a Web server) 2018/9/16 主讲教师:西北大学 贺小伟

154 Building a simple Web server
handles one HTTP request accepts the request parses header obtains requested file from server’s file system creates HTTP response message: header lines + file sends response to client after creating server, you can request file using a browser (eg IE explorer) see text for details 2018/9/16 主讲教师:西北大学 贺小伟

155 本讲小结 Our study of network apps now complete! 特定协议: 应用服务的要求: 客户端-服务器范式
http ftp smtp, pop3 dns 应用服务的要求: 可靠性, 带宽, 延迟 客户端-服务器范式 Internet 传输服务模型 面向连接的, 可靠的: TCP 不可靠的, 数据报: UDP 2018/9/16 主讲教师:西北大学 贺小伟

156 本讲小结 Most importantly: learned about protocols 典型的请求/应答报文交换: 报文格式:
客户请求信息或服务 服务器用数据, 状态码进行响应 报文格式: 首部: 说明数据的信息 数据: 进行通信的信息 控制 vs. 数据报文 in-based, out-of-band 集中式 vs. 非集中式 无状态 vs. 有状态 可靠的 vs. 不可靠的报文传输 “网络边缘上的复杂实体” 安全性: 认证 2018/9/16 主讲教师:西北大学 贺小伟


Download ppt "计算机网络 贺小伟 xwhe@hotmail.com."

Similar presentations


Ads by Google