📄 2.3.t
字号:
.\" Copyright (c) 1983, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)2.3.t 8.1 (Berkeley) 6/8/93.\".sh "Interprocess communications.NH 3Interprocess communication primitives.NH 4Communication domains.PPThe system provides access to an extensible set of communication \fIdomains\fP. A communication domainis identified by a manifest constant defined in thefile \fI<sys/socket.h>\fP.Important standard domains supported by the system are the ``unix''domain, AF_UNIX, for communication within the system, the ``Internet''domain for communication in the DARPA Internet, AF_INET,and the ``NS'' domain, AF_NS, for communicationusing the Xerox Network Systems protocols.Other domains can be added to the system..NH 4Socket types and protocols.PPWithin a domain, communication takes place between communication endpointsknown as \fIsockets\fP. Each socket has the potential to exchangeinformation with other sockets of an appropriate type within the domain..PPEach socket has an associatedabstract type, which describes the semantics of communication using thatsocket. Properties such as reliability, ordering, and preventionof duplication of messages are determined by the type.The basic set of socket types is defined in \fI<sys/socket.h>\fP:.DS/* Standard socket types */._d#define SOCK_DGRAM 1 /* datagram */#define SOCK_STREAM 2 /* virtual circuit */#define SOCK_RAW 3 /* raw socket */#define SOCK_RDM 4 /* reliably-delivered message */#define SOCK_SEQPACKET 5 /* sequenced packets */.DEThe SOCK_DGRAM type models the semantics of datagrams in network communication:messages may be lost or duplicated and may arrive out-of-order.A datagram socket may send messages to and receive messages from multiplepeers.The SOCK_RDM type models the semantics of reliable datagrams: messagesarrive unduplicated and in-order, the sender is notified ifmessages are lost.The \fIsend\fP and \fIreceive\fP operations (described below)generate reliable/unreliable datagrams.The SOCK_STREAM type models connection-based virtual circuits: two-waybyte streams with no record boundaries.Connection setup is required before data communication may begin.The SOCK_SEQPACKET type models a connection-based,full-duplex, reliable, sequenced packet exchange;the sender is notified if messages are lost, and messages are neverduplicated or presented out-of-order.Users of the last two abstractions may use the facilities forout-of-band transmission to send out-of-band data..PPSOCK_RAW is used for unprocessed access to internal network layersand interfaces; it has no specific semantics..PPOther socket types can be defined..PPEach socket may have a specific \fIprotocol\fP associated with it.This protocol is used within the domain to provide the semanticsrequired by the socket type.Not all socket types are supported by each domain;support depends on the existence and the implementationof a suitable protocol within the domain.For example, within the ``Internet'' domain, the SOCK_DGRAM type may beimplemented by the UDP user datagram protocol, and the SOCK_STREAMtype may be implemented by the TCP transmission control protocol, whileno standard protocols to provide SOCK_RDM or SOCK_SEQPACKET sockets exist..NH 4Socket creation, naming and service establishment.PPSockets may be \fIconnected\fP or \fIunconnected\fP. An unconnectedsocket descriptor is obtained by the \fIsocket\fP call:.DSs = socket(domain, type, protocol);result int s; int domain, type, protocol;.DEThe socket domain and type are as described above,and are specified using the definitions from \fI<sys/socket.h>\fP.The protocol may be given as 0, meaning any suitable protocol.One of several possible protocols may be selected using identifiersobtained from a library routine, \fIgetprotobyname\fP..PPAn unconnected socket descriptor of a connection-oriented typemay yield a connected socket descriptorin one of two ways: either by actively connecting to another socket,or by becoming associated with a name in the communications domain and\fIaccepting\fP a connection from another socket.Datagram sockets need not establish connections before use..PPTo accept connections or to receive datagrams,a socket must first have a bindingto a name (or address) within the communications domain.Such a binding may be established by a \fIbind\fP call:.DSbind(s, name, namelen);int s; struct sockaddr *name; int namelen;.DEDatagram sockets may have default bindings established when firstsending data if not explicitly bound earlier.In either case,a socket's bound name may be retrieved with a \fIgetsockname\fP call:.DSgetsockname(s, name, namelen);int s; result struct sockaddr *name; result int *namelen;.DEwhile the peer's name can be retrieved with \fIgetpeername\fP:.DSgetpeername(s, name, namelen);int s; result struct sockaddr *name; result int *namelen;.DEDomains may support sockets with several names..NH 4Accepting connections.PPOnce a binding is made to a connection-oriented socket,it is possible to \fIlisten\fP for connections:.DSlisten(s, backlog);int s, backlog;.DEThe \fIbacklog\fP specifies the maximum count of connectionsthat can be simultaneously queued awaiting acceptance..PPAn \fIaccept\fP call:.DSt = accept(s, name, anamelen);result int t; int s; result struct sockaddr *name; result int *anamelen;.DEreturns a descriptor for a new, connected, socketfrom the queue of pending connections on \fIs\fP.If no new connections are queued for acceptance,the call will wait for a connection unless non-blocking I/O has been enabled..NH 4Making connections.PPAn active connection to a named socket is made by the \fIconnect\fP call:.DSconnect(s, name, namelen);int s; struct sockaddr *name; int namelen;.DEAlthough datagram sockets do not establish connections,the \fIconnect\fP call may be used with such socketsto create an \fIassociation\fP with the foreign address.The address is recorded for use in future \fIsend\fP calls,which then need not supply destination addresses.Datagrams will be received only from that peer,and asynchronous error reports may be received..PPIt is also possible to create connected pairs of sockets withoutusing the domain's name space to rendezvous; this is done with the\fIsocketpair\fP call\(dg:.FS\(dg 4.3BSD supports \fIsocketpair\fP creation only in the ``unix''communication domain..FE.DSsocketpair(domain, type, protocol, sv);int domain, type, protocol; result int sv[2];.DEHere the returned \fIsv\fP descriptors correspond to those obtained with\fIaccept\fP and \fIconnect\fP..PPThe call.DSpipe(pv)result int pv[2];.DEcreates a pair of SOCK_STREAM sockets in the UNIX domain,with pv[0] only writable and pv[1] only readable..NH 4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -