1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" From: @(#)socket.2 8.1 (Berkeley) 6/4/93 29.\" 30.Dd January 15, 2023 31.Dt SOCKET 2 32.Os 33.Sh NAME 34.Nm socket 35.Nd create an endpoint for communication 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In sys/socket.h 40.Ft int 41.Fn socket "int domain" "int type" "int protocol" 42.Sh DESCRIPTION 43The 44.Fn socket 45system call 46creates an endpoint for communication and returns a descriptor. 47.Pp 48The 49.Fa domain 50argument specifies a communications domain within which 51communication will take place; this selects the protocol family 52which should be used. 53These families are defined in the include file 54.In sys/socket.h . 55The currently understood formats are: 56.Pp 57.Bd -literal -offset indent -compact 58PF_LOCAL Host-internal protocols (alias for PF_UNIX), 59PF_UNIX Host-internal protocols, 60PF_INET Internet version 4 protocols, 61PF_INET6 Internet version 6 protocols, 62PF_DIVERT Firewall packet diversion/re-injection, 63PF_ROUTE Internal routing protocol, 64PF_KEY Internal key-management function, 65PF_NETGRAPH Netgraph sockets, 66PF_NETLINK Netlink protocols, 67PF_BLUETOOTH Bluetooth protocols, 68PF_INET_SDP OFED socket direct protocol (IPv4), 69AF_HYPERV HyperV sockets 70.Ed 71.Pp 72Each protocol family is connected to an address family, which has the 73same name except that the prefix is 74.Dq Dv AF_ 75in place of 76.Dq Dv PF_ . 77Other protocol families may be also defined, beginning with 78.Dq Dv PF_ , 79with corresponding address families. 80.Pp 81The socket has the indicated 82.Fa type , 83which specifies the semantics of communication. 84Currently 85defined types are: 86.Pp 87.Bd -literal -offset indent -compact 88SOCK_STREAM Stream socket, 89SOCK_DGRAM Datagram socket, 90SOCK_RAW Raw-protocol interface, 91SOCK_SEQPACKET Sequenced packet stream 92.Ed 93.Pp 94A 95.Dv SOCK_STREAM 96type provides sequenced, reliable, 97two-way connection based byte streams. 98An out-of-band data transmission mechanism may be supported. 99A 100.Dv SOCK_DGRAM 101socket supports 102datagrams (connectionless, unreliable messages of 103a fixed (typically small) maximum length). 104A 105.Dv SOCK_SEQPACKET 106socket may provide a sequenced, reliable, 107two-way connection-based data transmission path for datagrams 108of fixed maximum length; a consumer may be required to read 109an entire packet with each read system call. 110This facility may have protocol-specific properties. 111.Dv SOCK_RAW 112sockets provide access to internal network protocols and interfaces. 113The 114.Dv SOCK_RAW 115type is available only to the super-user and is described in 116.Xr ip 4 117and 118.Xr ip6 4 . 119.Pp 120Additionally, the following flags are allowed in the 121.Fa type 122argument: 123.Pp 124.Bd -literal -offset indent -compact 125SOCK_CLOEXEC Set close-on-exec on the new descriptor, 126SOCK_NONBLOCK Set non-blocking mode on the new socket 127.Ed 128.Pp 129The 130.Fa protocol 131argument 132specifies a particular protocol to be used with the socket. 133Normally only a single protocol exists to support a particular 134socket type within a given protocol family. 135However, it is possible 136that many protocols may exist, in which case a particular protocol 137must be specified in this manner. 138The protocol number to use is 139particular to the 140.Dq "communication domain" 141in which communication 142is to take place; see 143.Xr protocols 5 . 144.Pp 145The 146.Fa protocol 147argument may be set to zero (0) to request the default 148implementation of a socket type for the protocol, if any. 149.Pp 150Sockets of type 151.Dv SOCK_STREAM 152are full-duplex byte streams, similar 153to pipes. 154A stream socket must be in a 155.Em connected 156state before any data may be sent or received 157on it. 158A connection to another socket is created with a 159.Xr connect 2 160system call. 161Once connected, data may be transferred using 162.Xr read 2 163and 164.Xr write 2 165calls or some variant of the 166.Xr send 2 167and 168.Xr recv 2 169functions. 170(Some protocol families, such as the Internet family, 171support the notion of an 172.Dq implied connect , 173which permits data to be sent piggybacked onto a connect operation by 174using the 175.Xr sendto 2 176system call.) 177When a session has been completed a 178.Xr close 2 179may be performed. 180Out-of-band data may also be transmitted as described in 181.Xr send 2 182and received as described in 183.Xr recv 2 . 184.Pp 185The communications protocols used to implement a 186.Dv SOCK_STREAM 187ensure that data 188is not lost or duplicated. 189If a piece of data for which the 190peer protocol has buffer space cannot be successfully transmitted 191within a reasonable length of time, then 192the connection is considered broken and calls 193will indicate an error with 194-1 returns and with 195.Er ETIMEDOUT 196as the specific code 197in the global variable 198.Va errno . 199The protocols optionally keep sockets 200.Dq warm 201by forcing transmissions 202roughly every minute in the absence of other activity. 203An error is then indicated if no response can be 204elicited on an otherwise 205idle connection for an extended period (e.g.\& 5 minutes). 206By default, a 207.Dv SIGPIPE 208signal is raised if a process sends 209on a broken stream, but this behavior may be inhibited via 210.Xr setsockopt 2 . 211.Pp 212.Dv SOCK_SEQPACKET 213sockets employ the same system calls 214as 215.Dv SOCK_STREAM 216sockets. 217The only difference 218is that 219.Xr read 2 220calls will return only the amount of data requested, 221and any remaining in the arriving packet will be discarded. 222.Pp 223.Dv SOCK_DGRAM 224and 225.Dv SOCK_RAW 226sockets allow sending of datagrams to correspondents 227named in 228.Xr send 2 229calls. 230Datagrams are generally received with 231.Xr recvfrom 2 , 232which returns the next datagram with its return address. 233.Pp 234An 235.Xr fcntl 2 236system call can be used to specify a process group to receive 237a 238.Dv SIGURG 239signal when the out-of-band data arrives. 240It may also enable non-blocking I/O 241and asynchronous notification of I/O events 242via 243.Dv SIGIO . 244.Pp 245The operation of sockets is controlled by socket level 246.Em options . 247These options are defined in the file 248.In sys/socket.h . 249The 250.Xr setsockopt 2 251and 252.Xr getsockopt 2 253system calls are used to set and get options, respectively. 254.Sh RETURN VALUES 255A -1 is returned if an error occurs, otherwise the return 256value is a descriptor referencing the socket. 257.Sh ERRORS 258The 259.Fn socket 260system call fails if: 261.Bl -tag -width Er 262.It Bq Er EACCES 263Permission to create a socket of the specified type and/or protocol 264is denied. 265.It Bq Er EAFNOSUPPORT 266The address family (domain) is not supported or the 267specified domain is not supported by this protocol family. 268.It Bq Er EMFILE 269The per-process descriptor table is full. 270.It Bq Er ENFILE 271The system file table is full. 272.It Bq Er ENOBUFS 273Insufficient buffer space is available. 274The socket cannot be created until sufficient resources are freed. 275.It Bq Er EPERM 276User has insufficient privileges to carry out the requested operation. 277.It Bq Er EPROTONOSUPPORT 278The protocol type or the specified protocol is not supported 279within this domain. 280.It Bq Er EPROTOTYPE 281The socket type is not supported by the protocol. 282.El 283.Sh SEE ALSO 284.Xr accept 2 , 285.Xr bind 2 , 286.Xr connect 2 , 287.Xr divert 4 , 288.Xr getpeername 2 , 289.Xr getsockname 2 , 290.Xr getsockopt 2 , 291.Xr ioctl 2 , 292.Xr ip 4 , 293.Xr ip6 4 , 294.Xr listen 2 , 295.Xr read 2 , 296.Xr recv 2 , 297.Xr select 2 , 298.Xr send 2 , 299.Xr shutdown 2 , 300.Xr socketpair 2 , 301.Xr write 2 , 302.Xr CMSG_DATA 3 , 303.Xr getprotoent 3 , 304.Xr netgraph 4 , 305.Xr protocols 5 306.Rs 307.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 308.%B PS1 309.%N 7 310.Re 311.Rs 312.%T "BSD Interprocess Communication Tutorial" 313.%B PS1 314.%N 8 315.Re 316.Sh STANDARDS 317The 318.Fn socket 319function conforms to 320.St -p1003.1-2008 . 321The 322.Tn POSIX 323standard specifies only the 324.Dv AF_INET , 325.Dv AF_INET6 , 326and 327.Dv AF_UNIX 328constants for address families, and requires the use of 329.Dv AF_* 330constants for the 331.Fa domain 332argument of 333.Fn socket . 334The 335.Dv SOCK_CLOEXEC 336flag is expected to conform to the next revision of the 337.Tn POSIX 338standard. 339The 340.Dv SOCK_RDM 341.Fa type , 342the 343.Dv PF_* 344constants, and other address families are 345.Fx 346extensions. 347.Sh HISTORY 348The 349.Fn socket 350system call appeared in 351.Bx 4.2 . 352