xref: /csrg-svn/lib/libc/sys/socket.2 (revision 61185)
1*61185Sbostic.\" Copyright (c) 1983, 1991, 1993
2*61185Sbostic.\"	The Regents of the University of California.  All rights reserved.
320271Smckusick.\"
443568Strent.\" %sccs.include.redist.man%
520271Smckusick.\"
6*61185Sbostic.\"     @(#)socket.2	8.1 (Berkeley) 06/04/93
736759Sbostic.\"
847208Scael.Dd
947208Scael.Dt SOCKET 2
1047208Scael.Os BSD 4.2
1147208Scael.Sh NAME
1247208Scael.Nm socket
1347208Scael.Nd create an endpoint for communication
1447208Scael.Sh SYNOPSIS
1547208Scael.Fd #include <sys/types.h>
1647208Scael.Fd #include <sys/socket.h>
1747208Scael.Ft int
1847208Scael.Fn socket "int domain" "int type" "int protocol"
1947208Scael.Sh DESCRIPTION
2047208Scael.Fn Socket
2120271Smckusickcreates an endpoint for communication and returns a descriptor.
2247208Scael.Pp
2320271SmckusickThe
2447208Scael.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
2947208Scael.Ao Pa sys/socket.h Ac .
3020271SmckusickThe currently understood formats are
3147208Scael.Pp
3247208Scael.Bd -literal -offset indent -compact
3347208ScaelAF_UNIX		(UNIX internal protocols),
3447208ScaelAF_INET		(ARPA Internet protocols),
3547208ScaelAF_ISO		(ISO protocols),
3647208ScaelAF_NS		(Xerox Network Systems protocols), and
3747208ScaelAF_IMPLINK	(IMP \*(lqhost at IMP\*(rq link layer).
3847208Scael.Ed
3947208Scael.Pp
4020271SmckusickThe socket has the indicated
4147208Scael.Fa type ,
4220271Smckusickwhich specifies the semantics of communication.  Currently
4320271Smckusickdefined types are:
4447208Scael.Pp
4547208Scael.Bd -literal -offset indent -compact
4620271SmckusickSOCK_STREAM
4720271SmckusickSOCK_DGRAM
4820271SmckusickSOCK_RAW
4920271SmckusickSOCK_SEQPACKET
5020271SmckusickSOCK_RDM
5147208Scael.Ed
5247208Scael.Pp
5347208ScaelA
5447208Scael.Dv SOCK_STREAM
5547208Scaeltype provides sequenced, reliable,
5628139Skarelstwo-way connection based byte streams.
5728139SkarelsAn out-of-band data transmission mechanism may be supported.
5847208ScaelA
5947208Scael.Dv SOCK_DGRAM
6047208Scaelsocket supports
6120271Smckusickdatagrams (connectionless, unreliable messages of
6220271Smckusicka fixed (typically small) maximum length).
6347208ScaelA
6447208Scael.Dv SOCK_SEQPACKET
6547208Scaelsocket 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
7047208Scaelonly for
7147208Scael.Dv PF_NS .
7247208Scael.Dv SOCK_RAW
7347208Scaelsockets provide access to internal network protocols and interfaces.
7447208ScaelThe types
7547208Scael.Dv SOCK_RAW ,
7620271Smckusickwhich is available only to the super-user, and
7747208Scael.Dv SOCK_RDM ,
7847208Scaelwhich is planned,
7920271Smckusickbut not yet implemented, are not described here.
8047208Scael.Pp
8120271SmckusickThe
8247208Scael.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
9047208Scael.Xr protocols 5 .
9147208Scael.Pp
9247208ScaelSockets of type
9347208Scael.Dv SOCK_STREAM
9420271Smckusickare full-duplex byte streams, similar
9520271Smckusickto pipes.  A stream socket must be in a
9647208Scael.Em connected
9720271Smckusickstate before any data may be sent or received
9820271Smckusickon it.  A connection to another socket is created with a
9947208Scael.Xr connect 2
10020271Smckusickcall.  Once connected, data may be transferred using
10147208Scael.Xr read 2
10220271Smckusickand
10347208Scael.Xr write 2
10420271Smckusickcalls or some variant of the
10547208Scael.Xr send 2
10620271Smckusickand
10747208Scael.Xr recv 2
10820271Smckusickcalls.  When a session has been completed a
10947208Scael.Xr close 2
11020271Smckusickmay be performed.
11120271SmckusickOut-of-band data may also be transmitted as described in
11247208Scael.Xr send 2
11320271Smckusickand received as described in
11447208Scael.Xr recv 2 .
11547208Scael.Pp
11620271SmckusickThe communications protocols used to implement a
11747208Scael.Dv SOCK_STREAM
11847208Scaelinsure 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
12447208Scael-1 returns and with
12547208Scael.Dv ETIMEDOUT
12647208Scaelas the specific code
12747208Scaelin the global variable
12847208Scael.Va errno .
12947208ScaelThe protocols optionally keep sockets
13047208Scael.Dq warm
13147208Scaelby 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).
13647208ScaelA
13747208Scael.Dv SIGPIPE
13847208Scaelsignal is raised if a process sends
13920271Smckusickon a broken stream; this causes naive processes,
14020271Smckusickwhich do not handle the signal, to exit.
14147208Scael.Pp
14247208Scael.Dv SOCK_SEQPACKET
14347208Scaelsockets employ the same system calls
14447208Scaelas
14547208Scael.Dv SOCK_STREAM
14647208Scaelsockets.  The only difference
14725722Smckusickis that
14847208Scael.Xr read 2
14925722Smckusickcalls will return only the amount of data requested,
15025722Smckusickand any remaining in the arriving packet will be discarded.
15147208Scael.Pp
15247208Scael.Dv SOCK_DGRAM
15347208Scaeland
15447208Scael.Dv SOCK_RAW
15520271Smckusicksockets allow sending of datagrams to correspondents
15620271Smckusicknamed in
15747208Scael.Xr send 2
15828139Skarelscalls.  Datagrams are generally received with
15947208Scael.Xr recvfrom 2 ,
16028139Skarelswhich returns the next datagram with its return address.
16147208Scael.Pp
16220271SmckusickAn
16347208Scael.Xr fcntl 2
16420271Smckusickcall can be used to specify a process group to receive
16547208Scaela
16647208Scael.Dv SIGURG
16747208Scaelsignal when the out-of-band data arrives.
16828139SkarelsIt may also enable non-blocking I/O
16928139Skarelsand asynchronous notification of I/O events
17047208Scaelvia
17147208Scael.Dv SIGIO .
17247208Scael.Pp
17320271SmckusickThe operation of sockets is controlled by socket level
17447208Scael.Em options .
17520271SmckusickThese options are defined in the file
17647208Scael.Ao Pa sys/socket.h Ac .
17747208Scael.Xr Setsockopt 2
17820271Smckusickand
17947208Scael.Xr getsockopt 2
18020271Smckusickare used to set and get options, respectively.
18147208Scael.Sh RETURN VALUES
18247208ScaelA -1 is returned if an error occurs, otherwise the return
18320271Smckusickvalue is a descriptor referencing the socket.
18447208Scael.Sh ERRORS
18547208ScaelThe
18647208Scael.Fn socket
18747208Scaelcall fails if:
18847208Scael.Bl -tag -width EPROTONOPSUPPORTA
18947208Scael.It Bq Er EPROTONOSUPPORT
19028139SkarelsThe protocol type or the specified protocol is not supported
19128139Skarelswithin this domain.
19247208Scael.It Bq Er EMFILE
19320271SmckusickThe per-process descriptor table is full.
19447208Scael.It Bq Er ENFILE
19528139SkarelsThe system file table is full.
19647208Scael.It Bq Er EACCESS
19728139SkarelsPermission to create a socket of the specified type and/or protocol
19828139Skarelsis denied.
19947208Scael.It Bq Er ENOBUFS
20028139SkarelsInsufficient buffer space is available.
20128139SkarelsThe socket cannot be created until sufficient resources are freed.
20247208Scael.El
20347208Scael.Sh SEE ALSO
20447208Scael.Xr accept 2 ,
20547208Scael.Xr bind 2 ,
20647208Scael.Xr connect 2 ,
20747208Scael.Xr getprotoent 3 ,
20847208Scael.Xr getsockname 2 ,
20947208Scael.Xr getsockopt 2 ,
21047208Scael.Xr ioctl 2 ,
21147208Scael.Xr listen 2 ,
21247208Scael.Xr read 2 ,
21347208Scael.Xr recv 2 ,
21447208Scael.Xr select 2 ,
21547208Scael.Xr send 2 ,
21647208Scael.Xr shutdown 2 ,
21747208Scael.Xr socketpair 2 ,
21847208Scael.Xr write 2
21947208Scael.Rs
22047208Scael.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
22147208Scael.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
22247208Scael.Re
22347208Scael.Rs
22447208Scael.%T "BSD Interprocess Communication Tutorial"
22547208Scael.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
22647208Scael.Re
22747208Scael.Sh HISTORY
22847208ScaelThe
22947208Scael.Nm
23047208Scaelfunction call appeared in
23147208Scael.Bx 4.2 .
232