1.\" $NetBSD: socket.2,v 1.49 2023/06/08 00:27:34 uwe 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 June 28, 2022 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.Bl -column -offset indent ".Dv PF_APPLETALK" 58.It Dv PF_LOCAL Ta local domain Po previously Tn UNIX domain Pc protocols 59.It Dv PF_INET Ta Tn ARPA Internet protocols 60.It Dv PF_INET6 Ta Tn IPv6 (Internet Protocol version 6) protocols 61.It Dv PF_NS Ta Xerox Network Systems protocols 62.It Dv PF_APPLETALK Ta AppleTalk protocols 63.It Dv PF_BLUETOOTH Ta Bluetooth protocols 64.It Dv PF_CAN Ta Tn CAN bus protocols 65.El 66.Pp 67The socket has the indicated 68.Fa type , 69which specifies the semantics of communication. 70Currently defined types are: 71.Pp 72.Bl -tag -offset indent -width Dv 73.It Dv SOCK_STREAM 74Provides sequenced, reliable, two-way connection based byte streams. 75An out-of-band data transmission mechanism may be supported. 76.It Dv SOCK_DGRAM 77Supports datagrams: connectionless, unreliable messages of a 78fixed\(emtypically small\(emmaximum length. 79.It Dv SOCK_RAW 80Provides access to internal network protocols and interfaces. 81Available only to the super-user. 82Not described here. 83.It Dv SOCK_SEQPACKET 84Provides a sequenced, reliable, two-way connection-based data 85transmission path for datagrams of fixed maximum length. 86A consumer may be required to read an entire packet with each read 87system call. 88.It Dv SOCK_RDM 89Not implemented. 90.El 91.Pp 92The following flags can be or'ed to the type to condition the returned 93file descriptor: 94The following flags are valid: 95.Pp 96.Bl -tag -offset indent -width Dv 97.It Dv SOCK_CLOEXEC 98Set the close on exec property. 99.It Dv SOCK_NONBLOCK 100Set non-blocking I/O. 101.It Dv SOCK_NOSIGPIPE 102Return 103.Er EPIPE 104instead of raising 105.Dv SIGPIPE . 106.El 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. 113However, it is possible that many protocols may exist, in which case 114a particular protocol must be specified in this manner. 115The protocol number to use is 116particular to the \*(lqcommunication domain\*(rq in which communication 117is to take place; see 118.Xr protocols 5 . 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 126on 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 150is not lost or duplicated. 151If a piece of data for which the 152peer protocol has buffer space cannot be successfully transmitted 153within a reasonable length of time, then 154the connection is considered broken and calls 155will indicate an error with 156\-1 returns and with 157.Er ETIMEDOUT 158as the specific code 159in the global variable 160.Va errno . 161The protocols optionally keep sockets 162.Dq warm 163by forcing transmissions 164roughly every minute in the absence of other activity. 165An error is then indicated if no response can be 166elicited on an otherwise 167idle connection for an extended period (e.g., 5 minutes). 168A 169.Dv SIGPIPE 170signal is raised if a process sends 171on a broken stream; this causes naive processes, 172which do not handle the signal, to exit. 173.Pp 174.Dv SOCK_SEQPACKET 175sockets employ the same system calls 176as 177.Dv SOCK_STREAM 178sockets. 179The only difference is that 180.Xr read 2 181calls will return only the amount of data requested, 182and any remaining in the arriving packet will be discarded. 183.Pp 184.Dv SOCK_DGRAM 185and 186.Dv SOCK_RAW 187sockets allow sending of datagrams to correspondents 188named in 189.Xr send 2 190calls. 191Datagrams are generally received with 192.Xr recvfrom 2 , 193which returns the next datagram with its return address. 194.Pp 195An 196.Xr fcntl 2 197call can be used to specify a process group to receive 198a 199.Dv SIGURG 200signal when the out-of-band data arrives. 201It may also enable non-blocking I/O 202and asynchronous notification of I/O events 203via 204.Dv SIGIO . 205.Pp 206The operation of sockets is controlled by socket level 207.Em options . 208These options are defined in the file 209.Ao Pa sys/socket.h Ac . 210The 211.Xr setsockopt 2 212and 213.Xr getsockopt 2 214system calls are used to set and get options, respectively. 215.Sh RETURN VALUES 216A \-1 is returned if an error occurs, otherwise the return 217value is a descriptor referencing the socket. 218.Sh ERRORS 219The 220.Fn socket 221call fails if: 222.Bl -tag -width Er 223.It Bq Er EACCES 224Permission to create a socket of the specified type and/or protocol 225is denied. 226.It Bq Er EAFNOSUPPORT 227The address family (domain) is not supported or 228the specified domain is not supported by this protocol family. 229.It Bq Er EMFILE 230The per-process descriptor table is full. 231.It Bq Er ENFILE 232The system file table is full. 233.It Bq Er ENOBUFS 234Insufficient buffer space is available. 235The socket cannot be created until sufficient resources are freed. 236.It Bq Er EPROTONOSUPPORT 237The protocol family is not supported or 238the specified protocol is not supported within this domain. 239.It Bq Er EPROTOTYPE 240The socket type is not supported by the protocol. 241.El 242.Sh SEE ALSO 243.Xr accept 2 , 244.Xr bind 2 , 245.Xr connect 2 , 246.Xr getsockname 2 , 247.Xr getsockopt 2 , 248.Xr ioctl 2 , 249.Xr listen 2 , 250.Xr poll 2 , 251.Xr read 2 , 252.Xr recv 2 , 253.Xr select 2 , 254.Xr send 2 , 255.Xr setsockopt 2 , 256.Xr shutdown 2 , 257.Xr socketpair 2 , 258.Xr write 2 , 259.Xr getprotoent 3 260.Rs 261.%T "An Introductory 4.4BSD Interprocess Communication Tutorial" 262.%A Stuart Sechrest 263.Re 264.Pq see Pa /usr/share/doc/reference/ref3/sockets 265.Rs 266.%T "Advanced 4.4BSD IPC Tutorial" 267.%A Samuel J. Leffler 268.%A Robert S. Fabry 269.%A William N. Joy 270.%A Phil Lapsley 271.%A Steve Miller 272.%A Chris Torek 273.Re 274.Pq see Pa /usr/share/doc/reference/ref3/sockets-advanced 275.Sh HISTORY 276The 277.Fn socket 278function call appeared in 279.Bx 4.2 . 280