⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsh_chap02_10.html

📁 posix标准英文,html格式
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>System Interfaces Chapter 2</title></head><body><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition</font></center><hr size="2" noshade><h3><a name="tag_02_10">   2.10 </a>Sockets</h3><p>A socket is an endpoint for communication using the facilities described in this section. A socket is created with a specificsocket type, described in <a href="#tag_02_10_06">Socket Types</a> , and is associated with a specific protocol, detailed in <ahref="#tag_02_10_03">Protocols</a>. A socket is accessed via a file descriptor obtained when the socket is created.</p><h4><a name="tag_02_10_01">  2.10.1 </a>Address Families</h4><p>All network protocols are associated with a specific address family. An address family provides basic services to the protocolimplementation to allow it to function within a specific network environment. These services may include packet fragmentation andreassembly, routing, addressing, and basic transport. An address family is normally comprised of a number of protocols, one persocket type. Each protocol is characterized by an abstract socket type. It is not required that an address family support allsocket types. An address family may contain multiple protocols supporting the same socket abstraction.</p><p><a href="#tag_02_10_17">Use of Sockets for Local UNIX Connections</a> , <a href="#tag_02_10_19">Use of Sockets over InternetProtocols Based on IPv4</a> , and <a href="#tag_02_10_20">Use of Sockets over Internet Protocols Based on IPv6</a> , respectively,describe the use of sockets for local UNIX connections, for Internet protocols based on IPv4, and for Internet protocols based onIPv6.</p><h4><a name="tag_02_10_02">  2.10.2 </a>Addressing</h4><p>An address family defines the format of a socket address. All network addresses are described using a general structure, calleda <b>sockaddr</b>, as defined in the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/sys/socket.h.html"><i>&lt;sys/socket.h&gt;</i></a>. However, each address family imposes finer and more specificstructure, generally defining a structure with fields specific to the address family. The field <i>sa_family</i> in the<b>sockaddr</b> structure contains the address family identifier, specifying the format of the <i>sa_data</i> area. The size of the<i>sa_data</i> area is unspecified.</p><h4><a name="tag_02_10_03">  2.10.3 </a>Protocols</h4><p>A protocol supports one of the socket abstractions detailed in <a href="#tag_02_10_06">Socket Types</a>. Selecting a protocolinvolves specifying the address family, socket type, and protocol number to the <a href="../functions/socket.html"><i>socket</i>()</a> function. Certain semantics of the basic socket abstractions are protocol-specific.All protocols are expected to support the basic model for their particular socket type, but may, in addition, provide non-standardfacilities or extensions to a mechanism.</p><h4><a name="tag_02_10_04">  2.10.4 </a>Routing</h4><p>Sockets provides packet routing facilities. A routing information database is maintained, which is used in selecting theappropriate network interface when transmitting packets.</p><h4><a name="tag_02_10_05">  2.10.5 </a>Interfaces</h4><p>Each network interface in a system corresponds to a path through which messages can be sent and received. A network interfaceusually has a hardware device associated with it, though certain interfaces such as the loopback interface, do not.</p><h4><a name="tag_02_10_06">  2.10.6 </a>Socket Types</h4><p>A socket is created with a specific type, which defines the communication semantics and which allows the selection of anappropriate communication protocol. Four types are defined: <sup>[<a href="javascript:open_code('RS')">RS</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;SOCK_RAW, <img src="../images/opt-end.gif" alt="[Option End]"border="0"> SOCK_STREAM, SOCK_SEQPACKET, and SOCK_DGRAM. Implementations may specify additional socket types.</p><p>The SOCK_STREAM socket type provides reliable, sequenced, full-duplex octet streams between the socket and a peer to which thesocket is connected. A socket of type SOCK_STREAM must be in a connected state before any data may be sent or received. Recordboundaries are not maintained; data sent on a stream socket using output operations of one size may be received using inputoperations of smaller or larger sizes without loss of data. Data may be buffered; successful return from an output function doesnot imply that the data has been delivered to the peer or even transmitted from the local system. If data cannot be successfullytransmitted within a given time then the connection is considered broken, and subsequent operations shall fail. A SIGPIPE signal israised if a thread sends on a broken stream (one that is no longer connected). Support for an out-of-band data transmissionfacility is protocol-specific.</p><p>The SOCK_SEQPACKET socket type is similar to the SOCK_STREAM type, and is also connection-oriented. The only difference betweenthese types is that record boundaries are maintained using the SOCK_SEQPACKET type. A record can be sent using one or more outputoperations and received using one or more input operations, but a single operation never transfers parts of more than one record.Record boundaries are visible to the receiver via the MSG_EOR flag in the received message flags returned by the <a href="../functions/recvmsg.html"><i>recvmsg</i>()</a> function. It is protocol-specific whether a maximum record size is imposed.</p><p>The SOCK_DGRAM socket type supports connectionless data transfer which is not necessarily acknowledged or reliable. Datagramsmay be sent to the address specified (possibly multicast or broadcast) in each output operation, and incoming datagrams may bereceived from multiple sources. The source address of each datagram is available when receiving the datagram. An application mayalso pre-specify a peer address, in which case calls to output functions shall send to the pre-specified peer. If a peer has beenspecified, only datagrams from that peer shall be received. A datagram must be sent in a single output operation, and must bereceived in a single input operation. The maximum size of a datagram is protocol-specific; with some protocols, the limit isimplementation-defined. Output datagrams may be buffered within the system; thus, a successful return from an output function doesnot guarantee that a datagram is actually sent or received. However, implementations should attempt to detect any errors possiblebefore the return of an output function, reporting any error by an unsuccessful return value.</p><p><sup>[<a href="javascript:open_code('RS')">RS</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> TheSOCK_RAW socket type is similar to the SOCK_DGRAM type. It differs in that it is normally used with communication providers thatunderlie those used for the other socket types. For this reason, the creation of a socket with type SOCK_RAW shall requireappropriate privilege. The format of datagrams sent and received with this socket type generally include specific protocol headers,and the formats are protocol-specific and implementation-defined. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><h4><a name="tag_02_10_07">  2.10.7 </a>Socket I/O Mode</h4><p>The I/O mode of a socket is described by the O_NONBLOCK file status flag which pertains to the open file description for thesocket. This flag is initially off when a socket is created, but may be set and cleared by the use of the F_SETFL command of the <ahref="../functions/fcntl.html"><i>fcntl</i>()</a> function.</p><p>When the O_NONBLOCK flag is set, functions that would normally block until they are complete shall either return immediatelywith an error, or shall complete asynchronously to the execution of the calling process. Data transfer operations (the <a href="../functions/read.html"><i>read</i>()</a>, <a href="../functions/write.html"><i>write</i>()</a>, <a href="../functions/send.html"><i>send</i>()</a>, and <a href="../functions/recv.html"><i>recv</i>()</a> functions) shall completeimmediately, transfer only as much as is available, and then return without blocking, or return an error indicating that notransfer could be made without blocking. The <a href="../functions/connect.html"><i>connect</i>()</a> function initiates aconnection and shall return without blocking when O_NONBLOCK is set; it shall return the error [EINPROGRESS] to indicate that theconnection was initiated successfully, but that it has not yet completed.</p><h4><a name="tag_02_10_08">  2.10.8 </a>Socket Owner</h4><p>The owner of a socket is unset when a socket is created. The owner may be set to a process ID or process group ID using theF_SETOWN command of the <a href="../functions/fcntl.html"><i>fcntl</i>()</a> function.</p><h4><a name="tag_02_10_09">  2.10.9 </a>Socket Queue Limits</h4><p>The transmit and receive queue sizes for a socket are set when the socket is created. The default sizes used are bothprotocol-specific and implementation-defined. The sizes may be changed using the <a href="../functions/setsockopt.html"><i>setsockopt</i>()</a> function.</p><h4><a name="tag_02_10_10">  2.10.10 </a>Pending Error</h4><p>Errors may occur asynchronously, and be reported to the socket in response to input from the network protocol. The socket storesthe pending error to be reported to a user of the socket at the next opportunity. The error is returned in response to a subsequent<a href="../functions/send.html"><i>send</i>()</a>, <a href="../functions/recv.html"><i>recv</i>()</a>, or <a href="../functions/getsockopt.html"><i>getsockopt</i>()</a> operation on the socket, and the pending error is then cleared.</p><h4><a name="tag_02_10_11">  2.10.11 </a>Socket Receive Queue</h4><p>A socket has a receive queue that buffers data when it is received by the system until it is removed by a receive call.Depending on the type of the socket and the communication provider, the receive queue may also contain ancillary data such as theaddressing and other protocol data associated with the normal data in the queue, and may contain out-of-band or expedited data. Thelimit on the queue size includes any normal, out-of-band data, datagram source addresses, and ancillary data in the queue. Thedescription in this section applies to all sockets, even though some elements cannot be present in some instances.</p><p>The contents of a receive buffer are logically structured as a series of data segments with associated ancillary data and otherinformation. A data segment may contain normal data or out-of-band data, but never both. A data segment may complete a record ifthe protocol supports records (always true for types SOCK_SEQPACKET and SOCK_DGRAM). A record may be stored as more than onesegment; the complete record might never be present in the receive buffer at one time, as a portion might already have beenreturned to the application, and another portion might not yet have been received from the communications provider. A data segmentmay contain ancillary protocol data, which is logically associated with the segment. Ancillary data is received as if it werequeued along with the first normal data octet in the segment (if any). A segment may contain ancillary data only, with no normal orout-of-band data. For the purposes of this section, a datagram is considered to be a data segment that terminates a record, andthat includes a source address as a special type of ancillary data. Data segments are placed into the queue as data is delivered tothe socket by the protocol. Normal data segments are placed at the end of the queue as they are delivered. If a new segmentcontains the same type of data as the preceding segment and includes no ancillary data, and if the preceding segment does notterminate a record, the segments are logically merged into a single segment.</p><p>The receive queue is logically terminated if an end-of-file indication has been received or a connection has been terminated. Asegment shall be considered to be terminated if another segment follows it in the queue, if the segment completes a record, or ifan end-of-file or other connection termination has been reported. The last segment in the receive queue shall also be considered tobe terminated while the socket has a pending error to be reported.</p><p>A receive operation shall never return data or ancillary data from more than one segment.</p><h4><a name="tag_02_10_12">  2.10.12 </a>Socket Out-of-Band Data State</h4><p>The handling of received out-of-band data is protocol-specific. Out-of-band data may be placed in the socket receive queue,either at the end of the queue or before all normal data in the queue. In this case, out-of-band data is returned to an applicationprogram by a normal receive call. Out-of-band data may also be queued separately rather than being placed in the socket receivequeue, in which case it shall be returned only in response to a receive call that requests out-of-band data. It isprotocol-specific whether an out-of-band data mark is placed in the receive queue to demarcate data preceding the out-of-band dataand following the out-of-band data. An out-of-band data mark is logically an empty data segment that cannot be merged with othersegments in the queue. An out-of-band data mark is never returned in response to an input operation. The <a href="../functions/sockatmark.html"><i>sockatmark</i>()</a> function can be used to test whether an out-of-band data mark is the firstelement in the queue. If an out-of-band data mark is the first element in the queue when an input function is called without theMSG_PEEK option, the mark is removed from the queue and the following data (if any) is processed as if the mark had not beenpresent.</p><h4><a name="tag_02_10_13">  2.10.13 </a>Connection Indication Queue</h4><p>Sockets that are used to accept incoming connections maintain a queue of outstanding connection indications. This queue is alist of connections that are awaiting acceptance by the application; see <a href="listen.html"><i>listen</i>()</a>.</p><h4><a name="tag_02_10_14">  2.10.14 </a>Signals</h4><p>One category of event at the socket interface is the generation of signals. These signals report protocol events or processerrors relating to the state of the socket. The generation or delivery of a signal does not change the state of the socket,although the generation of the signal may have been caused by a state change.</p><p>The SIGPIPE signal shall be sent to a thread that attempts to send data on a socket that is no longer able to send. In addition,the send operation fails with the error [EPIPE].</p><p>If a socket has an owner, the SIGURG signal is sent to the owner of the socket when it is notified of expedited or out-of-banddata. The socket state at this time is protocol-dependent, and the status of the socket is specified in <a href="#tag_02_10_17">Useof Sockets for Local UNIX Connections</a> , <a href="#tag_02_10_19">Use of Sockets over Internet Protocols Based on IPv4</a> , and<a href="#tag_02_10_20">Use of Sockets over Internet Protocols Based on IPv6</a>. Depending on the protocol, the expedited datamay or may not have arrived at the time of signal generation.</p><h4><a name="tag_02_10_15">  2.10.15 </a>Asynchronous Errors</h4><p>If any of the following conditions occur asynchronously for a socket, the corresponding value listed below shall become thepending error for the socket:</p><dl compact><dt>[ECONNABORTED]</dt><dd>The connection was aborted locally.</dd><dt>[ECONNREFUSED]</dt><dd>For a connection-mode socket attempting a non-blocking connection, the attempt to connect was forcefully rejected. For aconnectionless-mode socket, an attempt to deliver a datagram was forcefully rejected.</dd><dt>[ECONNRESET]</dt><dd>The peer has aborted the connection.</dd><dt>[EHOSTDOWN]</dt>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -