《计算机网络与因特网》.ppt
《《计算机网络与因特网》.ppt》由会员分享,可在线阅读,更多相关《《计算机网络与因特网》.ppt(73页珍藏版)》请在三一办公上搜索。
1、Computer Networks and Internets计算机网络与因特网课件,PART IV Network Applications,Chapter 27 The Socket Interface Socket 接口,27.1 Introduction,The interface between an application and protocol software.How an application uses protocol software to communication.Set of procedures that an application uses to beco
2、me a client or a server,to contact a remote destination,or to transfer data.,27.2 Application Program Interface,Client and server application use transport protocols to communicate.The interface an application uses when it interacts with transport protocol software is known as an Application Program
3、 Interface(API).An API defines a set of operations that an application can perform when interacting with protocol software,the API determines the functionality that is available.,27.3 The Socket API,Although protocol standards allow operating system designers to choose an API,many have adopted the S
4、ocket API.The Socket API is available for many operating systems.The Socket API originated as part of the BSD Unix operating system.The Socket API is a de facto standard.,27.4 Sockets And Socket Libraries,In BSD Unix and the system derived from it,socket functions are part of the operating system it
5、self.In many cases,instead of modifying their basic operating system,vendors created a socket library that provides the Socket API.,A socket library can provide applications with a socket API on a computer system that does not provide native sockets.When an application calls one of the socket proced
6、ures,control passes to a library routine that makes one or more calls to the underlying operating system to implement the socket function.,27.5 Socket Communication And Unix I/O,An application communicates through a socket similar to the way the application transfers data to or from a file.Unix uses
7、 an open-read-write-close paradigm for all I/o,补充:1.Socket API与Unix,Unix是一个通用的交互式多用户分时操作系统。Unix由AT&T 公司Bell实验室于1969年开发。Unix 有两个主要的流派:System V 和 BSDSystem V 由最初的AT&T Unix 演化而来。BSD Unix 是由加州大学伯克利(Berkeley)分校开发,其目标是提供一个具有复杂网络支持的开放系统。TCP/IP 协议族也与Unix 的发展有很密切关系。Socket API与Unix 操作系统关系非常密切。,UNIX系统的I/O命令集,其
8、模式为打开一读/写一关闭(open-write-read-close)。在一个用户进程进行I/O操作时,它首先调用“打开”获得对指定文件或设备的使用权,并返回称为文件描述符的整型数,以描述用户在打开的文件或设备上进行I/O操作的进程。然后这个用户进程多次调用“读/写”以传输数据。当所有的传输操作完成后,用户进程关闭调用,通知操作系统已经完成了对某对象的使用。,TCP/IP协议被集成到UNIX内核中时,相当于在UNIX系统引入了一种新型的I/O操作。UNIX用户进程与网络协议的交互作用比用户进程与传统的I/O设备相互作用复杂得多。首先,进行网络操作的两个进程可能在不同主机上,如何建立它们之间的联
9、系?其次,存在多种网络协议,如何建立一种通用机制以支持多种协议?这些都是网络应用编程所要解决的问题。,进程通信的概念最初来源于单机系统。由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进程之间既互不干扰又协调一致工作,操作系统为进程通信提供了相应设施:如UNIXBSD中的管道(pipe)、命名管道(namedpipe)和软中断信号(signal),UNIXsystemV的消息(message)、共享存储区(sharedmemory)和信号量(semaphore)等,但都仅限于用在本机进程之间通信。,2 进程间通信,3 网间进程通信,网间进程通信要解决的是不同主机进程间的相互通信问题。
10、可以把同机进程通信看作是其中的特例。为此,首先要解决的是网间进程标识问题。同一主机上,不同进程可用进程号(processID)唯一标识。但在网络环境下,各主机独立分配的进程号不能唯一标识该进程。,其次,操作系统支持的网络协议众多,不同协议的工作方式不同,地址格式也不同。因此,网间进程通信还要解决多重协议的识别问题。,27.6 Sockets,Descriptors,And network I/O,Socket communication also uses the descriptor approach.Before an application can use protocols to co
11、mmunicate,the application must request the operating system to create a socket that will be used for communication.The system returns a small integer descriptor that identifies the socket.,In a Unix implementation,sockets are completely integrated with other I/o.A single application can be written t
12、hat transfers data to an arbitrary location.,4.协议端口,网络中可以被命名和寻址的通信端口,是操作系统可分配的一种资源。传输层与网络层在功能上的最大区别是传输层提供进程通信能力。因此,网络通信的最终地址就不仅仅是主机地址了,还包括可以描述进程的某种标识符。为此,TCP/IP协议提出了协议端口(protocolport,简称端口)的概念,用于标识通信的进程。,端口是一种抽象的软件结构(包括一些数据结构和I/O缓冲区)。应用程序(即进程)通过系统调用与某端口建立连接(binding)后,传输层传给该端口的数据都被相应进程所接收,相应进程发给传输层的数据
13、都通过该端口输出。在TCP/IP协议的实现中,端口间的操作类似于一般的I/O操作,进程获取一个端口,相当于获取本地唯一的I/O文件,可以用一般的读写原语访问之。,类似于文件描述符,每个端口都拥有一个叫端口号(portnumber)的整数型标识符,用于区别不同端口。由于TCP/IP传输层的两个协议TCP和UDP是完全独立的两个软件模块,因此各自的端口号也相互独立,如TCP有一个255号端口,UDP也可以有一个255号端口,二者并不冲突。,半相关,网络中用一个三元组可以在全局唯一标志一个进程:(协议,本地地址,本地端口号)这样一个三元组,叫做一个半相关(half-association),它指定连
14、接的每半部分。,全相关,一个完整的网间进程通信需要由两个进程组成,并且只能使用同一种高层协议。一个完整的网间通信需要一个五元组来标识:(协议,本地地址,本地端口号,远程地址,远程端口号)这样一个五元组,叫做一个相关(association),即两个协议相同的半相关才能组合成一个合适的相关,或完全指定组成一连接。,Socket接口应用程序的基本网络接口由操作系统提供;进程的通信端点一个socket的信息含义协议类型本地地址本地端口号远端地址远端端口号,27.7 Parameters And The Socket API,Socket programming differs from conven
15、tional I/o.Because an application must specify many details to use a socket.To accommodate all the details,each socket has many parameters and options.To avoid having a single socket function with separate parameters for each option,designers of the socket API chose to define many function.,插口结构,Cli
16、ent-Server执行模式,Client-Server端执行模式,Server,Client,注意:上面这些Socket的意思与指IP地址和TCP端口号的组合的Socket不同,27.8 Procedures that Implement The Socket API,27.8.1 The Socket Procedure.,descriptor=socket(protofamily,type,protocol)Return a socket descriptor if successful;-1 if error.,The Socket Procedure:The socket proce
17、dure creates a socket and returns an integer descriptor.It sets values for only the first three fields of the socket structure.The other fields are set by the other functions or by the operating system.,Argument Protofamily:,1:AF-INET4 IPv4协议2:AF-INET6 IPv6协议3:AF-LOCAL Unix域协议4:AF-ROUTE 路由插口,Argumen
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机网络与因特网 计算机网络 因特网
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-5904138.html