man-pages/man2/socket.2.html
2021-03-31 01:06:50 +01:00

716 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SOCKET</TITLE>
</HEAD><BODY>
<H1>SOCKET</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
socket - create an endpoint for communication
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B> /* See NOTES */
<BR>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<P>
<B>int socket(int </B><I>domain</I><B>, int </B><I>type</I><B>, int </B><I>protocol</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>socket</B>()
creates an endpoint for communication and returns a file descriptor
that refers to that endpoint.
The file descriptor returned by a successful call will be
the lowest-numbered file descriptor not currently open for the process.
<P>
The
<I>domain</I>
argument specifies a communication domain; this selects the protocol
family which will be used for communication.
These families are defined in
<I>&lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</I>.
The formats currently understood by the Linux kernel include:
<TABLE>
<TR VALIGN=top><TD>Name&nbsp;</TD><TD>Purpose</TD><TD>Man page<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_UNIX</B>
&nbsp;</TD><TD>
Local communication
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_LOCAL</B>
&nbsp;</TD><TD>
Synonym for
<B>AF_UNIX</B>
</TD><TD>
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_INET</B>
&nbsp;</TD><TD>IPv4 Internet protocols</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_AX25</B>
&nbsp;</TD><TD>
Amateur radio AX.25 protocol
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?4+ax25">ax25</A></B>(4)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_IPX</B>
&nbsp;</TD><TD>IPX - Novell protocols</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_APPLETALK</B>
&nbsp;</TD><TD>AppleTalk</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+ddp">ddp</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_X25</B>
&nbsp;</TD><TD>ITU-T X.25 / ISO-8208 protocol</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+x25">x25</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_INET6</B>
&nbsp;</TD><TD>IPv6 Internet protocols</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+ipv6">ipv6</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_DECnet</B>
&nbsp;</TD><TD>
DECet protocol sockets
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_KEY</B>
&nbsp;</TD><TD>
Key management protocol, originally developed for usage with IPsec
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_NETLINK</B>
&nbsp;</TD><TD>
Kernel user interface device
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_PACKET</B>
&nbsp;</TD><TD>
Low-level packet interface
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_RDS</B>
&nbsp;</TD><TD>
Reliable Datagram Sockets (RDS) protocol
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+rds">rds</A></B>(7)
<BR>
<B><A HREF="/cgi-bin/man/man2html?7+rds-rdma">rds-rdma</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_PPPOX</B>
&nbsp;</TD><TD>
Generic PPP transport layer, for setting up L2 tunnels
(L2TP and PPPoE)
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_LLC</B>
&nbsp;</TD><TD>
Logical link control (IEEE 802.2 LLC) protocol
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_IB</B>
&nbsp;</TD><TD>
InfiniBand native addressing
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_MPLS</B>
&nbsp;</TD><TD>
Multiprotocol Label Switching
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_CAN</B>
&nbsp;</TD><TD>
Controller Area Network automotive bus protocol
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_TIPC</B>
&nbsp;</TD><TD>
TIPC, &quot;cluster domain sockets&quot; protocol
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_BLUETOOTH</B>
&nbsp;</TD><TD>
Bluetooth low-level socket protocol
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_ALG</B>
&nbsp;</TD><TD>
Interface to kernel crypto API
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_VSOCK</B>
&nbsp;</TD><TD>
VSOCK (originally &quot;VMWare VSockets&quot;) protocol
for hypervisor-guest communication
</TD><TD>
<B><A HREF="/cgi-bin/man/man2html?7+vsock">vsock</A></B>(7)
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_KCM</B>
&nbsp;</TD><TD>
KCM (kernel connection multiplexor) interface
</TD><TD><BR></TD></TR>
<TR VALIGN=top><TD>
<B>AF_XDP</B>
&nbsp;</TD><TD>
XDP (express data path) interface
</TD><TD><BR></TD></TR>
</TABLE>
<P>
Further details of the above address families,
as well as information on several other address families, can be found in
<B><A HREF="/cgi-bin/man/man2html?7+address_families">address_families</A></B>(7).
<P>
The socket has the indicated
<I>type</I>,
which specifies the communication semantics.
Currently defined types
are:
<DL COMPACT>
<DT id="1"><B>SOCK_STREAM</B>
<DD>
Provides sequenced, reliable, two-way, connection-based byte streams.
An out-of-band data transmission mechanism may be supported.
<DT id="2"><B>SOCK_DGRAM</B>
<DD>
Supports datagrams (connectionless, unreliable messages of a fixed
maximum length).
<DT id="3"><B>SOCK_SEQPACKET</B>
<DD>
Provides a sequenced, reliable, two-way connection-based data
transmission path for datagrams of fixed maximum length; a consumer is
required to read an entire packet with each input system call.
<DT id="4"><B>SOCK_RAW</B>
<DD>
Provides raw network protocol access.
<DT id="5"><B>SOCK_RDM</B>
<DD>
Provides a reliable datagram layer that does not guarantee ordering.
<DT id="6"><B>SOCK_PACKET</B>
<DD>
Obsolete and should not be used in new programs;
see
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7).
</DL>
<P>
Some socket types may not be implemented by all protocol families.
<P>
Since Linux 2.6.27, the
<I>type</I>
argument serves a second purpose:
in addition to specifying a socket type,
it may include the bitwise OR of any of the following values,
to modify the behavior of
<B>socket</B>():
<DL COMPACT>
<DT id="7"><B>SOCK_NONBLOCK</B>
<DD>
Set the
<B>O_NONBLOCK</B>
file status flag on the open file description (see
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
referred to by the new file descriptor.
Using this flag saves extra calls to
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
to achieve the same result.
<DT id="8"><B>SOCK_CLOEXEC</B>
<DD>
Set the close-on-exec
(<B>FD_CLOEXEC</B>)
flag on the new file descriptor.
See the description of the
<B>O_CLOEXEC</B>
flag in
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
for reasons why this may be useful.
</DL>
<P>
The
<I>protocol</I>
specifies a particular protocol to be used with the socket.
Normally only a single protocol exists to support a particular
socket type within a given protocol family, in which case
<I>protocol</I>
can be specified as 0.
However, it is possible that many protocols may exist, in
which case a particular protocol must be specified in this manner.
The protocol number to use is specific to the ``communication domain''
in which communication is to take place; see
<B><A HREF="/cgi-bin/man/man2html?5+protocols">protocols</A></B>(5).
See
<B><A HREF="/cgi-bin/man/man2html?3+getprotoent">getprotoent</A></B>(3)
on how to map protocol name strings to protocol numbers.
<P>
Sockets of type
<B>SOCK_STREAM</B>
are full-duplex byte streams.
They do not preserve
record boundaries.
A stream socket must be in
a
<I>connected</I>
state before any data may be sent or received on it.
A connection to
another socket is created with a
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
call.
Once connected, data may be transferred using
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
calls or some variant of the
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2)
calls.
When a session has been completed a
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)
may be performed.
Out-of-band data may also be transmitted as described in
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2)
and received as described in
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2).
<P>
The communications protocols which implement a
<B>SOCK_STREAM</B>
ensure that data is not lost or duplicated.
If a piece of data for which
the peer protocol has buffer space cannot be successfully transmitted
within a reasonable length of time, then the connection is considered
to be dead.
When
<B>SO_KEEPALIVE</B>
is enabled on the socket the protocol checks in a protocol-specific
manner if the other end is still alive.
A
<B>SIGPIPE</B>
signal is raised if a process sends or receives
on a broken stream; this causes naive processes,
which do not handle the signal, to exit.
<B>SOCK_SEQPACKET</B>
sockets employ the same system calls as
<B>SOCK_STREAM</B>
sockets.
The only difference is that
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
calls will return only the amount of data requested,
and any data remaining in the arriving packet will be discarded.
Also all message boundaries in incoming datagrams are preserved.
<P>
<B>SOCK_DGRAM</B>
and
<B>SOCK_RAW</B>
sockets allow sending of datagrams to correspondents named in
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2)
calls.
Datagrams are generally received with
<B><A HREF="/cgi-bin/man/man2html?2+recvfrom">recvfrom</A></B>(2),
which returns the next datagram along with the address of its sender.
<P>
<B>SOCK_PACKET</B>
is an obsolete socket type to receive raw packets directly from the
device driver.
Use
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7)
instead.
<P>
An
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
<B>F_SETOWN</B>
operation can be used to specify a process or process group to receive a
<B>SIGURG</B>
signal when the out-of-band data arrives or
<B>SIGPIPE</B>
signal when a
<B>SOCK_STREAM</B>
connection breaks unexpectedly.
This operation may also be used to set the process or process group
that receives the I/O and asynchronous notification of I/O events via
<B>SIGIO</B>.
Using
<B>F_SETOWN</B>
is equivalent to an
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2)
call with the
<B>FIOSETOWN</B>
or
<B>SIOCSPGRP</B>
argument.
<P>
When the network signals an error condition to the protocol module (e.g.,
using an ICMP message for IP) the pending error flag is set for the socket.
The next operation on this socket will return the error code of the pending
error.
For some protocols it is possible to enable a per-socket error queue
to retrieve detailed information about the error; see
<B>IP_RECVERR</B>
in
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
<P>
The operation of sockets is controlled by socket level
<I>options</I>.
These options are defined in
<I>&lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</I>.
The functions
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
are used to set and get options.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, a file descriptor for the new socket is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="9"><B>EACCES</B>
<DD>
Permission to create a socket of the specified type and/or protocol
is denied.
<DT id="10"><B>EAFNOSUPPORT</B>
<DD>
The implementation does not support the specified address family.
<DT id="11"><B>EINVAL</B>
<DD>
Unknown protocol, or protocol family not available.
<DT id="12"><B>EINVAL</B>
<DD>
Invalid flags in
<I>type</I>.
<DT id="13"><B>EMFILE</B>
<DD>
The per-process limit on the number of open file descriptors has been reached.
<DT id="14"><B>ENFILE</B>
<DD>
The system-wide limit on the total number of open files has been reached.
<DT id="15"><B>ENOBUFS</B> or <B>ENOMEM</B>
<DD>
Insufficient memory is available.
The socket cannot be
created until sufficient resources are freed.
<DT id="16"><B>EPROTONOSUPPORT</B>
<DD>
The protocol type or the specified protocol is not
supported within this domain.
</DL>
<P>
Other errors may be generated by the underlying protocol modules.
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, 4.4BSD.
<P>
The
<B>SOCK_NONBLOCK</B>
and
<B>SOCK_CLOEXEC</B>
flags are Linux-specific.
<P>
<B>socket</B>()
appeared in 4.2BSD.
It is generally portable to/from
non-BSD systems supporting clones of the BSD socket layer (including
System&nbsp;V variants).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1 does not require the inclusion of
<I>&lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</I>,
and this header file is not required on Linux.
However, some historical (BSD) implementations required this header
file, and portable applications are probably wise to include it.
<P>
The manifest constants used under 4.x BSD for protocol families
are
<B>PF_UNIX</B>,
<B>PF_INET</B>,
and so on, while
<B>AF_UNIX</B>,
<B>AF_INET</B>,
and so on are used for address
families.
However, already the BSD man page promises: &quot;The protocol
family generally is the same as the address family&quot;, and subsequent
standards use AF_* everywhere.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
An example of the use of
<B>socket</B>()
is shown in
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3).
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getpeername">getpeername</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+shutdown">shutdown</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socketpair">socketpair</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+getprotoent">getprotoent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+address_families">address_families</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+udp">udp</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)
<P>
"An Introductory 4.3BSD Interprocess Communication Tutorial"
and
"BSD Interprocess Communication Tutorial",
reprinted in
<I>UNIX Programmer's Supplementary Documents Volume 1.</I>
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="17"><A HREF="#lbAB">NAME</A><DD>
<DT id="18"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="19"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="20"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="21"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="22"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="23"><A HREF="#lbAH">NOTES</A><DD>
<DT id="24"><A HREF="#lbAI">EXAMPLE</A><DD>
<DT id="25"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="26"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>