📄 2.t
字号:
.\" Copyright (c) 1986, 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.t 8.1 (Berkeley) 8/14/93.\".\".ds RH "Basics.bp.nr H1 2.nr H2 0.\" The next line is a major hack to get around internal changes in the groff.\" implementation of .NH..nr nh*hl 1.bp.LG.B.ce2. BASICS.sp 2.R.NL.PPThe basic building block for communication is the \fIsocket\fP.A socket is an endpoint of communication to which a name maybe \fIbound\fP. Each socket in use has a \fItype\fPand one or more associated processes. Sockets exist within\fIcommunication domains\fP. A communication domain is anabstraction introduced to bundle common properties ofprocesses communicating through sockets.One such property is the scheme used to name sockets. Forexample, in the UNIX communication domain sockets arenamed with UNIX path names; e.g. asocket may be named \*(lq/dev/foo\*(rq. Sockets normallyexchange data only withsockets in the same domain (it may be possible to cross domainboundaries, but only if some translation process isperformed). The4.4BSD IPC facilities support four separate communication domains:the UNIX domain, for on-system communication;the Internet domain, which is used byprocesses which communicateusing the Internet standard communication protocols;the NS domain, which is used by processes whichcommunicate using the Xerox standard communicationprotocols*;.FS* See \fIInternet Transport Protocols\fP, Xerox System IntegrationStandard (XSIS)028112 for more information. This document isalmost a necessity for one trying to write NS applications..FEand the ISO OSI protocols, which are not documented in this tutorial.The underlying communicationfacilities provided by these domains have a significant influenceon the internal system implementation as well as the interface tosocket facilities available to a user. An example of thelatter is that a socket \*(lqoperating\*(rq in the UNIX domainsees a subset of the error conditions which are possiblewhen operating in the Internet (or NS) domain..NH 2Socket types.PPSockets aretyped according to the communication properties visible to auser. Processes are presumed to communicate only between sockets ofthe same type, although there isnothing that prevents communication between sockets of differenttypes should the underlying communicationprotocols support this..PPFour types of sockets currently are available to a user.A \fIstream\fP socket provides for the bidirectional, reliable,sequenced, and unduplicated flow of data without record boundaries.Aside from the bidirectionality of data flow, a pair of connectedstream sockets provides an interface nearly identical to that of pipes\(dg..FS\(dg In the UNIX domain, in fact, the semantics are identical and,as one might expect, pipes have been implemented internallyas simply a pair of connected stream sockets..FE.PPA \fIdatagram\fP socket supports bidirectional flow of data whichis not promised to be sequenced, reliable, or unduplicated. That is, a processreceiving messages on a datagram socket may find messages duplicated, and, possibly,in an order different from the order in which it was sent. An important characteristic of a datagramsocket is that record boundaries in data are preserved. Datagramsockets closely model the facilities found in many contemporarypacket switched networks such as the Ethernet..PPA \fIraw\fP socket provides users access tothe underlying communicationprotocols which support socket abstractions.These sockets are normally datagram oriented, though theirexact characteristics are dependent on the interface provided bythe protocol. Raw sockets are not intended for the general user; theyhave been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the moreesoteric facilities of an existing protocol. The use of raw socketsis considered in section 5..PPA \fIsequenced packet\fP socket is similar to a stream socket,with the exception that record boundaries are preserved. This interface is provided only as part of the NS socket abstraction,and is very important in most serious NS applications.Sequenced-packet sockets allow the user to manipulate theSPP or IDP headers on a packet or a group of packets eitherby writing a prototype header along with whatever data isto be sent, or by specifying a default header to be used withall outgoing data, and allows the user to receive the headerson incoming packets. The use of these options is considered insection 5..PPAnother potential socket type which has interesting properties isthe \fIreliably deliveredmessage\fP socket.The reliably delivered message socket hassimilar properties to a datagram socket, but withreliable delivery. There is currently no support for thistype of socket, but a reliably delivered message protocolsimilar to Xerox's Packet Exchange Protocol (PEX) may besimulated at the user level. More information on this topiccan be found in section 5..NH 2Socket creation.PPTo create a socket the \fIsocket\fP system call is used:.DSs = socket(domain, type, protocol);.DEThis call requests that the system create a socket in the specified\fIdomain\fP and of the specified \fItype\fP. A particular protocol mayalso be requested. If the protocol is left unspecified (a valueof 0), the system will select an appropriate protocol from thoseprotocols which comprise the communication domain and whichmay be used to support the requested socket type. The user isreturned a descriptor (a small integer number) which may be usedin later system calls which operate on sockets. The domain is specified asone of the manifest constants defined in the file <\fIsys/socket.h\fP>.For the UNIX domain the constant is AF_UNIX*; for the Internet.FS* The manifest constants are named AF_whatever as they indicatethe ``address format'' to use in interpreting names..FEdomain AF_INET; and for the NS domain, AF_NS. The socket types are also defined in this fileand one of SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, or SOCK_SEQPACKETmust be specified.To create a stream socket in the Internet domain the followingcall might be used:.DSs = socket(AF_INET, SOCK_STREAM, 0);.DEThis call would result in a stream socket being created with the TCPprotocol providing the underlying communication support. Tocreate a datagram socket for on-machine use the call mightbe:.DSs = socket(AF_UNIX, SOCK_DGRAM, 0);.DE.PPThe default protocol (used when the \fIprotocol\fP argument to the\fIsocket\fP call is 0) should be correct for most everysituation. However, it is possible to specify a protocolother than the default; this will be covered insection 5..PPThere are several reasons a socket call may fail. Aside fromthe rare occurrence of lack of memory (ENOBUFS), a socketrequest may fail due to a request for an unknown protocol(EPROTONOSUPPORT), or a request for a type of socket forwhich there is no supporting protocol (EPROTOTYPE). .NH 2Binding local names.PPA socket is created without a name. Until a name is boundto a socket, processes have no way to reference it and, consequently,no messages may be received on it.Communicating processes are boundby an \fIassociation\fP. In the Internet and NS domains,an association is composed of local and foreignaddresses, and local and foreign ports,while in the UNIX domain, an association is composed oflocal and foreign path names (the phrase ``foreign pathname''means a pathname created by a foreign process, not a pathnameon a foreign system).In most domains, associations must be unique.In the Internet domain theremay never be duplicate <protocol, local address, local port, foreignaddress, foreign port> tuples. UNIX domain sockets need not alwaysbe bound to a name, but when boundthere may never be duplicate <protocol, local pathname, foreignpathname> tuples.The pathnames may not refer to filesalready existing on the systemin 4.3; the situation may change in future releases..PPThe \fIbind\fP system call allows a process to specify half ofan association, <local address, local port>(or <local pathname>), while the \fIconnect\fPand \fIaccept\fP primitives are used to complete a socket's association..PPIn the Internet domain,binding names to sockets can be fairly complex.Fortunately, it is usually not necessary to specifically bind anaddress and port number to a socket, because the\fIconnect\fP and \fIsend\fP calls will automaticallybind an appropriate address if they are used with anunbound socket. The process of binding names to NSsockets is similar in most ways to that ofbinding names to Internet sockets..PPThe \fIbind\fP system call is used as follows:.DSbind(s, name, namelen);.DEThe bound name is a variable length byte string which is interpretedby the supporting protocol(s). Its interpretation may vary fromcommunication domain to communication domain (this is one ofthe properties which comprise the \*(lqdomain\*(rq).As mentioned, in theInternet domain names contain an Internet address and portnumber. NS domain names contain an NS address andport number. In the UNIX domain, names contain a path name anda family, which is always AF_UNIX. If one wanted to bindthe name \*(lq/tmp/foo\*(rq to a UNIX domain socket, thefollowing code would be used*:.FS* Note that, although the tendency here is to call the \*(lqaddr\*(rqstructure \*(lqsun\*(rq, doing so would cause problems if the codewere ever ported to a Sun workstation..FE.DS#include <sys/un.h> ...struct sockaddr_un addr; ...strcpy(addr.sun_path, "/tmp/foo");addr.sun_family = AF_UNIX;bind(s, (struct sockaddr *) &addr, strlen(addr.sun_path) + sizeof (addr.sun_len) + sizeof (addr.sun_family));.DENote that in determining the size of a UNIX domain address nullbytes are not counted, which is why \fIstrlen\fP is used. Inthe current implementation of UNIX domain IPC,the file namereferred to in \fIaddr.sun_path\fP is created as a socketin the system file space.The caller must, therefore, havewrite permission in the directory where\fIaddr.sun_path\fP is to reside, and this file should be deleted by thecaller when it is no longer needed. Future versions of 4BSDmay not create this file..PPIn binding an Internet address things become morecomplicated. The actual call is similar,.DS#include <sys/types.h>#include <netinet/in.h> ...struct sockaddr_in sin; ...bind(s, (struct sockaddr *) &sin, sizeof (sin));.DEbut the selection of what to place in the address \fIsin\fPrequires some discussion. We will come back to the problemof formulating Internet addresses in section 3 when the library routines used in name resolution are discussed..PPBinding an NS address to a socket is even moredifficult,especially since the Internet library routines do notwork with NS hostnames. The actual call is again similar:.DS#include <sys/types.h>#include <netns/ns.h> ...struct sockaddr_ns sns; ...bind(s, (struct sockaddr *) &sns, sizeof (sns));.DEAgain, discussion of what to place in a \*(lqstruct sockaddr_ns\*(rqwill be deferred to section 3..NH 2Connection establishment.PPConnection establishment is usually asymmetric,with one process a \*(lqclient\*(rq and the other a \*(lqserver\*(rq.The server, when willing to offer its advertised services,binds a socket to a well-known address associated with the serviceand then passively \*(lqlistens\*(rq on its socket.It is then possible for an unrelated process to rendezvouswith the server.The client requests services from the server by initiating a\*(lqconnection\*(rq to the server's socket.On the client side the \fIconnect\fP call isused to initiate a connection. Using the UNIX domain, thismight appear as,.DSstruct sockaddr_un server; ...connect(s, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof (server.sun_family));.DEwhile in the Internet domain,.DSstruct sockaddr_in server; ...connect(s, (struct sockaddr *)&server, sizeof (server));.DEand in the NS domain,.DSstruct sockaddr_ns server; ...connect(s, (struct sockaddr *)&server, sizeof (server));.DEwhere \fIserver\fP in the example above would contain either the UNIXpathname, Internet address and port number, or NS address andport number of the server to which theclient process wishes to speak.If the client process's socket is unbound at the time ofthe connect call,the system will automatically select and bind a name tothe socket if necessary; c.f. section 5.4.This is the usual way that local addresses are boundto a socket..PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -