1*47208Scael.\" Copyright (c) 1983, 1991 The Regents of the University of California. 236759Sbostic.\" All rights reserved. 320271Smckusick.\" 443568Strent.\" %sccs.include.redist.man% 520271Smckusick.\" 6*47208Scael.\" @(#)socket.2 6.8 (Berkeley) 03/10/91 736759Sbostic.\" 8*47208Scael.Dd 9*47208Scael.Dt SOCKET 2 10*47208Scael.Os BSD 4.2 11*47208Scael.Sh NAME 12*47208Scael.Nm socket 13*47208Scael.Nd create an endpoint for communication 14*47208Scael.Sh SYNOPSIS 15*47208Scael.Fd #include <sys/types.h> 16*47208Scael.Fd #include <sys/socket.h> 17*47208Scael.Ft int 18*47208Scael.Fn socket "int domain" "int type" "int protocol" 19*47208Scael.Sh DESCRIPTION 20*47208Scael.Fn Socket 2120271Smckusickcreates an endpoint for communication and returns a descriptor. 22*47208Scael.Pp 2320271SmckusickThe 24*47208Scael.Fa domain 2528139Skarelsparameter specifies a communications domain within which 2628139Skarelscommunication will take place; this selects the protocol family 2728139Skarelswhich should be used. 2828139SkarelsThese families are defined in the include file 29*47208Scael.Ao Pa sys/socket.h Ac . 3020271SmckusickThe currently understood formats are 31*47208Scael.Pp 32*47208Scael.Bd -literal -offset indent -compact 33*47208ScaelAF_UNIX (UNIX internal protocols), 34*47208ScaelAF_INET (ARPA Internet protocols), 35*47208ScaelAF_ISO (ISO protocols), 36*47208ScaelAF_NS (Xerox Network Systems protocols), and 37*47208ScaelAF_IMPLINK (IMP \*(lqhost at IMP\*(rq link layer). 38*47208Scael.Ed 39*47208Scael.Pp 4020271SmckusickThe socket has the indicated 41*47208Scael.Fa type , 4220271Smckusickwhich specifies the semantics of communication. Currently 4320271Smckusickdefined types are: 44*47208Scael.Pp 45*47208Scael.Bd -literal -offset indent -compact 4620271SmckusickSOCK_STREAM 4720271SmckusickSOCK_DGRAM 4820271SmckusickSOCK_RAW 4920271SmckusickSOCK_SEQPACKET 5020271SmckusickSOCK_RDM 51*47208Scael.Ed 52*47208Scael.Pp 53*47208ScaelA 54*47208Scael.Dv SOCK_STREAM 55*47208Scaeltype provides sequenced, reliable, 5628139Skarelstwo-way connection based byte streams. 5728139SkarelsAn out-of-band data transmission mechanism may be supported. 58*47208ScaelA 59*47208Scael.Dv SOCK_DGRAM 60*47208Scaelsocket supports 6120271Smckusickdatagrams (connectionless, unreliable messages of 6220271Smckusicka fixed (typically small) maximum length). 63*47208ScaelA 64*47208Scael.Dv SOCK_SEQPACKET 65*47208Scaelsocket may provide a sequenced, reliable, 6625722Smckusicktwo-way connection-based data transmission path for datagrams 6725722Smckusickof fixed maximum length; a consumer may be required to read 6825722Smckusickan entire packet with each read system call. 6925722SmckusickThis facility is protocol specific, and presently implemented 70*47208Scaelonly for 71*47208Scael.Dv PF_NS . 72*47208Scael.Dv SOCK_RAW 73*47208Scaelsockets provide access to internal network protocols and interfaces. 74*47208ScaelThe types 75*47208Scael.Dv SOCK_RAW , 7620271Smckusickwhich is available only to the super-user, and 77*47208Scael.Dv SOCK_RDM , 78*47208Scaelwhich is planned, 7920271Smckusickbut not yet implemented, are not described here. 80*47208Scael.Pp 8120271SmckusickThe 82*47208Scael.Fa protocol 8320271Smckusickspecifies a particular protocol to be used with the socket. 8420271SmckusickNormally only a single protocol exists to support a particular 8528139Skarelssocket type within a given protocol family. However, it is possible 8628139Skarelsthat many protocols may exist, in which case a particular protocol 8720271Smckusickmust be specified in this manner. The protocol number to use is 8820271Smckusickparticular to the \*(lqcommunication domain\*(rq in which communication 8920271Smckusickis to take place; see 90*47208Scael.Xr protocols 5 . 91*47208Scael.Pp 92*47208ScaelSockets of type 93*47208Scael.Dv SOCK_STREAM 9420271Smckusickare full-duplex byte streams, similar 9520271Smckusickto pipes. A stream socket must be in a 96*47208Scael.Em connected 9720271Smckusickstate before any data may be sent or received 9820271Smckusickon it. A connection to another socket is created with a 99*47208Scael.Xr connect 2 10020271Smckusickcall. Once connected, data may be transferred using 101*47208Scael.Xr read 2 10220271Smckusickand 103*47208Scael.Xr write 2 10420271Smckusickcalls or some variant of the 105*47208Scael.Xr send 2 10620271Smckusickand 107*47208Scael.Xr recv 2 10820271Smckusickcalls. When a session has been completed a 109*47208Scael.Xr close 2 11020271Smckusickmay be performed. 11120271SmckusickOut-of-band data may also be transmitted as described in 112*47208Scael.Xr send 2 11320271Smckusickand received as described in 114*47208Scael.Xr recv 2 . 115*47208Scael.Pp 11620271SmckusickThe communications protocols used to implement a 117*47208Scael.Dv SOCK_STREAM 118*47208Scaelinsure that data 11920271Smckusickis not lost or duplicated. If a piece of data for which the 12020271Smckusickpeer protocol has buffer space cannot be successfully transmitted 12120271Smckusickwithin a reasonable length of time, then 12220271Smckusickthe connection is considered broken and calls 12320271Smckusickwill indicate an error with 124*47208Scael-1 returns and with 125*47208Scael.Dv ETIMEDOUT 126*47208Scaelas the specific code 127*47208Scaelin the global variable 128*47208Scael.Va errno . 129*47208ScaelThe protocols optionally keep sockets 130*47208Scael.Dq warm 131*47208Scaelby forcing transmissions 13220271Smckusickroughly every minute in the absence of other activity. 13320271SmckusickAn error is then indicated if no response can be 13420271Smckusickelicited on an otherwise 13520271Smckusickidle connection for a extended period (e.g. 5 minutes). 136*47208ScaelA 137*47208Scael.Dv SIGPIPE 138*47208Scaelsignal is raised if a process sends 13920271Smckusickon a broken stream; this causes naive processes, 14020271Smckusickwhich do not handle the signal, to exit. 141*47208Scael.Pp 142*47208Scael.Dv SOCK_SEQPACKET 143*47208Scaelsockets employ the same system calls 144*47208Scaelas 145*47208Scael.Dv SOCK_STREAM 146*47208Scaelsockets. The only difference 14725722Smckusickis that 148*47208Scael.Xr read 2 14925722Smckusickcalls will return only the amount of data requested, 15025722Smckusickand any remaining in the arriving packet will be discarded. 151*47208Scael.Pp 152*47208Scael.Dv SOCK_DGRAM 153*47208Scaeland 154*47208Scael.Dv SOCK_RAW 15520271Smckusicksockets allow sending of datagrams to correspondents 15620271Smckusicknamed in 157*47208Scael.Xr send 2 15828139Skarelscalls. Datagrams are generally received with 159*47208Scael.Xr recvfrom 2 , 16028139Skarelswhich returns the next datagram with its return address. 161*47208Scael.Pp 16220271SmckusickAn 163*47208Scael.Xr fcntl 2 16420271Smckusickcall can be used to specify a process group to receive 165*47208Scaela 166*47208Scael.Dv SIGURG 167*47208Scaelsignal when the out-of-band data arrives. 16828139SkarelsIt may also enable non-blocking I/O 16928139Skarelsand asynchronous notification of I/O events 170*47208Scaelvia 171*47208Scael.Dv SIGIO . 172*47208Scael.Pp 17320271SmckusickThe operation of sockets is controlled by socket level 174*47208Scael.Em options . 17520271SmckusickThese options are defined in the file 176*47208Scael.Ao Pa sys/socket.h Ac . 177*47208Scael.Xr Setsockopt 2 17820271Smckusickand 179*47208Scael.Xr getsockopt 2 18020271Smckusickare used to set and get options, respectively. 181*47208Scael.Sh RETURN VALUES 182*47208ScaelA -1 is returned if an error occurs, otherwise the return 18320271Smckusickvalue is a descriptor referencing the socket. 184*47208Scael.Sh ERRORS 185*47208ScaelThe 186*47208Scael.Fn socket 187*47208Scaelcall fails if: 188*47208Scael.Bl -tag -width EPROTONOPSUPPORTA 189*47208Scael.It Bq Er EPROTONOSUPPORT 19028139SkarelsThe protocol type or the specified protocol is not supported 19128139Skarelswithin this domain. 192*47208Scael.It Bq Er EMFILE 19320271SmckusickThe per-process descriptor table is full. 194*47208Scael.It Bq Er ENFILE 19528139SkarelsThe system file table is full. 196*47208Scael.It Bq Er EACCESS 19728139SkarelsPermission to create a socket of the specified type and/or protocol 19828139Skarelsis denied. 199*47208Scael.It Bq Er ENOBUFS 20028139SkarelsInsufficient buffer space is available. 20128139SkarelsThe socket cannot be created until sufficient resources are freed. 202*47208Scael.El 203*47208Scael.Sh SEE ALSO 204*47208Scael.Xr accept 2 , 205*47208Scael.Xr bind 2 , 206*47208Scael.Xr connect 2 , 207*47208Scael.Xr getprotoent 3 , 208*47208Scael.Xr getsockname 2 , 209*47208Scael.Xr getsockopt 2 , 210*47208Scael.Xr ioctl 2 , 211*47208Scael.Xr listen 2 , 212*47208Scael.Xr read 2 , 213*47208Scael.Xr recv 2 , 214*47208Scael.Xr select 2 , 215*47208Scael.Xr send 2 , 216*47208Scael.Xr shutdown 2 , 217*47208Scael.Xr socketpair 2 , 218*47208Scael.Xr write 2 219*47208Scael.Rs 220*47208Scael.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 221*47208Scael.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 222*47208Scael.Re 223*47208Scael.Rs 224*47208Scael.%T "BSD Interprocess Communication Tutorial" 225*47208Scael.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 226*47208Scael.Re 227*47208Scael.Sh HISTORY 228*47208ScaelThe 229*47208Scael.Nm 230*47208Scaelfunction call appeared in 231*47208Scael.Bx 4.2 . 232