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 September 28, 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), 67PF_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 92Additionally, the following flags are allowed in the 93.Fa type 94argument: 95.Pp 96.Bd -literal -offset indent -compact 97SOCK_CLOEXEC Set close-on-exec on the new descriptor, 98SOCK_CLOFORK Set close-on-fork on the new descriptor, 99SOCK_NONBLOCK Set non-blocking mode on the new socket 100.Ed 101.Pp 102The 103.Fa protocol 104argument 105specifies a particular protocol to be used with the socket. 106Normally only a single protocol exists to support a particular 107socket type within a given protocol family. 108However, it is possible 109that many protocols may exist, in which case a particular protocol 110must be specified in this manner. 111The protocol number to use is 112particular to the 113.Dq "communication domain" 114in which communication 115is to take place; see 116.Xr protocols 5 . 117The 118.Fa protocol 119argument may be set to zero (0) to request the default 120implementation of a socket type for the protocol, if any. 121.Sh STREAM SOCKET TYPE 122The 123.Dv SOCK_STREAM 124socket type provides reliable, sequenced, full-duplex octet streams between 125the socket and a peer to which the socket is connected. 126A socket of type 127.Dv SOCK_STREAM 128needs to be in a 129.Em connected 130state before any data can be sent or received. 131A connection to another socket is created with a 132.Xr connect 2 133system call. 134(Some protocol families, such as the Internet family, 135support the notion of an 136.Dq implied connect , 137which permits data to be sent piggybacked onto a connect operation by 138using the 139.Xr sendto 2 140system call.) 141Once connected, data may be sent using 142.Xr send 2 , 143.Xr sendto 2 , 144.Xr sendmsg 2 145and 146.Xr write 2 147system calls. 148Data may be received using 149.Xr recv 2 , 150.Xr recvfrom 2 , 151.Xr recvmsg 2 , 152and 153.Xr read 2 154system calls. 155Record boundaries are not maintained; data sent on a stream socket using output 156operations of one size can be received using input operations of smaller or 157larger sizes without loss of data. 158Data may be buffered; successful return from an output function does not imply 159that the data has been delivered to the peer or even transmitted from the local 160system. 161For certain protocols out-of-band data may also be transmitted as described in 162.Xr send 2 163and received as described in 164.Xr recv 2 . 165.Pp 166If data cannot be successfully transmitted within a given time then the 167connection is considered broken, and subsequent operations shall fail with 168a protocol specific error code. 169A 170.Dv SIGPIPE 171signal is raised if a thread attempts to send data on a broken stream (one that 172is no longer connected). 173The signal can be suppressed by the 174.Dv MSG_NOSIGNAL 175flag with distinct 176.Xr send 2 , 177.Xr sendto 2 , 178and 179.Xr sendmsg 2 180system calls or by the 181.Dv SO_NOSIGPIPE 182socket option set on the socket with 183.Xr setsockopt 2 . 184.Pp 185The 186.Dv SOCK_STREAM 187socket is supported by the following protocol families: 188.Dv PF_INET , 189.Dv PF_INET6 , 190.Dv PF_UNIX , 191.Dv PF_BLUETOOTH , 192.Dv PF_HYPERV , 193and 194.Dv PF_INET_SDP . 195Out-of-band data transmission mechanism is supported for stream sockets of 196.Dv PF_INET 197and 198.Dv PF_INET6 199protocol families. 200.Sh DATAGRAM SOCKET TYPE 201The 202.Dv SOCK_DGRAM 203socket type supports connectionless data transfer which is not necessarily 204acknowledged or reliable. 205Datagrams can be sent to the address specified (possibly multicast or 206broadcast) in each output operation, and incoming datagrams can be received 207from multiple sources. 208The source address of each datagram is available when receiving the datagram 209with 210.Xr recvfrom 2 211or 212.Xr recvmsg 2 . 213An application can also pre-specify a peer address with 214.Xr sendto 2 215or 216.Xr sendmsg 2 , 217in which case calls to output functions that do not specify a peer address 218shall send to the pre-specified peer. 219If a peer has been specified, only datagrams from that peer shall be received. 220A datagram shall be sent in a single output operation, and needs to be received 221in a single input operation. 222The maximum size of a datagram is protocol-specific. 223Output datagrams may be buffered within the system; thus, a successful return 224from an output function does not guarantee that a datagram is actually sent or 225received. 226.Pp 227The 228.Dv SOCK_DGRAM 229socket is supported by the following protocol families: 230.Dv PF_INET , 231.Dv PF_INET6 , 232.Dv PF_UNIX , 233.Dv PF_NETGRAPH , 234and 235.Dv PF_NETLINK . 236.Sh SEQUENCED PACKET SOCKET TYPE 237The 238.Dv SOCK_SEQPACKET 239socket type is similar to the 240.Dv SOCK_STREAM 241type, and is also connection-oriented. 242The only difference between these types is that record boundaries are 243maintained using the 244.Dv SOCK_SEQPACKET 245type. 246A record can be sent using one or more output operations and received using one 247or more input operations, but a single operation never transfers parts of more 248than one record. 249Record boundaries are set by the sender with the 250.Dv MSG_EOR 251flag of 252.Xr send 2 253or 254.Xr sendmsg 2 255functions. 256There is no possibility to set a record boundary with 257.Xr write 2 . 258Record boundaries are visible to the receiver via the 259.Dv MSG_EOR 260flag in the received message flags returned by the 261.Xr recvmsg 2 262function. 263It is protocol-specific whether a maximum record size is imposed. 264.Pp 265The 266.Dv SOCK_SEQPACKET 267socket is supported by the following protocol families: 268.Dv PF_INET , 269.Dv PF_INET6 , 270and 271.Dv PF_UNIX . 272.Pp 273.Sh RAW SOCKET TYPE 274The 275.Dv SOCK_RAW 276socket type provides access to internal network protocols and interfaces. 277It is a datagram socket in its nature, thus has the same semantics of 278read and write operations. 279The 280.Dv SOCK_RAW 281type is available only to the super-user and is described in 282.Xr ip 4 283and 284.Xr ip6 4 . 285.Sh NON-BLOCKING MODE 286A socket can be created in 287.Em non-blocking mode 288with the help of 289.Dv SOCK_NONBLOCK 290flag. 291Alternatively, the non-blocking mode on a socket can be turned on and off with 292the help of the 293.Dv O_NONBLOCK 294flag of the 295.Xr fcntl 2 296system call. 297.Pp 298When a non-blocking socket has not enough data in its receive buffer to fulfill 299the application supplied buffer, then data receiving system calls like 300.Xr recv 2 , 301.Xr recvfrom 2 , 302.Xr recvmsg 2 303and 304.Xr read 2 305will not block waiting for the data but immediately return. 306Return value will indicate amount of bytes read into the supplied buffer. 307The 308.Va errno 309will be set to 310.Dv EAGAIN 311.Po 312has same value as 313.Dv EWOULDBLOCK 314.Pc . 315.Pp 316If application tries to send more data on a non-blocking socket than the socket 317send buffer can accomodate with 318.Xr send 2 , 319.Xr sendto 2 , 320.Xr sendmsg 2 321or 322.Xr write 2 323system calls partial data will be sent. 324Return value will indicate amount of bytes sent. 325The 326.Va errno 327will be set to 328.Dv EAGAIN . 329Note that sockets of 330.Dv SOCK_DGRAM 331type are unreliable, thus for these sockets sending operations will never fail 332with 333.Dv EAGAIN 334in non-blocking mode neither will block in blocking mode. 335.Sh OTHER OPERATIONS ON SOCKETS 336Since socket descriptors are file descriptors, many generic file operations 337performed by 338.Xr fcntl 2 , 339apply. 340Socket descriptors can be used with all event engines, such as 341.Xr kevent 2 , 342.Xr select 2 343and 344.Xr poll 2 . 345.Pp 346An 347.Xr fcntl 2 348system call can be used to specify a process group to receive 349a 350.Dv SIGURG 351signal when the out-of-band data arrives. 352It may also enable non-blocking I/O 353and asynchronous notification of I/O events 354via 355.Dv SIGIO . 356.Pp 357The operation of sockets is controlled by socket level 358.Em options . 359These options are defined in the file 360.In sys/socket.h . 361The 362.Xr setsockopt 2 363and 364.Xr getsockopt 2 365system calls are used to set and get options, respectively. 366.Pp 367Connection associated with a socket can be terminated by 368.Xr close 2 369system call. 370One direction of communication can be disabled with 371.Xr shutdown 2 . 372.Sh RETURN VALUES 373A -1 is returned if an error occurs, otherwise the return 374value is a descriptor referencing the socket. 375.Sh ERRORS 376The 377.Fn socket 378system call fails if: 379.Bl -tag -width Er 380.It Bq Er EACCES 381Permission to create a socket of the specified type and/or protocol 382is denied. 383.It Bq Er EAFNOSUPPORT 384The address family (domain) is not supported or the 385specified domain is not supported by this protocol family. 386.It Bq Er EMFILE 387The per-process descriptor table is full. 388.It Bq Er ENFILE 389The system file table is full. 390.It Bq Er ENOBUFS 391Insufficient buffer space is available. 392The socket cannot be created until sufficient resources are freed. 393.It Bq Er EPERM 394User has insufficient privileges to carry out the requested operation. 395.It Bq Er EPROTONOSUPPORT 396The protocol type or the specified protocol is not supported 397within this domain. 398.It Bq Er EPROTOTYPE 399The socket type is not supported by the protocol. 400.El 401.Sh SEE ALSO 402.Xr accept 2 , 403.Xr bind 2 , 404.Xr close 2 , 405.Xr connect 2 , 406.Xr fcntl 2 , 407.Xr getpeername 2 , 408.Xr getsockname 2 , 409.Xr getsockopt 2 , 410.Xr ioctl 2 , 411.Xr kevent 2 , 412.Xr listen 2 , 413.Xr poll 2 , 414.Xr read 2 , 415.Xr recv 2 , 416.Xr select 2 , 417.Xr send 2 , 418.Xr sendmsg 2 , 419.Xr sendto 2 , 420.Xr signal 3 , 421.Xr shutdown 2 , 422.Xr socketpair 2 , 423.Xr write 2 , 424.Xr CMSG_DATA 3 , 425.Xr getprotoent 3 , 426.Xr divert 4 , 427.Xr ip 4 , 428.Xr ip6 4 , 429.Xr netgraph 4 , 430.Xr protocols 5 431.Rs 432.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 433.%B PS1 434.%N 7 435.Re 436.Rs 437.%T "BSD Interprocess Communication Tutorial" 438.%B PS1 439.%N 8 440.Re 441.Sh STANDARDS 442The 443.Fn socket 444function conforms to 445.St -p1003.1-2008 . 446The 447.Tn POSIX 448standard specifies only the 449.Dv AF_INET , 450.Dv AF_INET6 , 451and 452.Dv AF_UNIX 453constants for address families, and requires the use of 454.Dv AF_* 455constants for the 456.Fa domain 457argument of 458.Fn socket . 459The 460.Dv SOCK_CLOEXEC 461and 462.Dv SOCK_CLOFORK 463flags are expected to conform to 464.St -p1003.1-2024 . 465.Tn POSIX 466standard. 467The 468.Dv SOCK_RDM 469.Fa type , 470the 471.Dv PF_* 472constants, and other address families are 473.Fx 474extensions. 475.Sh HISTORY 476The 477.Fn socket 478system call appeared in 479.Bx 4.2 . 480.Pp 481The 482.Dv SOCK_CLOFORK 483flag appeared in 484.Fx 15.0 . 485