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