1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. 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.\" 4. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" From: @(#)socket.2 8.1 (Berkeley) 6/4/93 29.\" $FreeBSD$ 30.\" 31.Dd January 5, 2009 32.Dt SOCKET 2 33.Os 34.Sh NAME 35.Nm socket 36.Nd create an endpoint for communication 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In sys/types.h 41.In sys/socket.h 42.Ft int 43.Fn socket "int domain" "int type" "int protocol" 44.Sh DESCRIPTION 45The 46.Fn socket 47system call 48creates an endpoint for communication and returns a descriptor. 49.Pp 50The 51.Fa domain 52argument 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.In sys/socket.h . 57The currently understood formats are: 58.Pp 59.Bd -literal -offset indent -compact 60PF_LOCAL Host-internal protocols, formerly called PF_UNIX, 61PF_UNIX Host-internal protocols, deprecated, use PF_LOCAL, 62PF_INET Internet version 4 protocols, 63PF_PUP PUP protocols, like BSP, 64PF_APPLETALK AppleTalk protocols, 65PF_ROUTE Internal Routing protocol, 66PF_LINK Link layer interface, 67PF_IPX Novell Internet Packet eXchange protocol, 68PF_RTIP Help Identify RTIP packets, 69PF_PIP Help Identify PIP packets, 70PF_ISDN Integrated Services Digital Network, 71PF_KEY Internal key-management function, 72PF_INET6 Internet version 6 protocols, 73PF_NATM Native ATM access, 74PF_ATM ATM, 75PF_NETGRAPH Netgraph sockets 76.Ed 77.Pp 78The socket has the indicated 79.Fa type , 80which specifies the semantics of communication. 81Currently 82defined types are: 83.Pp 84.Bd -literal -offset indent -compact 85SOCK_STREAM Stream socket, 86SOCK_DGRAM Datagram socket, 87SOCK_RAW Raw-protocol interface, 88SOCK_RDM Reliably-delivered packet, 89SOCK_SEQPACKET Sequenced packet stream 90.Ed 91.Pp 92A 93.Dv SOCK_STREAM 94type provides sequenced, reliable, 95two-way connection based byte streams. 96An out-of-band data transmission mechanism may be supported. 97A 98.Dv SOCK_DGRAM 99socket supports 100datagrams (connectionless, unreliable messages of 101a fixed (typically small) maximum length). 102A 103.Dv SOCK_SEQPACKET 104socket may provide a sequenced, reliable, 105two-way connection-based data transmission path for datagrams 106of fixed maximum length; a consumer may be required to read 107an entire packet with each read system call. 108This facility is protocol specific, and presently unimplemented. 109.Dv SOCK_RAW 110sockets provide access to internal network protocols and interfaces. 111The types 112.Dv SOCK_RAW , 113which is available only to the super-user, and 114.Dv SOCK_RDM , 115which is planned, 116but not yet implemented, are not described here. 117.Pp 118The 119.Fa protocol 120argument 121specifies a particular protocol to be used with the socket. 122Normally only a single protocol exists to support a particular 123socket type within a given protocol family. 124However, it is possible 125that many protocols may exist, in which case a particular protocol 126must be specified in this manner. 127The protocol number to use is 128particular to the 129.Dq "communication domain" 130in which communication 131is to take place; see 132.Xr protocols 5 . 133.Pp 134The 135.Fa protocol 136argument may be set to zero (0) to request the default 137implementation of a socket type for the protocol, if any. 138.Pp 139Sockets of type 140.Dv SOCK_STREAM 141are full-duplex byte streams, similar 142to pipes. 143A stream socket must be in a 144.Em connected 145state before any data may be sent or received 146on it. 147A connection to another socket is created with a 148.Xr connect 2 149system call. 150Once connected, data may be transferred using 151.Xr read 2 152and 153.Xr write 2 154calls or some variant of the 155.Xr send 2 156and 157.Xr recv 2 158functions. 159(Some protocol families, such as the Internet family, 160support the notion of an 161.Dq implied connect , 162which permits data to be sent piggybacked onto a connect operation by 163using the 164.Xr sendto 2 165system call.) 166When a session has been completed a 167.Xr close 2 168may be performed. 169Out-of-band data may also be transmitted as described in 170.Xr send 2 171and received as described in 172.Xr recv 2 . 173.Pp 174The communications protocols used to implement a 175.Dv SOCK_STREAM 176ensure that data 177is not lost or duplicated. 178If a piece of data for which the 179peer protocol has buffer space cannot be successfully transmitted 180within a reasonable length of time, then 181the connection is considered broken and calls 182will indicate an error with 183-1 returns and with 184.Er ETIMEDOUT 185as the specific code 186in the global variable 187.Va errno . 188The protocols optionally keep sockets 189.Dq warm 190by forcing transmissions 191roughly every minute in the absence of other activity. 192An error is then indicated if no response can be 193elicited on an otherwise 194idle connection for an extended period (e.g.\& 5 minutes). 195By default, a 196.Dv SIGPIPE 197signal is raised if a process sends 198on a broken stream, but this behavior may be inhibited via 199.Xr setsockopt 2 . 200.Pp 201.Dv SOCK_SEQPACKET 202sockets employ the same system calls 203as 204.Dv SOCK_STREAM 205sockets. 206The only difference 207is that 208.Xr read 2 209calls will return only the amount of data requested, 210and any remaining in the arriving packet will be discarded. 211.Pp 212.Dv SOCK_DGRAM 213and 214.Dv SOCK_RAW 215sockets allow sending of datagrams to correspondents 216named in 217.Xr send 2 218calls. 219Datagrams are generally received with 220.Xr recvfrom 2 , 221which returns the next datagram with its return address. 222.Pp 223An 224.Xr fcntl 2 225system call can be used to specify a process group to receive 226a 227.Dv SIGURG 228signal when the out-of-band data arrives. 229It may also enable non-blocking I/O 230and asynchronous notification of I/O events 231via 232.Dv SIGIO . 233.Pp 234The operation of sockets is controlled by socket level 235.Em options . 236These options are defined in the file 237.In sys/socket.h . 238The 239.Xr setsockopt 2 240and 241.Xr getsockopt 2 242system calls are used to set and get options, respectively. 243.Sh RETURN VALUES 244A -1 is returned if an error occurs, otherwise the return 245value is a descriptor referencing the socket. 246.Sh ERRORS 247The 248.Fn socket 249system call fails if: 250.Bl -tag -width Er 251.It Bq Er EPROTONOSUPPORT 252The protocol type or the specified protocol is not supported 253within this domain. 254.It Bq Er EMFILE 255The per-process descriptor table is full. 256.It Bq Er ENFILE 257The system file table is full. 258.It Bq Er EACCES 259Permission to create a socket of the specified type and/or protocol 260is denied. 261.It Bq Er ENOBUFS 262Insufficient buffer space is available. 263The socket cannot be created until sufficient resources are freed. 264.It Bq Er EPERM 265User has insufficient privileges to carry out the requested operation. 266.El 267.Sh SEE ALSO 268.Xr accept 2 , 269.Xr bind 2 , 270.Xr connect 2 , 271.Xr getpeername 2 , 272.Xr getsockname 2 , 273.Xr getsockopt 2 , 274.Xr ioctl 2 , 275.Xr listen 2 , 276.Xr read 2 , 277.Xr recv 2 , 278.Xr select 2 , 279.Xr send 2 , 280.Xr shutdown 2 , 281.Xr socketpair 2 , 282.Xr write 2 , 283.Xr getprotoent 3 , 284.Xr netgraph 4 , 285.Xr protocols 5 286.Rs 287.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 288.%B PS1 289.%N 7 290.Re 291.Rs 292.%T "BSD Interprocess Communication Tutorial" 293.%B PS1 294.%N 8 295.Re 296.Sh HISTORY 297The 298.Fn socket 299system call appeared in 300.Bx 4.2 . 301