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: src/lib/libc/sys/socket.2,v 1.12.2.11 2002/12/29 16:35:34 schweikh Exp $ 30.\" 31.Dd October 28, 2015 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 45.Fn Socket 46creates an endpoint for communication and returns a descriptor. 47.Pp 48The 49.Fa domain 50parameter 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, formerly called PF_UNIX, 59PF_UNIX Host-internal protocols, deprecated, use PF_LOCAL, 60PF_INET Internet version 4 protocols, 61PF_IMPLINK ARPAnet IMP addresses, 62PF_PUP PUP protocols, like BSP, 63PF_CHAOS MIT CHAOS protocols, 64PF_NS Xerox Network Systems protocols, 65PF_ISO ISO protocols, 66PF_OSI Open Systems Interconnection protocols, 67PF_ECMA European Computer Manufacturers, 68PF_DATAKIT Datakit protocols, 69PF_CCITT ITU-T protocols, like X.25, 70PF_SNA IBM SNA, 71PF_DECnet DECnet, 72PF_DLI DEC Direct Data Link Interface protocol, 73PF_LAT LAT protocol, 74PF_HYLINK NSC Hyperchannel, 75PF_ROUTE Internal Routing protocol, 76PF_LINK Link layer interface, 77PF_XTP eXpress Transfer Protocol, 78PF_COIP Connection-Oriented IP, aka ST II, 79PF_CNT Computer Network Technology, 80PF_SIP Simple Internet Protocol, 81PF_IPX Novell Internet Packet eXchange protocol, 82PF_RTIP Help Identify RTIP packets, 83PF_PIP Help Identify PIP packets, 84PF_ISDN Integrated Services Digital Network, 85PF_KEY Internal key-management function, 86PF_INET6 Internet version 6 protocols, 87PF_NATM Native ATM access, 88PF_ATM ATM, 89PF_NETGRAPH Netgraph sockets 90.Ed 91.Pp 92The socket has the indicated 93.Fa type , 94which specifies the semantics of communication. Currently 95defined types are: 96.Pp 97.Bd -literal -offset indent -compact 98SOCK_STREAM Stream socket, 99SOCK_DGRAM Datagram socket, 100SOCK_RAW Raw-protocol interface, 101SOCK_RDM Reliably-delivered packet, 102SOCK_SEQPACKET Sequenced packet stream 103.Ed 104.Pp 105A 106.Dv SOCK_STREAM 107type provides sequenced, reliable, 108two-way connection based byte streams. 109An out-of-band data transmission mechanism may be supported. 110A 111.Dv SOCK_DGRAM 112socket supports 113datagrams (connectionless, unreliable messages of 114a fixed (typically small) maximum length). 115A 116.Dv SOCK_SEQPACKET 117socket may provide a sequenced, reliable, 118two-way connection-based data transmission path for datagrams 119of fixed maximum length; a consumer may be required to read 120an entire packet with each read system call. 121This facility is protocol specific, and presently implemented 122only for 123.Dv PF_NS 124and 125.Dv PF_UNIX . 126.Dv SOCK_RAW 127sockets provide access to internal network protocols and interfaces. 128The types 129.Dv SOCK_RAW , 130which is available only to the super-user, and 131.Dv SOCK_RDM , 132which is planned, 133but not yet implemented, are not described here. 134.Pp 135Additionally, the following flags are allowed in the 136.Fa type 137argument: 138.Pp 139.Bd -literal -offset indent -compact 140SOCK_CLOEXEC Set close-on-exec on the new descriptor, 141SOCK_NONBLOCK Set non-blocking mode on the new socket 142.Ed 143.Pp 144The 145.Fa protocol 146specifies a particular protocol to be used with the socket. 147Normally only a single protocol exists to support a particular 148socket type within a given protocol family. However, it is possible 149that many protocols may exist, in which case a particular protocol 150must be specified in this manner. The protocol number to use is 151particular to the 152.Dq "communication domain" 153in which communication 154is to take place; see 155.Xr protocols 5 . 156.Pp 157Sockets of type 158.Dv SOCK_STREAM 159are full-duplex byte streams, similar 160to pipes. A stream socket must be in a 161.Em connected 162state before any data may be sent or received 163on it. A connection to another socket is created with a 164.Xr connect 2 165call. 166Once connected, data may be transferred using 167.Xr read 2 168and 169.Xr write 2 170calls or some variant of the 171.Xr send 2 172and 173.Xr recv 2 174calls. 175(Some protocol families, such as the Internet family, 176support the notion of an 177.Dq implied connect , 178which permits data to be sent piggybacked onto a connect operation by 179using the 180.Xr sendto 2 181call.) 182When a session has been completed a 183.Xr close 2 184may be performed. 185Out-of-band data may also be transmitted as described in 186.Xr send 2 187and received as described in 188.Xr recv 2 . 189.Pp 190The communications protocols used to implement a 191.Dv SOCK_STREAM 192insure that data 193is not lost or duplicated. If 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). 210A 211.Dv SIGPIPE 212signal is raised if a process sends 213on a broken stream; this causes naive processes, 214which do not handle the signal, to exit. 215.Pp 216.Dv SOCK_SEQPACKET 217sockets employ the same system calls 218as 219.Dv SOCK_STREAM 220sockets. The 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. Datagrams are generally received with 233.Xr recvfrom 2 , 234which returns the next datagram with its return address. 235.Pp 236An 237.Xr fcntl 2 238call can be used to specify a process group to receive 239a 240.Dv SIGURG 241signal when the out-of-band data arrives. 242It may also enable non-blocking I/O 243and asynchronous notification of I/O events 244via 245.Dv SIGIO . 246.Pp 247The operation of sockets is controlled by socket level 248.Em options . 249These options are defined in the file 250.In sys/socket.h . 251.Xr Setsockopt 2 252and 253.Xr getsockopt 2 254are used to set and get options, respectively. 255.Sh RETURN VALUES 256Upon successful completion 257.Fn socket 258returns a descriptor referencing the socket. 259Otherwise, -1 is returned and the global variable 260.Va errno 261is set to indicate the error. 262.Sh ERRORS 263The 264.Fn socket 265call fails if: 266.Bl -tag -width Er 267.It Bq Er EPROTONOSUPPORT 268The protocol type or the specified protocol is not supported 269within this domain. 270.It Bq Er EMFILE 271The per-process descriptor table is full. 272.It Bq Er ENFILE 273The system file table is full. 274.It Bq Er EACCES 275Permission to create a socket of the specified type and/or protocol 276is denied. 277.It Bq Er ENOBUFS 278Insufficient buffer space is available. 279The socket cannot be created until sufficient resources are freed. 280.El 281.Sh SEE ALSO 282.Xr accept 2 , 283.Xr bind 2 , 284.Xr connect 2 , 285.Xr getpeername 2 , 286.Xr getsockname 2 , 287.Xr getsockopt 2 , 288.Xr ioctl 2 , 289.Xr listen 2 , 290.Xr read 2 , 291.Xr recv 2 , 292.Xr select 2 , 293.Xr send 2 , 294.Xr shutdown 2 , 295.Xr socketpair 2 , 296.Xr write 2 , 297.Xr getprotoent 3 , 298.Xr netgraph 4 , 299.Xr protocols 5 300.Rs 301.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 302.%B PS1 303.%N 7 304.Re 305.Rs 306.%T "BSD Interprocess Communication Tutorial" 307.%B PS1 308.%N 8 309.Re 310.Sh HISTORY 311The 312.Fn socket 313function call appeared in 314.Bx 4.2 . 315