1.\" $OpenBSD: socket.2,v 1.32 2011/02/24 17:20:08 mikeb 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: February 24 2011 $ 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/types.h> 41.Fd #include <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.Ao Pa sys/socket.h Ac . 55The currently understood formats are: 56.Pp 57.Bl -tag -width "AF_IMPLINKXXX" -offset indent -compact 58.It AF_UNIX 59UNIX internal protocols 60.It AF_INET 61ARPA Internet protocols 62.It AF_INET6 63IPv6 (Internet Protocol version 6) protocols 64.It AF_IMPLINK 65IMP host at IMP link layer 66.It AF_BLUETOOTH 67Bluetooth protocols 68.El 69.Pp 70The socket has the indicated 71.Fa type , 72which specifies the semantics of communication. 73Currently defined types are: 74.Pp 75.Bl -tag -width "SOCK_SEQPACKETXXX" -offset indent -compact 76.It SOCK_STREAM 77.It SOCK_DGRAM 78.It SOCK_RAW 79.It SOCK_SEQPACKET 80.El 81.Pp 82A 83.Dv SOCK_STREAM 84type provides sequenced, reliable, 85two-way connection based byte streams. 86An out-of-band data transmission mechanism may be supported. 87A 88.Dv SOCK_DGRAM 89socket supports 90datagrams (connectionless, unreliable messages of 91a fixed (typically small) maximum length). 92A 93.Dv SOCK_SEQPACKET 94socket may provide a sequenced, reliable, 95two-way connection-based data transmission path for datagrams 96of fixed maximum length; a consumer may be required to read 97an entire packet with each read system call. 98This facility is protocol specific, and presently implemented 99only for 100.Dv AF_BLUETOOTH . 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.Ao Pa sys/socket.h Ac . 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 EPROTONOSUPPORT 213The protocol type or the specified protocol is not supported 214within this domain. 215.It Bq Er EMFILE 216The per-process descriptor table is full. 217.It Bq Er ENFILE 218The system file table is full. 219.It Bq Er EACCES 220Permission to create a socket of the specified type and/or protocol 221is denied. 222.It Bq Er ENOBUFS 223Insufficient buffer space is available. 224The socket cannot be created until sufficient resources are freed. 225.El 226.Sh SEE ALSO 227.Xr accept 2 , 228.Xr bind 2 , 229.Xr connect 2 , 230.Xr getsockname 2 , 231.Xr getsockopt 2 , 232.Xr ioctl 2 , 233.Xr listen 2 , 234.Xr poll 2 , 235.Xr read 2 , 236.Xr recv 2 , 237.Xr select 2 , 238.Xr send 2 , 239.Xr setsockopt 2 , 240.Xr shutdown 2 , 241.Xr socketpair 2 , 242.Xr write 2 , 243.Xr getprotoent 3 , 244.Xr inet 4 , 245.Xr inet6 4 , 246.Xr netintro 4 , 247.Xr unix 4 248.Rs 249.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 250.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 251.Re 252.Rs 253.%T "BSD Interprocess Communication Tutorial" 254.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 255.Re 256.Sh HISTORY 257The 258.Fn socket 259function call appeared in 260.Bx 4.2 . 261