1.\" $NetBSD: socket.2,v 1.5 1995/02/27 12:37:53 cgd Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)socket.2 8.1 (Berkeley) 6/4/93 35.\" 36.Dd March 18, 2015 37.Dt SOCKET 2 38.Os 39.Sh NAME 40.Nm socket 41.Nd create an endpoint for communication 42.Sh SYNOPSIS 43.Fd #include <sys/socket.h> 44.Ft int 45.Fo socket 46.Fa "int domain" 47.Fa "int type" 48.Fa "int protocol" 49.Fc 50.Sh DESCRIPTION 51.Fn socket 52creates an endpoint for communication and returns a descriptor. 53.Pp 54The 55.Fa domain 56parameter specifies a communications domain within which 57communication will take place; this selects the protocol family 58which should be used. 59These families are defined in the include file 60.Ao Pa sys/socket.h Ac . 61The currently understood formats are 62.Pp 63.Bd -literal -offset indent -compact 64PF_LOCAL Host-internal protocols, formerly called PF_UNIX, 65PF_UNIX Host-internal protocols, deprecated, use PF_LOCAL, 66PF_INET Internet version 4 protocols, 67PF_ROUTE Internal Routing protocol, 68PF_KEY Internal key-management function, 69PF_INET6 Internet version 6 protocols, 70PF_SYSTEM System domain, 71PF_NDRV Raw access to network device, 72PF_VSOCK VM Sockets protocols 73.Ed 74.Pp 75The socket has the indicated 76.Fa type , 77which specifies the semantics of communication. Currently 78defined types are: 79.Pp 80.Bd -literal -offset indent -compact 81SOCK_STREAM 82SOCK_DGRAM 83SOCK_RAW 84.\"SOCK_SEQPACKET 85.\"SOCK_RDM 86.Ed 87.Pp 88A 89.Dv SOCK_STREAM 90type provides sequenced, reliable, 91two-way connection based byte streams. 92An out-of-band data transmission mechanism may be supported. 93A 94.Dv SOCK_DGRAM 95socket supports 96datagrams (connectionless, unreliable messages of 97a fixed (typically small) maximum length). 98.\"A 99.\".Dv SOCK_SEQPACKET 100.\"socket may provide a sequenced, reliable, 101.\"two-way connection-based data transmission path for datagrams 102.\"of fixed maximum length; a consumer may be required to read 103.\"an entire packet with each read system call. 104.\"This facility is protocol specific, and presently implemented 105.\"only for 106.\".Dv PF_NS . 107.Dv SOCK_RAW 108sockets provide access to internal network protocols and interfaces. 109The type 110.Dv SOCK_RAW , 111which is available only to the super-user. 112.\" , and 113.\" .Dv SOCK_RDM , 114.\" which is planned, 115.\" but not yet implemented, are not described here. 116.Pp 117The 118.Fa protocol 119specifies a particular protocol to be used with the socket. 120Normally only a single protocol exists to support a particular 121socket type within a given protocol family. However, it is possible 122that many protocols may exist, in which case a particular protocol 123must be specified in this manner. The protocol number to use is 124particular to the \*(lqcommunication domain\*(rq in which communication 125is to take place; see 126.Xr protocols 5 . 127.Pp 128Sockets of type 129.Dv SOCK_STREAM 130are full-duplex byte streams, similar 131to pipes. A stream socket must be in a 132.Em connected 133state before any data may be sent or received 134on it. A connection to another socket is created with a 135.Xr connect 2 136or 137.Xr connectx 2 138call. Once connected, data may be transferred using 139.Xr read 2 140and 141.Xr write 2 142calls or some variant of the 143.Xr send 2 144and 145.Xr recv 2 146calls. When a session has been completed a 147.Xr close 2 148may be performed. 149Out-of-band data may also be transmitted as described in 150.Xr send 2 151and received as described in 152.Xr recv 2 . 153.Pp 154The communications protocols used to implement a 155.Dv SOCK_STREAM 156insure that data 157is not lost or duplicated. If a piece of data for which the 158peer protocol has buffer space cannot be successfully transmitted 159within a reasonable length of time, then 160the connection is considered broken and calls 161will indicate an error with 162-1 returns and with 163.Dv ETIMEDOUT 164as the specific code 165in the global variable 166.Va errno . 167The protocols optionally keep sockets 168.Dq warm 169by forcing transmissions 170roughly every minute in the absence of other activity. 171An error is then indicated if no response can be 172elicited on an otherwise 173idle connection for a extended period (e.g. 5 minutes). 174A 175.Dv SIGPIPE 176signal is raised if a process sends 177on a broken stream; this causes naive processes, 178which do not handle the signal, to exit. 179.\" .Pp 180.\" .Dv SOCK_SEQPACKET 181.\" sockets employ the same system calls 182.\" as 183.\" .Dv SOCK_STREAM 184.\" sockets. The only difference 185.\" is that 186.\" .Xr read 2 187.\" calls will return only the amount of data requested, 188.\" and any remaining in the arriving packet will be discarded. 189.Pp 190.Dv SOCK_DGRAM 191and 192.Dv SOCK_RAW 193sockets allow sending of datagrams to correspondents 194named in 195.Xr send 2 196calls. Datagrams are generally received with 197.Xr recvfrom 2 , 198which returns the next datagram with its return address. 199.Pp 200An 201.Xr fcntl 2 202call can be used to specify a process group to receive 203a 204.Dv SIGURG 205signal when the out-of-band data arrives. 206It may also enable non-blocking I/O 207and asynchronous notification of I/O events 208via 209.Dv SIGIO . 210.Pp 211The operation of sockets is controlled by socket level 212.Em options . 213These options are defined in the file 214.Ao Pa sys/socket.h Ac . 215.Xr Setsockopt 2 216and 217.Xr getsockopt 2 218are used to set and get options, respectively. 219.Sh RETURN VALUES 220A -1 is returned if an error occurs, otherwise the return 221value is a descriptor referencing the socket. 222.Sh ERRORS 223The 224.Fn socket 225system call fails if: 226.Bl -tag -width Er 227.\" =========== 228.It Bq Er EACCES 229Permission to create a socket of the specified type and/or protocol 230is denied. 231.\" =========== 232.It Bq Er EAFNOSUPPORT 233The specified address family is not supported. 234.\" =========== 235.It Bq Er EMFILE 236The per-process descriptor table is full. 237.\" =========== 238.It Bq Er ENFILE 239The system file table is full. 240.\" =========== 241.It Bq Er ENOBUFS 242Insufficient buffer space is available. 243The socket cannot be created until sufficient resources are freed. 244.\" =========== 245.It Bq Er ENOMEM 246Insufficient memory was available to fulfill the request. 247.\" =========== 248.It Bq Er EPROTONOSUPPORT 249The protocol type or the specified protocol is not supported 250within this domain. 251.\" =========== 252.It Bq Er EPROTOTYPE 253The socket type is not supported by the protocol. 254.El 255.Pp 256If a new protocol family is defined, 257the socreate process is free to return any desired error code. 258The 259.Fn socket 260system call will pass this error code along 261(even if it is undefined). 262.Sh LEGACY SYNOPSIS 263.Fd #include <sys/types.h> 264.Fd #include <sys/socket.h> 265.Pp 266The include file 267.In sys/types.h 268is necessary. 269.Sh SEE ALSO 270.Xr accept 2 , 271.Xr bind 2 , 272.Xr connect 2 , 273.Xr connectx 2 , 274.Xr disconnectx 2 , 275.Xr getsockname 2 , 276.Xr getsockopt 2 , 277.Xr ioctl 2 , 278.Xr listen 2 , 279.Xr read 2 , 280.Xr recv 2 , 281.Xr select 2 , 282.Xr send 2 , 283.Xr shutdown 2 , 284.Xr socketpair 2 , 285.Xr write 2 , 286.Xr getprotoent 3 , 287.Xr inet 4 , 288.Xr inet6 4 , 289.Xr unix 4 , 290.Xr compat 5 291.Rs 292.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 293.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 294.Re 295.Rs 296.%T "BSD Interprocess Communication Tutorial" 297.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 298.Re 299.Sh HISTORY 300The 301.Fn socket 302function call appeared in 303.Bx 4.2 . 304