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