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.\" 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 8.1 (Berkeley) 6/4/93 33.\" $FreeBSD$ 34.\" 35.Dd November 24, 1997 36.Dt SOCKET 2 37.Os BSD 4.2 38.Sh NAME 39.Nm socket 40.Nd create an endpoint for communication 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.Fd #include <sys/types.h> 45.Fd #include <sys/socket.h> 46.Ft int 47.Fn socket "int domain" "int type" "int protocol" 48.Sh DESCRIPTION 49.Fn Socket 50creates an endpoint for communication and returns a descriptor. 51.Pp 52The 53.Fa domain 54parameter specifies a communications domain within which 55communication will take place; this selects the protocol family 56which should be used. 57These families are defined in the include file 58.Ao Pa sys/socket.h Ac . 59The currently understood formats are: 60.Pp 61.Bd -literal -offset indent -compact 62PF_LOCAL Host-internal protocols, formerly called PF_UNIX, 63PF_UNIX Host-internal protocols, depreciated, use PF_LOCAL, 64PF_INET Internet version 4 protocols, 65PF_IMPLINK ARPAnet IMP addresses, 66PF_PUP PUP protocols, like BSP, 67PF_CHAOS MIT CHAOS protocols, 68PF_NS Xerox Network Systems protocols, 69PF_ISO ISO protocols, 70PF_OSI Open Systems Interconnection protocols, 71PF_ECMA European Computer Manufacturers, 72PF_DATAKIT Datakit protocols, 73PF_CCITT ITU-T protocols, like X.25, 74PF_SNA IBM SNA, 75PF_DECnet DECnet, 76PF_DLI DEC Direct Data Link Interface protocol, 77PF_LAT LAT protocol, 78PF_HYLINK NSC Hyperchannel, 79PF_APPLETALK AppleTalk protocols, 80PF_ROUTE Internal Routing protocol, 81PF_LINK Link layer interface, 82PF_XTP eXpress Transfer Protocol, 83PF_COIP Connection-Oriented IP, aka ST II, 84PF_CNT Computer Network Technology, 85PF_SIP Simple Internet Protocol, 86PF_IPX Novell Intenet Packet eXchange protocol, 87PF_RTIP Help Identify RTIP packets, 88PF_PIP Help Identify PIP packets, 89PF_ISDN Integrated Services Digital Network, 90PF_KEY Internal key-management function, 91PF_INET6 Internet version 6 protocols, 92PF_NATM Native ATM access, 93PF_ATM ATM, 94PF_NETGRAPH Netgraph sockets 95.Ed 96.Pp 97The socket has the indicated 98.Fa type , 99which specifies the semantics of communication. Currently 100defined types are: 101.Pp 102.Bd -literal -offset indent -compact 103SOCK_STREAM Stream socket, 104SOCK_DGRAM Datagram socket, 105SOCK_RAW Raw-protocol interface, 106SOCK_RDM Sequenced packet stream, 107SOCK_SEQPACKET Reliably-delivered packet 108.Ed 109.Pp 110A 111.Dv SOCK_STREAM 112type provides sequenced, reliable, 113two-way connection based byte streams. 114An out-of-band data transmission mechanism may be supported. 115A 116.Dv SOCK_DGRAM 117socket supports 118datagrams (connectionless, unreliable messages of 119a fixed (typically small) maximum length). 120A 121.Dv SOCK_SEQPACKET 122socket may provide a sequenced, reliable, 123two-way connection-based data transmission path for datagrams 124of fixed maximum length; a consumer may be required to read 125an entire packet with each read system call. 126This facility is protocol specific, and presently implemented 127only for 128.Dv PF_NS . 129.Dv SOCK_RAW 130sockets provide access to internal network protocols and interfaces. 131The types 132.Dv SOCK_RAW , 133which is available only to the super-user, and 134.Dv SOCK_RDM , 135which is planned, 136but not yet implemented, are not described here. 137.Pp 138The 139.Fa protocol 140specifies a particular protocol to be used with the socket. 141Normally only a single protocol exists to support a particular 142socket type within a given protocol family. However, it is possible 143that many protocols may exist, in which case a particular protocol 144must be specified in this manner. The protocol number to use is 145particular to the \*(lqcommunication domain\*(rq in which communication 146is to take place; see 147.Xr protocols 5 . 148.Pp 149Sockets of type 150.Dv SOCK_STREAM 151are full-duplex byte streams, similar 152to pipes. A stream socket must be in a 153.Em connected 154state before any data may be sent or received 155on it. A connection to another socket is created with a 156.Xr connect 2 157call. 158Once connected, data may be transferred using 159.Xr read 2 160and 161.Xr write 2 162calls or some variant of the 163.Xr send 2 164and 165.Xr recv 2 166calls. 167(Some protocol families, such as the Internet family, 168support the notion of an 169.Dq implied connect, 170which permits data to be sent piggybacked onto a connect operation by 171using the 172.Xr sendto 2 173call.) 174When a session has been completed a 175.Xr close 2 176may be performed. 177Out-of-band data may also be transmitted as described in 178.Xr send 2 179and received as described in 180.Xr recv 2 . 181.Pp 182The communications protocols used to implement a 183.Dv SOCK_STREAM 184insure that data 185is not lost or duplicated. If a piece of data for which the 186peer protocol has buffer space cannot be successfully transmitted 187within a reasonable length of time, then 188the connection is considered broken and calls 189will indicate an error with 190-1 returns and with 191.Dv ETIMEDOUT 192as the specific code 193in the global variable 194.Va errno . 195The protocols optionally keep sockets 196.Dq warm 197by forcing transmissions 198roughly every minute in the absence of other activity. 199An error is then indicated if no response can be 200elicited on an otherwise 201idle connection for a extended period (e.g. 5 minutes). 202A 203.Dv SIGPIPE 204signal is raised if a process sends 205on a broken stream; this causes naive processes, 206which do not handle the signal, to exit. 207.Pp 208.Dv SOCK_SEQPACKET 209sockets employ the same system calls 210as 211.Dv SOCK_STREAM 212sockets. The only difference 213is that 214.Xr read 2 215calls will return only the amount of data requested, 216and any remaining in the arriving packet will be discarded. 217.Pp 218.Dv SOCK_DGRAM 219and 220.Dv SOCK_RAW 221sockets allow sending of datagrams to correspondents 222named in 223.Xr send 2 224calls. Datagrams are generally received with 225.Xr recvfrom 2 , 226which returns the next datagram with its return address. 227.Pp 228An 229.Xr fcntl 2 230call can be used to specify a process group to receive 231a 232.Dv SIGURG 233signal when the out-of-band data arrives. 234It may also enable non-blocking I/O 235and asynchronous notification of I/O events 236via 237.Dv SIGIO . 238.Pp 239The operation of sockets is controlled by socket level 240.Em options . 241These options are defined in the file 242.Ao Pa sys/socket.h Ac . 243.Xr Setsockopt 2 244and 245.Xr getsockopt 2 246are used to set and get options, respectively. 247.Sh RETURN VALUES 248A -1 is returned if an error occurs, otherwise the return 249value is a descriptor referencing the socket. 250.Sh ERRORS 251The 252.Fn socket 253call fails if: 254.Bl -tag -width Er 255.It Bq Er EPROTONOSUPPORT 256The protocol type or the specified protocol is not supported 257within this domain. 258.It Bq Er EMFILE 259The per-process descriptor table is full. 260.It Bq Er ENFILE 261The system file table is full. 262.It Bq Er EACCES 263Permission to create a socket of the specified type and/or protocol 264is denied. 265.It Bq Er ENOBUFS 266Insufficient buffer space is available. 267The socket cannot be created until sufficient resources are freed. 268.El 269.Sh SEE ALSO 270.Xr accept 2 , 271.Xr bind 2 , 272.Xr connect 2 , 273.Xr getpeername 2 , 274.Xr getsockname 2 , 275.Xr getsockopt 2 , 276.Xr ioctl 2 , 277.Xr listen 2 , 278.Xr read 2 , 279.Xr recv 2 , 280.Xr select 2 , 281.Xr send 2 , 282.Xr shutdown 2 , 283.Xr socketpair 2 , 284.Xr write 2 , 285.Xr getprotoent 3 , 286.Xr netgraph 4 , 287.Xr protocols 5 288.Rs 289.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 290.%B PS1 291.%N 7 292.Re 293.Rs 294.%T "BSD Interprocess Communication Tutorial" 295.%B PS1 296.%N 8 297.Re 298.Sh HISTORY 299The 300.Fn socket 301function call appeared in 302.Bx 4.2 . 303