1. 套接字socket
Posted 一步一步学习人工智能
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. 套接字socket相关的知识,希望对你有一定的参考价值。
套接字(socket)
套接字的概念并不好理解。
socket的最早定义见于1971年5月7日麻省理工学院林肯实验室发布的RFC147, RFC ( RequestFor Comments)是由互联网工程任务组(IETF)发布的一系列备忘录。那时socket用于ARPA网络(AdvancedResearch Project Agency, 是美国国防部高级计划研究计划局于十九世纪六十年代后期用作军事作战的网络,是现在英特网的前身)。
根据这一概念, 1983年伯克利将它实现并用于TCP/IP, socket用于进程间通信,这就是BSD Unix 4.2。这时候,socket已经指的是一个socket层,一个socket文件或对象(实现为一个数据结构)。
所以, socket最早指的是网络信息流经的端点(end), 由32位的二进制数来表示。后来,也可以指socket对象,它包含了一系列的方法,用于进程间通信。
至于国内的译名,最早见于1977年02期《电子计算机动态》的《计算机网词汇译名讨论》,文中把socket译为“媒介(字)”。
在1979年09期《电子计算机参考资料》上一篇无名氏作的《计算机网络软件简介》上,也是关于ARPANET的,作者使用了“套接字”这三个字,同时作者也备注作“信口”。
阎保平发表于1982年12期《计算机研究与发展》的《一种局部互连分组交换网络的体系结构》文章,设计了一种局域网组网的原理和实现,在文中提到了套接字,又在一个图表中示意出了socket。
陆圣铭发表在1984年07期《电子技术》上的《微计算机时代的普及型局部网络》,在介绍OMNINET(一种结构简单、灵活的低成本微型机局域网方案)时提到socket为“软插口”,并附加了注释“并不是硬件上的插座”。
这些翻译,用的是socket最初的含义,从这方面说译作“套接字”还是可以的。
但现在,socket常指的是第二种意思,即一个用于进程间通信的复杂的数据结构,这时说成是套接体可能更容易理解。
其实,socket即便在英语世界也颇令人费解。其原因就是,各种操作系统用这个名字做了不同的实现,并把实现也仍然叫做socket。
(1) 最初定义
我们先来看看RFC147的内容吧:
A socket is defined to be the unique identification to or from which information is
transmitted in the network.
套接字被定义为网络中信息流入或流出的唯一标识。
The socket is specified as a 32 bit number with even sockets identifying receiving sockets and odd sockets identifying sending sockets.
套接字被定义为一个32位数,偶数为接收套接字,奇数为发送套接字。
A socket is also identified by the host in which the sending or receiving processer is located.
套接字也由发送或接收处理器所在的主机标识。
Previous network papers postulated that a process running under control of the host's operating system would have access to a number of ports.
A port might be a physical input or output device, or a logical I/O device supported by system calls to the host's operating system.
The latter category includes
a) I/O directed to a physical device which is beingspooled by the operating system,
b) a physical device whose basic characteristics have not been altered but whose
access has been limited and possibly transformed by a mapping algorithm
(e.g. device address mapping or cylinder relocation as in virtual mini disks),
c) access to a file system through a directory and access method
maintained by the operating system,
d) a procedure for process to process communications,
e) a procedure for machine to machine communication (such as defined
by the network protocol.)
以前的网络论文假设在主机操作系统控制下运行的进程可以访问多个端口(port)。
端口可以是物理的输入或输出设备,也可以是主机操作系统的系统调用所支持的逻辑I/O设备。
后一类包括:
a) 指向被操作系统假脱机的物理设备的I/O。
b) 其基本特性没有改变但其访问受到限制,并且可能被映射算法
(例如,虚拟迷你盘中的设备地址映射或柱面重定位)转换的物理设备。
c) 通过一个目录访问的文件系统,且访问方法由操作系统维持。
d) 进程间通信的过程
e) 机器间通信的过程(如由网络协议定义的)。
A socket has been defined to be the identification of a port for machine to machine
communication through the ARPA network. Sockets allocated to each host must be uniquely associated with a known process or be undefined.
套接字定义为机器间通过ARPA网通信的端口标识。分配给每一个主机的套接字必须与已知进程唯一关联或未定义。
The name of some sockets must be universally known and associated with a known process operating with a specified protocol. (e.g., a logger socket, RJE socket, a file transfer socket).
有些套接字的名字必须广为人知,并且关联到由指定协议操作的已知进程。(比如,日志logger套接字,RJE套接字,文件传输套接字)。
The name of other sockets might not be universally known, but given in a transmission over a universally known socket, (c. g. the socket pairs pecified by the transmission over the logger socket under the Initial Connection Protocol (ICP).
其他套接字的名称通常并不知名, 但会在知名套接字的传输中给出(例如,在初始连接协议(ICP)下通过logger套接字的传输指定的套接字对)。
In any case, communication over the network is from one socket to another socket, each socket being identified with a process running at a known host.
在任何情况下,网络上的通信都是从一个套接字到另一个套接字,每个套接字都由已知主机上
运行的进程识别。
The question arises as to whether socket names must be known to users of network programs or whether the specification of sockets can be made transparent to the user.
问题是套接字名称是否必须为网络程序的用户所知,或者套接字的规范是否可以对用户透明。
Also, should the socket used at one time by a process be the same socket used at a later time by the same process for the same purpose?
还有,这次进程使用的套接字是否应该与以后同一进程出于相同目的使用的套接字相同?
If sockets are not transparent to the user, then the sockets used must not be dependent on the order in which network connections are made.
如果套接字对用户不透明,则使用的套接字不能依赖网络连接建立的顺序。
The definition of a socket is also related to the accounting procedures followed for
network usage. Network Control Programs (NCPs) should log each connection made and record the time the connection was made, the time the connection was closed, the number of messages and number of bits transmitted over the connection, the sending and receiving hosts, and the sockets at the sending host and receiving host which participated in the connection.
套接字的定义也与网络使用所遵循的记帐过程有关。网络控制程序(NCP)应记录每个连接,并记录建立连接的时间、关闭连接的时间、通过连接传输的消息数量和位数、发送和接收主机以及参与连接的发送主机和接收主机的套接字。
In order for these sockets to be meaningful, they should be identifiable with the user,
account, or process name with which each socket is associated.
为了使这些套接字有意义,套接字应该可以使用每个套接字关联的用户、帐户或进程名称进行标识。
It has previously been suggested that the sockets used by a network user be identified with that user no matter which host he used for network communications. Users would be registered at some host and be identified as a user from that host even if he used the system as a second host tocommunicate with the system at a third host.
先前已经建议,无论网络用户使用哪台主机进行网络通信,网络用户使用的套接字都应与该用户标识。用户将在某个主机上注册,并被标识为来自该主机的用户,即使他将该系统用作第二主机来与第三主机上的系统通信。
To satisfy the above requirements within the name space of a 32 bit socket, the
following procedure is suggested. This procedure has been implemented with the NCP on the Lincoln Laboratory 360/67 system and is used by all processes making use of network facilities. ) A 32 bit socket is divided into an 8 bit"home" field, a 16 bit "user"field and an 8 bit "tag"field.
The tag consists of a 7 bit "plug" and a one bit "polarity" where a "0" polarity indicates a receive socket and a"1" polarity indicates a send socket.
Thus a user on one host system may run processes with upto 128 send sockets and 128 receive sockets. This procedure allows for 256 hosts and 65,536 users per host.
为了在32位的套接字的名称空间内满足上述要求,建议使用以下过程。此程序已在林肯实验室360/67系统上的NCP中实施,并被所有使用网络设施的进·程使用。):
一个32位套接字被分成一个8位的“home”字段,一个16位的“user”字段和8位“标签tag”字段。
标签由7位“插头”和1位“极性”组成,其中“0”极性表示接收套接字,“1”极性表示发送套接字。
因此,一个主机系统上的用户可以运行多达128个发送套接字和128个接收套接字的进程。此过程允许256台主机和每台主机65,536个用户。
The only difficulty is in mapping user or process identifiers at a host into a 16 bit
user number. This may be done through a table lookup,possibly using a hash coding
technique. Though many systems have a unique run time index associated with each process, if this index were used as the user number, the user number would not be the same each time the process were used for network activity.
唯一的困难是将主机上的用户或进程标识符映射成16位用户编号。这可以通过可能使用哈希编码技术的表查找来完成。尽管许多系统都有与每个进程相关联的唯一运行时索引,但如果将此索引用作用户编号,则每次将进程用于网络活动时,用户编号都不会相同。
What is required, is a unique mapping from a process identifier (usually a character
string) into a 16 bit binary number. The sockets used for facilities following a common
network protocol, such as the ICP,should also follow this socket definition.
需要的是从进程标识符(通常是字符串)到16位二进制数的唯一映射。用于遵循公共网络协议(如
ICP)的设施的套接字也应遵循此套接字定义
Thus the logger socket at the Lincoln Laboratory 360/67would be, and is, x’0A0000 01,’ , i.e. home 10, user 0, and tag 1.
因此,林肯实验室360/67处的logger套接字应该是x‘0A0000 01’,即HOME10、USER 0和TAG 1。
This procedure for defining sockets enables an accounting procedure for identifying
users of network facilities and for measuring network usage.
此定义套接字的过程启用了一个标识网络设施用户和测量网络用途的记账过程。
下面是套接字的一个示意图,但并不属于原文。
(2)引申的定义
下面的定义,有时指的是套接字,有时指的是套接字对象
一个应用进程和传输层之间的接口,通过套接字,本地或远程之间的应用进程可以通信。
在Unix行话里,套接字就是一个文件描述符,与打开文件关联的整数。
套接字类型:Internet套接字、Unix套接字、X.25套接字等
(3) Internet套接字
流套接字(SOCK_STREAM):面向连接,依赖TCP ,提供可靠的双向连接通信。
数据报套接字(SOCK_DGRAM):依赖UDP,连接并不可靠。
以上是关于1. 套接字socket的主要内容,如果未能解决你的问题,请参考以下文章