📄 socket.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>socket</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4>NAME</h4><blockquote>socket - create an endpoint for communication</blockquote><h4>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="syssocket.h.html">sys/socket.h</a>>int socket(int <I>domain</I>, int <I>type</I>, int <I>protocol</I>);</code></pre></blockquote><h4>DESCRIPTION</h4><blockquote>The<i>socket()</i>function creates an unbound socket in a communications domain, and returns afile descriptor that can be used in later function calls that operate onsockets.<p>The function takes the following arguments:<dl compact><dt><I>domain</I><dd>Specifies the communications domain in which a socket is to be created.<dt><I>type</I><dd>Specifies the type of socket to be created.<dt><I>protocol</I><dd>Specifies a particular protocol to be used with the socket.Specifying a <I>protocol</I> of 0 causes<i>socket()</i>to use an unspecified default protocol appropriate for therequested socket type.</dl><p>The <I>domain</I> argument specifies the address family used in thecommunications domain. The address families supported by the system areimplementation-dependent.<p>The<i><a href="syssocket.h.html"><sys/socket.h></a></i>header defines at least the following values for the<I>domain</I>argument:<dl compact><dt>AF_UNIX<dd>File system pathnames.<dt>AF_INET<dd>Internet address.</dl><p>The <I>type</I> argument specifies the socket type, which determinesthe semantics of communication over the socket. The socket typessupported by the system are implementation-dependent. Possiblesocket types include:<dl compact><dt>SOCK_STREAM<dd>Provides sequenced, reliable, bidirectional, connection-mode byte streams,and may provide a transmission mechanism for out-of-band data.<dt>SOCK_DGRAM<dd>Provides datagrams, which are connectionless-mode, unreliable messages of fixedmaximum length.<dt>SOCK_SEQPACKET<dd>Provides sequenced, reliable, bidirectional, connection-mode transmissionpath for records. A record can be sent using one or more output operationsand received using one or more input operations, but a single operation nevertransfers part of more than one record. Record boundaries are visible tothe receiver via the MSG_EOR flag.</dl><p>If the <I>protocol</I> argument is non-zero, it must specify aprotocol that is supported by the address family. The protocolssupported by the system are implementation-dependent.<p>The process may need to have appropriate privileges to use the<i>socket()</i>function or to create some sockets.</blockquote><h4>RETURN VALUE</h4><blockquote>Upon successful completion,<i>socket()</i>returns a nonnegative integer, the socket file descriptor. Otherwise a valueof -1 is returned and <I>errno</I> is set to indicate the error.</blockquote><h4>ERRORS</h4><blockquote>The<i>socket()</i>function will fail if:<dl compact><dt>[EAFNOSUPPORT]<dd>The implementation does not support the specified address family.<dt>[EMFILE]<dd>No more file descriptors are available for this process.<dt>[ENFILE]<dd>No more file descriptors are available for the system.<dt>[EPROTONOSUPPORT]<dd><br>The protocol is not supported by the address family, orthe protocol is not supported by the implementation.<dt>[EPROTOTYPE]<dd>The socket type is not supported by the protocol.</dl><p>The<i>socket()</i>function may fail if:<dl compact><dt>[EACCES]<dd>The process does not have appropriate privileges.<dt>[ENOBUFS]<dd>Insufficient resources were available in the system to perform the operation.<dt>[ENOMEM]<dd>Insufficient memory was available to fulfill the request.<dt>[ENOSR]<dd>There were insufficient STREAMS resources available for the operation tocomplete.</dl></blockquote><h4>APPLICATION USAGE</h4><blockquote>The documentation for specific address families specify which protocols eachaddress family supports. The documentation for specific protocols specifywhich socket types each protocol supports.<p>The application can determine if an address family is supportedby trying to create a socket with<I>domain</I>set to the protocol in question.</blockquote><h4>SEE ALSO</h4><blockquote><i><a href="accept.html">accept()</a></i>,<i><a href="bind.html">bind()</a></i>,<i><a href="connect.html">connect()</a></i>,<i><a href="getsockname.html">getsockname()</a></i>,<i><a href="getsockopt.html">getsockopt()</a></i>,<i><a href="listen.html">listen()</a></i>,<i><a href="recv.html">recv()</a></i>,<i><a href="recvfrom.html">recvfrom()</a></i>,<i><a href="recvmsg.html">recvmsg()</a></i>,<i><a href="send.html">send()</a></i>,<i><a href="sendmsg.html">sendmsg()</a></i>,<i><a href="setsockopt.html">setsockopt()</a></i>,<i><a href="shutdown.html">shutdown()</a></i>,<i><a href="socketpair.html">socketpair()</a></i>,<i><a href="netinetin.h.html"><netinet/in.h></a></i>,<i><a href="syssocket.h.html"><sys/socket.h></a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -