Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2 Application Layer A note on the use of these ppt slides: We’re making these slides freely available to all (faculty, students, readers). They’re in PowerPoint form so you can add, modify, and delete slides (including this one) and slide content to suit your needs. They obviously represent a lot of work on our part. In return for use, we only ask the following: If you use these slides (e.g., in a class) in substantially unaltered form, that you mention their source (after all, we’d like people to use our book!) If you post any slides in substantially unaltered form on a www site, that you note that they are adapted from (or perhaps identical to) our slides, and note our copyright of this material. Computer Networking: A Top Down Approach, 4th edition. Jim Kurose, Keith Ross Addison-Wesley, July 2007. Thanks and enjoy! JFK/KWR All material copyright 1996-2007 J.F Kurose and K.W. Ross, All Rights Reserved 2: Application Layer 1 第二章:应用层 2.1 网络应用程序的设计 2.6 P2P 应用程序 原则 2.2 Web 与 HTTP 2.3 FTP 2.4 电子邮件 2.7 使用TCP的套接字编程 2.8使用UDP的套接字编程 SMTP, POP3, IMAP 2.5 DNS 2: Application Layer 2 第二章:应用层 学习目标: 网络应用协议的概念与 实现 传输层服务模型 client-server 范例 peer-to-peer 范例 通过探讨应用层协议了 解网络协议 HTTP FTP SMTP / POP3 / IMAP DNS 编程实现网络程序 套接字 API 2: Application Layer 3 网络应用程序举例 电子邮件 VOIP Web 实时视频会议 即时消息 网格计算 远程登录 P2P 文件共享 多用户网络游戏 流媒体视频 2: Application Layer 4 创建一个网络应用程序 编写程序: application transport network data link physical 运行在(不同的)端系统上 通过网络通信 e.g., Web 服务器软件与浏览 器软件之间进行通信 不需要为网络核心的设备编写 软件 网络核心的设备上不运行用 户程序 端系统上的应用程序可以快 速部署和传播 application transport network data link physical application transport network data link physical 2: Application Layer 5 第二章:应用层 2.1 网络应用程序的设计 2.6 P2P 应用程序 原则 2.2 Web 与 HTTP 2.3 FTP 2.4 电子邮件 2.7 使用TCP的套接字编程 2.8使用UDP的套接字编程 2.9 构建一个 Web 服务器 SMTP, POP3, IMAP 2.5 DNS 2: Application Layer 6 网络应用程序体系结构 Client-Server Peer-to-peer (P2P) C/S 与 P2P 的混杂结构 2: Application Layer 7 Client-Server 结构 服务器: 一直工作的主机 永久 IP 地址 可伸缩的服务器集群 客户端: client/server 与服务器进行通信 可能只是间歇性的连接 可能使用的动态 IP 客户端之间不直接互相通信 2: Application Layer 8 纯 P2P 结构 没有一直工作的服务器 任意端系统键直接通信 对等端之间间歇的建立连peer-peer 接,IP地址是改变的 example: Gnutella 高度的可伸缩性但是难于管 理 2: Application Layer 9 C/S 与 P2P 混合结构 Skype VOIP P2P 应用程序 中心服务器:查找远端的地址 客户端对客户端连接:直接的 (不通过服务器) 即时消息/即时通信 两个用户之间聊天采用的是 P2P 结构 中心化的服务:用于客户端的存在感知/位置 • 用户上线时向中心服务器注册它的IP地址 • 用户通过练习中心服务器获得伙伴的 IP 地址 2: Application Layer 10 进程通信 进程: 在主机上运行的一个 程序 在同一个主机上,两个进 程使用进程间通信 (由 OS定义). 客户端进程: 发起通信的进 程 服务器进程: 等待连接的进 程 不同主机上的进程通过交 换消息实现通信 注意: P2P 结构的应用程 序也存在客户端进程和服 务器端进程 2: Application Layer 11 套接字 进程通过使用套接字收发消 息 套接字类似于门 host or server host or server 发送进程将消息推出门外 process 发送进程依靠门另一边的传 输基础设施将消息送给接收 进程的套接字 socket controlled by app developer process socket TCP with buffers, variables Internet TCP with buffers, variables controlled by OS API: (1) 选择传输协议; (2) 规定一些参数 2: Application Layer 12 进程寻址 标识符 包括 IP 地址和与 要接收消息,进程必须具 有标识符 主机设备具有唯一的 32bit IP 地址 Q: IP 地址是否足够标识 运行在其上的进程? A: 不能,同一个主机 上可能在运行多个进 程 主机上的进程关联的端口 号。 端口号举例: HTTP 服务器: 80 邮件服务器: 25 向 gaia.cs.umass.edu 的 Web 服务器发送HTTP消 息: IP address: 128.119.245.12 Port number: 80 2: Application Layer 13 应用层协议定义 交换消息的类型: e.g., request, response 消息语法: 消息包括哪些数据域 & 数 据域如何设置 消息语义 数据域中信息的意义 公共协议: 在 RFC 中定义 考虑了互操作性 e.g., HTTP, SMTP 专用协议: e.g., Skype 进程何时、如何发送 & 回 应消息的规则 2: Application Layer 14 What transport service does an app need? Data loss some apps (e.g., audio) can tolerate some loss other apps (e.g., file transfer, telnet) require 100% reliable data transfer Timing some apps (e.g., Internet telephony, interactive games) require low delay to be “effective” Bandwidth some apps (e.g., multimedia) require minimum amount of bandwidth to be “effective” other apps (“elastic apps”) make use of whatever bandwidth they get 2: Application Layer 15 Transport service requirements of common apps Data loss Bandwidth Time Sensitive file transfer e-mail Web documents real-time audio/video no loss no loss no loss loss-tolerant no no no yes, 100’s msec stored audio/video interactive games instant messaging loss-tolerant loss-tolerant no loss elastic elastic elastic audio: 5kbps-1Mbps video:10kbps-5Mbps same as above few kbps up elastic Application yes, few secs yes, 100’s msec yes and no 2: Application Layer 16 Internet transport protocols services TCP service: connection-oriented: setup required between client and server processes reliable transport between sending and receiving process flow control: sender won’t overwhelm receiver congestion control: throttle sender when network overloaded does not provide: timing, minimum bandwidth guarantees UDP service: unreliable data transfer between sending and receiving process does not provide: connection setup, reliability, flow control, congestion control, timing, or bandwidth guarantee Q: why bother? Why is there a UDP? 2: Application Layer 17 Internet apps: application, transport protocols Application e-mail remote terminal access Web file transfer streaming multimedia Internet telephony Application layer protocol Underlying transport protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] proprietary (e.g. RealNetworks) proprietary (e.g., Vonage,Dialpad) TCP TCP TCP TCP TCP or UDP typically UDP 2: Application Layer 18 Chapter 2: Application layer 2.1 Principles of network applications app architectures app requirements 2.2 Web and HTTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.6 P2P file sharing 2.7 Socket programming with TCP 2.8 Socket programming with UDP 2.5 DNS 2: Application Layer 19 Web 与 HTTP 一些术语 Web 页由一系列对象构成 对象可以是 HTML 文件、 JPEG 图片、 Java 小程序 、音频文件… Web 页由包含数个引用对象的基本 HTML文件构成 每个对象可通过 URL 寻址 URL 示例: www.someschool.edu/someDept/pic.gif 主机名称 路径名称 2: Application Layer 20 HTTP 概述 HTTP: 超文本传输协议( hypertext transfer protocol) Web 的应用层协议 PC running Explorer C/S 模型 客户端: 浏览器,请求、 接收、“显示” Web 对 象 服务器: 发送对象响应请 求 HTTP 1.0: RFC 1945 HTTP 1.1: RFC 2068 Server running Apache Web server Mac running Navigator 2: Application Layer 21 HTTP 概述 使用 TCP: HTTP 是“无状态”的 客户端发起到服务器的 TCP 连 服务器不维护过去的客 接 (创建套接字),端口号:80 服务器接受来自客户端的 TCP 连接 HTTP 消息 (应用层协议消息) 在浏览器 (HTTP 客户端) 与 Web 服务器 (HTTP 服务器端) 之间进行交换 TCP 连接关闭 户端请求的信息 aside 维护“状态”的协议比较复杂 过去的历史(状态)必须维 护 如果服务器/客户端崩溃,它 们对于“状态”的认知将是 不一致的 ,必须进行一致性 重建 2: Application Layer 22 HTTP 连接 非持久 HTTP 一个 TCP 连接至多发送 一个对象 HTTP/1.0 使用 非持久 HTTP 持久 HTTP 多个对象可经单个 TCP 连接在客户端和服务器 之间进行传递 HTTP/1.1 在默认模式下 使用持久连接 2: Application Layer 23 HTTP 请求消息 两种类型的 HTTP 消息: request, response HTTP 消息: ASCII (人能读懂的格式) request line (GET, POST, HEAD commands) GET /somedir/page.html HTTP/1.1 Host: www.someschool.edu User-agent: Mozilla/4.0 header Connection: close lines Accept-language:fr Carriage return, line feed indicates end of message (extra carriage return, line feed) 2: Application Layer 24 HTTP request message: general format 2: Application Layer 25 HTTP 响应消息 status line (protocol status code status phrase) header lines data, e.g., requested HTML file HTTP/1.1 200 OK Connection close Date: Thu, 06 Aug 1998 12:00:15 GMT Server: Apache/1.3.0 (Unix) Last-Modified: Mon, 22 Jun 1998 …... Content-Length: 6821 Content-Type: text/html data data data data data ... 2: Application Layer 26 FTP: 文件传输协议 user at host FTP FTP user client interface file transfer FTP server remote file system local file system 传输文件到/从远端主机 C/S 模型 client: 发起传输的一端 (到或者从远端) server: 远端主机 ftp: RFC 959 ftp server: port 21 2: Application Layer 27 FTP: 分离的控制与数据连接 FTP 客户端通过 21 端口连 TCP control connection port 21 接 FTP 服务器,使用的传输 协议是 TCP TCP data connection FTP FTP port 20 客户端经由控制连接进行验 client server 证 客户端经由控制连接通过发 传输另一个文件时,服务器将 打开另一个 TCP 连接 送命令来浏览远端目录 服务器接收到文件传输命令 控制连接: “out of band” 是,建立到客户端的第二个 FTP 服务器维护“状态”:当前 目录、先前进行过的验证 TCP 连接 传输完一个文件后,服务器 关闭数据连接 2: Application Layer 28 FTP 命令、响应 命令举例: 返回码举例: 通过控制通道发送 ASCII 文 状态码与短语 (与HTTP中相 本 USER username PASS password 返回当前目录的文 件列表 RETR filename 获取文件 LIST STOR filename 将文件存 储到远端主机 识) 331 Username OK, password required 125 data connection already open; transfer starting 425 Can’t open data connection 452 Error writing file 2: Application Layer 29 套接字编程 Goal: learn how to build client/server application that communicate using sockets Socket API 1981年在 BSD4.1 UNIX 中引 入 由应用程序显式创建、使用和 释放 C/S 模型 使用套接字 API 的两种传输服 务: 不可靠数据报 面向字节流的可靠服务 socket 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 2: Application Layer 30 Socket-programming using TCP Socket: a door between application process and endend-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process process socket TCP with buffers, variables host or server internet socket TCP with buffers, variables controlled by application developer controlled by operating system host or server 2: Application Layer 31 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) application viewpoint TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server 2: Application Layer 32 Client/server socket interaction: TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP wait for incoming connection request connection connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket close connectionSocket setup create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket read reply from clientSocket close clientSocket 2: Application Layer 33 Stream jargon keyboard monitor output stream inFromServer Client Process process input stream outToServer characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g., keyboard or socket. An output stream is attached to an output source, e.g., monitor or socket. inFromUser A stream is a sequence of input stream client TCP clientSocket socket to network TCP socket from network 2: Application Layer 34 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) 2: Application Layer 35 Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Create input stream Create client socket, connect to server Create output stream attached to socket BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 2: Application Layer 36 Example: Java client (TCP), cont. Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); Send line to server outToServer.writeBytes(sentence + '\n'); Read line from server modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } } 2: Application Layer 37 Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 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())); 2: Application Layer 38 Example: Java server (TCP), cont Create output stream, attached to socket DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); Read in line from socket clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; Write out line to socket outToClient.writeBytes(capitalizedSentence); } } } End of while loop, loop back and wait for another client connection 2: Application Layer 39 Chapter 2: Application layer 2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P file sharing 2.7 Socket programming with TCP 2.8 Socket programming with UDP 2.9 Building a Web server 2: Application Layer 40 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 application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server UDP: transmitted data may be received out of order, or lost 2: Application Layer 41 Client/server socket interaction: UDP Server (running on hostid) create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number Client create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read reply from clientSocket close clientSocket 2: Application Layer 42 Example: Java client (UDP) input stream Client process monitor inFromUser keyboard Process Input: receives packet (recall thatTCP received “byte stream”) UDP packet receivePacket packet (recall that TCP sent “byte stream”) sendPacket Output: sends client UDP clientSocket socket to network UDP packet UDP socket from network 2: Application Layer 43 Example: Java client (UDP) import java.io.*; import java.net.*; Create input stream Create client socket Translate hostname to IP address using DNS 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(); 2: Application Layer 44 Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); Send datagram to server clientSocket.send(sendPacket); Read datagram from server clientSocket.receive(receivePacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } 2: Application Layer 45 Example: Java server (UDP) import java.io.*; import java.net.*; Create datagram socket at port 9876 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) { Create space for received datagram Receive datagram DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); 2: Application Layer 46 Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); Get IP addr port #, of sender InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); Create datagram to send to client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Write out datagram to socket serverSocket.send(sendPacket); } } } End of while loop, loop back and wait for another datagram 2: Application Layer 47 Chapter 2: Summary our study of network apps now complete! application architectures client-server P2P hybrid application service requirements: reliability, bandwidth, delay specific protocols: HTTP FTP SMTP, POP, IMAP DNS P2P: BitTorrent, Skype socket programming Internet transport service model connection-oriented, reliable: TCP unreliable, datagrams: UDP 2: Application Layer 48 Chapter 2: Summary Most importantly: learned about protocols typical request/reply message exchange: client requests info or service server responds with data, status code message formats: headers: fields giving info about data data: info being communicated Important themes: control vs. data msgs in-band, out-of-band centralized vs. decentralized stateless vs. stateful reliable vs. unreliable msg transfer “complexity at network edge” 2: Application Layer 49