1.\" $OpenBSD: socket.2,v 1.22 2004/04/15 19:52:18 jmc 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 June 4, 1993 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.Bd -literal -offset indent 57AF_UNIX (UNIX internal protocols), 58AF_INET (ARPA Internet protocols), 59AF_INET6 (ARPA IPv6 protocols), 60AF_ISO (ISO protocols), 61AF_NS (Xerox Network Systems protocols), 62AF_IPX (Internetwork Packet Exchange), and 63AF_IMPLINK (IMP \*(lqhost at IMP\*(rq link layer). 64.Ed 65.Pp 66The socket has the indicated 67.Fa type , 68which specifies the semantics of communication. 69Currently defined types are: 70.Bd -literal -offset indent 71SOCK_STREAM 72SOCK_DGRAM 73SOCK_RAW 74SOCK_SEQPACKET 75SOCK_RDM 76.Ed 77.Pp 78A 79.Dv SOCK_STREAM 80type provides sequenced, reliable, 81two-way connection based byte streams. 82An out-of-band data transmission mechanism may be supported. 83A 84.Dv SOCK_DGRAM 85socket supports 86datagrams (connectionless, unreliable messages of 87a fixed (typically small) maximum length). 88A 89.Dv SOCK_SEQPACKET 90socket may provide a sequenced, reliable, 91two-way connection-based data transmission path for datagrams 92of fixed maximum length; a consumer may be required to read 93an entire packet with each read system call. 94This facility is protocol specific, and presently implemented 95only for 96.Dv PF_NS . 97.Dv SOCK_RAW 98sockets provide access to internal network protocols and interfaces. 99The types 100.Dv SOCK_RAW , 101which is available only to the superuser, and 102.Dv SOCK_RDM , 103which is planned, 104but not yet implemented, are not described here. 105.Pp 106The 107.Fa protocol 108specifies a particular protocol to be used with the socket. 109Normally only a single protocol exists to support a particular 110socket type within a given protocol family. 111However, it is possible that many protocols may exist, 112in which case a particular protocol must be specified in this manner. 113The protocol number to use is particular to the \*(lqcommunication domain\*(rq 114in which communication is to take place; see 115.Xr protocols 5 . 116A value of 0 for 117.Fa protocol 118will let the system select an appropriate protocol for the requested 119socket type. 120.Pp 121Sockets of type 122.Dv SOCK_STREAM 123are full-duplex byte streams. 124A stream socket must be in a 125.Em connected 126state before any data may be sent or received on it. 127A connection to another socket is created with a 128.Xr connect 2 129call. 130Once connected, data may be transferred using 131.Xr read 2 132and 133.Xr write 2 134calls or some variant of the 135.Xr send 2 136and 137.Xr recv 2 138calls. 139When a session has been completed a 140.Xr close 2 141may be performed. 142Out-of-band data may also be transmitted as described in 143.Xr send 2 144and received as described in 145.Xr recv 2 . 146.Pp 147The communications protocols used to implement a 148.Dv SOCK_STREAM 149ensure that data is not lost or duplicated. 150If a piece of data for which the peer protocol has buffer space cannot 151be successfully transmitted within a reasonable length of time, then the 152connection is considered broken and calls will indicate an error with \-1 153returns and with 154.Er ETIMEDOUT 155as the specific code in the global variable 156.Va errno . 157The protocols optionally keep sockets 158.Dq warm 159by forcing transmissions roughly every minute in the absence of other activity. 160An error is then indicated if no response can be elicited on an otherwise 161idle connection for an extended period (e.g., 5 minutes). 162A 163.Dv SIGPIPE 164signal is raised if a process sends on a broken stream; this causes 165naive processes, which do not handle the signal, to exit. 166.Pp 167.Dv SOCK_SEQPACKET 168sockets employ the same system calls 169as 170.Dv SOCK_STREAM 171sockets. 172The only difference is that 173.Xr read 2 174calls will return only the amount of data requested, 175and any remaining in the arriving packet will be discarded. 176.Pp 177.Dv SOCK_DGRAM 178and 179.Dv SOCK_RAW 180sockets allow sending of datagrams to correspondents named in 181.Xr send 2 182calls. 183Datagrams are generally received with 184.Xr recvfrom 2 , 185which returns the next datagram with its return address. 186.Pp 187An 188.Xr fcntl 2 189call can be used to specify a process group to receive 190a 191.Dv SIGURG 192signal when the out-of-band data arrives. 193It may also enable non-blocking I/O and asynchronous notification 194of I/O events via 195.Dv SIGIO . 196.Pp 197The operation of sockets is controlled by socket level 198.Em options . 199These options are defined in the file 200.Ao Pa sys/socket.h Ac . 201.Xr setsockopt 2 202and 203.Xr getsockopt 2 204are used to set and get options, respectively. 205.Sh RETURN VALUES 206A \-1 is returned if an error occurs, otherwise the return 207value is a descriptor referencing the socket. 208.Sh ERRORS 209The 210.Fn socket 211call fails if: 212.Bl -tag -width Er 213.It Bq Er EPROTONOSUPPORT 214The protocol type or the specified protocol is not supported 215within this domain. 216.It Bq Er EMFILE 217The per-process descriptor table is full. 218.It Bq Er ENFILE 219The system file table is full. 220.It Bq Er EACCES 221Permission to create a socket of the specified type and/or protocol 222is denied. 223.It Bq Er ENOBUFS 224Insufficient buffer space is available. 225The socket cannot be created until sufficient resources are freed. 226.El 227.Sh SEE ALSO 228.Xr accept 2 , 229.Xr bind 2 , 230.Xr connect 2 , 231.Xr getsockname 2 , 232.Xr getsockopt 2 , 233.Xr ioctl 2 , 234.Xr listen 2 , 235.Xr poll 2 , 236.Xr read 2 , 237.Xr recv 2 , 238.Xr select 2 , 239.Xr send 2 , 240.Xr setsockopt 2 , 241.Xr shutdown 2 , 242.Xr socketpair 2 , 243.Xr write 2 , 244.Xr getprotoent 3 , 245.Xr netintro 4 246.Rs 247.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 248.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 249.Re 250.Rs 251.%T "BSD Interprocess Communication Tutorial" 252.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 253.Re 254.Sh HISTORY 255The 256.Fn socket 257function call appeared in 258.Bx 4.2 . 259