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