1SOCKET(2) System Calls Manual SOCKET(2)
2
3
4
6 socket - create an endpoint for communication
7
9 #include <sys/types.h>
10 #include <sys/socket.h>
11
12 s = socket(domain, type, protocol)
13 int s, domain, type, protocol;
14
16 Socket creates an endpoint for communication and returns a descriptor.
17
18 The domain parameter specifies a communications domain within which
19 communication will take place; this selects the protocol family which
20 should be used. The protocol family generally is the same as the
21 address family for the addresses supplied in later operations on the
22 socket. These families are defined in the include file <sys/socket.h>.
23 The currently understood formats are
24
25 PF_UNIX (UNIX internal protocols),
26 PF_INET (ARPA Internet protocols),
27 PF_NS (Xerox Network Systems protocols), and
28 PF_IMPLINK (IMP “host at IMP” link layer).
29
30 The socket has the indicated type, which specifies the semantics of
31 communication. Currently defined types are:
32
33 SOCK_STREAM
34 SOCK_DGRAM
35 SOCK_RAW
36 SOCK_SEQPACKET
37 SOCK_RDM
38
39 A SOCK_STREAM type provides sequenced, reliable, two-way connection
40 based byte streams. An out-of-band data transmission mechanism may be
41 supported. A SOCK_DGRAM socket supports datagrams (connectionless,
42 unreliable messages of a fixed (typically small) maximum length). A
43 SOCK_SEQPACKET socket may provide a sequenced, reliable, two-way con‐
44 nection-based data transmission path for datagrams of fixed maximum
45 length; a consumer may be required to read an entire packet with each
46 read system call. This facility is protocol specific, and presently
47 implemented only for PF_NS. SOCK_RAW sockets provide access to inter‐
48 nal network protocols and interfaces. The types SOCK_RAW, which is
49 available only to the super-user, and SOCK_RDM, which is planned, but
50 not yet implemented, are not described here.
51
52 The protocol specifies a particular protocol to be used with the
53 socket. Normally only a single protocol exists to support a particular
54 socket type within a given protocol family. However, it is possible
55 that many protocols may exist, in which case a particular protocol must
56 be specified in this manner. The protocol number to use is particular
57 to the “communication domain” in which communication is to take place;
58 see protocols(3N).
59
60 Sockets of type SOCK_STREAM are full-duplex byte streams, similar to
61 pipes. A stream socket must be in a connected state before any data
62 may be sent or received on it. A connection to another socket is cre‐
63 ated with a connect(2) call. Once connected, data may be transferred
64 using read(2) and write(2) calls or some variant of the send(2) and
65 recv(2) calls. When a session has been completed a close(2) may be
66 performed. Out-of-band data may also be transmitted as described in
67 send(2) and received as described in recv(2).
68
69 The communications protocols used to implement a SOCK_STREAM insure
70 that data is not lost or duplicated. If a piece of data for which the
71 peer protocol has buffer space cannot be successfully transmitted
72 within a reasonable length of time, then the connection is considered
73 broken and calls will indicate an error with -1 returns and with
74 ETIMEDOUT as the specific code in the global variable errno. The pro‐
75 tocols optionally keep sockets “warm” by forcing transmissions roughly
76 every minute in the absence of other activity. An error is then indi‐
77 cated if no response can be elicited on an otherwise idle connection
78 for a extended period (e.g. 5 minutes). A SIGPIPE signal is raised if
79 a process sends on a broken stream; this causes naive processes, which
80 do not handle the signal, to exit.
81
82 SOCK_SEQPACKET sockets employ the same system calls as SOCK_STREAM
83 sockets. The only difference is that read(2) calls will return only
84 the amount of data requested, and any remaining in the arriving packet
85 will be discarded.
86
87 SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to corre‐
88 spondents named in send(2) calls. Datagrams are generally received
89 with recvfrom(2), which returns the next datagram with its return
90 address.
91
92 An fcntl(2) call can be used to specify a process group to receive a
93 SIGURG signal when the out-of-band data arrives. It may also enable
94 non-blocking I/O and asynchronous notification of I/O events via SIGIO.
95
96 The operation of sockets is controlled by socket level options. These
97 options are defined in the file <sys/socket.h>. Setsockopt(2) and get‐
98 sockopt(2) are used to set and get options, respectively.
99
101 A -1 is returned if an error occurs, otherwise the return value is a
102 descriptor referencing the socket.
103
105 The socket call fails if:
106
107 [EPROTONOSUPPORT] The protocol type or the specified protocol is not
108 supported within this domain.
109
110 [EMFILE] The per-process descriptor table is full.
111
112 [ENFILE] The system file table is full.
113
114 [EACCESS] Permission to create a socket of the specified type
115 and/or protocol is denied.
116
117 [ENOBUFS] Insufficient buffer space is available. The socket
118 cannot be created until sufficient resources are
119 freed.
120
122 accept(2), bind(2), connect(2), getsockname(2), getsockopt(2),
123 ioctl(2), listen(2), read(2), recv(2), select(2), send(2), shutdown(2),
124 socketpair(2), write(2)
125 ``An Introductory 4.3BSD Interprocess Communication Tutorial.''
126 (reprinted in UNIX Programmer's Supplementary Documents Volume 1,
127 PS1:7) ``An Advanced 4.3BSD Interprocess Communication Tutorial.''
128 (reprinted in UNIX Programmer's Supplementary Documents Volume 1,
129 PS1:8)
130
131
132
1334.2 Berkeley Distribution May 23, 1986 SOCKET(2)