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