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