1.\" $NetBSD: socket.2,v 1.39 2012/01/25 09:18:52 wiz 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)socket.2 8.1 (Berkeley) 6/4/93 31.\" 32.Dd January 23, 2012 33.Dt SOCKET 2 34.Os 35.Sh NAME 36.Nm socket 37.Nd create an endpoint for communication 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 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.Ao Pa sys/socket.h Ac . 55The currently understood formats are: 56.Pp 57.Bd -literal -offset indent -compact 58PF_LOCAL local (previously UNIX) domain protocols 59PF_INET ARPA Internet protocols 60PF_INET6 IPv6 (Internet Protocol version 6) protocols 61PF_ISO ISO protocols 62PF_NS Xerox Network Systems protocols 63PF_IMPLINK IMP \*(lqhost at IMP\*(rq link layer 64PF_APPLETALK AppleTalk protocols 65PF_BLUETOOTH Bluetooth protocols 66.Ed 67.Pp 68The socket has the indicated 69.Fa type , 70which specifies the semantics of communication. 71Currently defined types are: 72.Pp 73.Bd -literal -offset indent -compact 74SOCK_STREAM 75SOCK_DGRAM 76SOCK_RAW 77SOCK_SEQPACKET 78SOCK_RDM 79.Ed 80.Pp 81The following flags can be or'ed to the type to condition the returned 82file descriptor: 83The following flags are valid: 84.Bl -column SOCK_NONBLOCK -offset indent 85.It Dv SOCK_CLOEXEC 86Set the close on exec property. 87.It Dv SOCK_NONBLOCK 88Sets non-blocking I/O. 89.It Dv SOCK_NOSIGPIPE 90Return 91.Er EPIPE 92instead of raising 93.Dv SIGPIPE . 94.El 95.Pp 96A 97.Dv SOCK_STREAM 98type provides sequenced, reliable, 99two-way connection based byte streams. 100An out-of-band data transmission mechanism may be supported. 101A 102.Dv SOCK_DGRAM 103socket supports 104datagrams (connectionless, unreliable messages of 105a fixed (typically small) maximum length). 106A 107.Dv SOCK_SEQPACKET 108socket may provide a sequenced, reliable, 109two-way connection-based data transmission path for datagrams 110of fixed maximum length; a consumer may be required to read 111an entire packet with each read system call. 112This facility is protocol specific, and presently implemented 113only for 114.Dv PF_NS . 115.Dv SOCK_RAW 116sockets provide access to internal network protocols and interfaces. 117The types 118.Dv SOCK_RAW , 119which is available only to the super-user, and 120.Dv SOCK_RDM , 121which is planned, 122but not yet implemented, are not described here. 123.Pp 124The 125.Fa protocol 126specifies a particular protocol to be used with the socket. 127Normally only a single protocol exists to support a particular 128socket type within a given protocol family. 129However, it is possible that many protocols may exist, in which case 130a particular protocol must be specified in this manner. 131The protocol number to use is 132particular to the \*(lqcommunication domain\*(rq in which communication 133is to take place; see 134.Xr protocols 5 . 135.Pp 136Sockets of type 137.Dv SOCK_STREAM 138are full-duplex byte streams. 139A stream socket must be in a 140.Em connected 141state before any data may be sent or received 142on it. 143A connection to another socket is created with a 144.Xr connect 2 145call. 146Once connected, data may be transferred using 147.Xr read 2 148and 149.Xr write 2 150calls or some variant of the 151.Xr send 2 152and 153.Xr recv 2 154calls. 155When a session has been completed a 156.Xr close 2 157may be performed. 158Out-of-band data may also be transmitted as described in 159.Xr send 2 160and received as described in 161.Xr recv 2 . 162.Pp 163The communications protocols used to implement a 164.Dv SOCK_STREAM 165ensure that data 166is not lost or duplicated. 167If a piece of data for which the 168peer protocol has buffer space cannot be successfully transmitted 169within a reasonable length of time, then 170the connection is considered broken and calls 171will indicate an error with 172\-1 returns and with 173.Er ETIMEDOUT 174as the specific code 175in the global variable 176.Va errno . 177The protocols optionally keep sockets 178.Dq warm 179by forcing transmissions 180roughly every minute in the absence of other activity. 181An error is then indicated if no response can be 182elicited on an otherwise 183idle connection for an extended period (e.g., 5 minutes). 184A 185.Dv SIGPIPE 186signal is raised if a process sends 187on a broken stream; this causes naive processes, 188which do not handle the signal, to exit. 189.Pp 190.Dv SOCK_SEQPACKET 191sockets employ the same system calls 192as 193.Dv SOCK_STREAM 194sockets. 195The only difference is that 196.Xr read 2 197calls will return only the amount of data requested, 198and any remaining in the arriving packet will be discarded. 199.Pp 200.Dv SOCK_DGRAM 201and 202.Dv SOCK_RAW 203sockets allow sending of datagrams to correspondents 204named in 205.Xr send 2 206calls. 207Datagrams are generally received with 208.Xr recvfrom 2 , 209which returns the next datagram with its return address. 210.Pp 211An 212.Xr fcntl 2 213call can be used to specify a process group to receive 214a 215.Dv SIGURG 216signal when the out-of-band data arrives. 217It may also enable non-blocking I/O 218and asynchronous notification of I/O events 219via 220.Dv SIGIO . 221.Pp 222The operation of sockets is controlled by socket level 223.Em options . 224These options are defined in the file 225.Ao Pa sys/socket.h Ac . 226The 227.Xr setsockopt 2 228and 229.Xr getsockopt 2 230system calls are used to set and get options, respectively. 231.Sh RETURN VALUES 232A \-1 is returned if an error occurs, otherwise the return 233value is a descriptor referencing the socket. 234.Sh ERRORS 235The 236.Fn socket 237call fails if: 238.Bl -tag -width Er 239.It Bq Er EACCES 240Permission to create a socket of the specified type and/or protocol 241is denied. 242.It Bq Er EAFNOSUPPORT 243The address family (domain) is not supported or 244the specified domain is not supported by this protocol family. 245.It Bq Er EMFILE 246The per-process descriptor table is full. 247.It Bq Er ENFILE 248The system file table is full. 249.It Bq Er ENOBUFS 250Insufficient buffer space is available. 251The socket cannot be created until sufficient resources are freed. 252.It Bq Er EPROTONOSUPPORT 253The protocol family is not supported or 254the specified protocol is not supported within this domain. 255.It Bq Er EPROTOTYPE 256The socket type is not supported by the protocol. 257.El 258.Sh SEE ALSO 259.Xr accept 2 , 260.Xr bind 2 , 261.Xr connect 2 , 262.Xr getsockname 2 , 263.Xr getsockopt 2 , 264.Xr ioctl 2 , 265.Xr listen 2 , 266.Xr poll 2 , 267.Xr read 2 , 268.Xr recv 2 , 269.Xr select 2 , 270.Xr send 2 , 271.Xr setsockopt 2 , 272.Xr shutdown 2 , 273.Xr socketpair 2 , 274.Xr write 2 , 275.Xr getprotoent 3 276.Rs 277.%T "An Introductory 4.4BSD Interprocess Communication Tutorial" 278.%A Stuart Sechrest 279.Re 280.Pq see Pa /usr/share/doc/psd/20.ipctut 281.Rs 282.%T "Advanced 4.4BSD IPC Tutorial" 283.%A Samuel J. Leffler 284.%A Robert S. Fabry 285.%A William N. Joy 286.%A Phil Lapsley 287.%A Steve Miller 288.%A Chris Torek 289.Re 290.Pq see Pa /usr/share/doc/psd/21.ipc 291.Sh HISTORY 292The 293.Fn socket 294function call appeared in 295.Bx 4.2 . 296