1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
'\" te
.\" Copyright (C) 2009, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH SOCKET 3SOCKET "Jan 28, 2009"
.SH NAME
socket \- create an endpoint for communication
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR ... ]
#include <sys/types.h>
#include <sys/socket.h>
\fBint\fR \fBsocket\fR(\fBint\fR \fIdomain\fR, \fBint\fR \fItype\fR, \fBint\fR \fIprotocol\fR);
.fi
.SH DESCRIPTION
.sp
.LP
The \fBsocket()\fR function creates an endpoint for communication and returns a
descriptor.
.sp
.LP
The \fIdomain\fR argument specifies the protocol family within which
communication takes place. The protocol family is generally the same as the
address family for the addresses supplied in later operations on the socket.
These families are defined in \fB<sys/socket.h>\fR.
.sp
.LP
The currently supported protocol families are:
.sp
.ne 2
.na
\fB\fBPF_UNIX\fR\fR
.ad
.RS 12n
\fBUNIX\fR system internal protocols
.RE
.sp
.ne 2
.na
\fB\fBPF_INET\fR\fR
.ad
.RS 12n
Internet Protocol Version 4 (IPv4)
.RE
.sp
.ne 2
.na
\fB\fBPF_INET6\fR\fR
.ad
.RS 12n
Internet Protocol Version 6 (IPv6)
.RE
.sp
.ne 2
.na
\fB\fBPF_NCA\fR\fR
.ad
.RS 12n
Network Cache and Accelerator (NCA) protocols
.RE
.sp
.LP
The socket has the indicated \fItype\fR, which specifies the communication
semantics. Currently defined types are:
.sp
.in +2
.nf
SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
SOCK_SEQPACKET
SOCK_RDM
.fi
.in -2
.sp
.LP
The \fItype\fR may be augmented by a bitwise-inclusive-OR of flags from the
following list, defined in <sys/socket.h>.
.sp
.ne 2
.na
\fB\fBSOCK_CLOEXEC\fR\fR
.ad
.RS 12n
Creates the socket with the \fBFD_CLOEXEC\fR flag set, causing the underlying
file descriptor to be closed prior to any future calls to \fBexec\fR(2). This
is similar in purpose to the \fBO_CLOEXEC\fR flag to \fBopen\fR(2).
.RE
.sp
.ne 2
.na
\fB\fBSOCK_NDELAY\fR\fR
.ad
.RS 12n
Creates the socket with the \fBO_NDELAY\fR flag set, causing the socket to
provide nonblocking semantics as described for \fBO_NDELAY\fR in \fBopen\fR(2).
\fBSOCK_NONBLOCK\fR should normally be used in preference to \fBSOCK_NDELAY\fR,
and takes precedence if both are set. See \fBopen\fR(2) for further details.
.RE
.sp
.ne 2
.na
\fB\fBSOCK_NONBLOCK\fR\fR
.ad
.RS 12n
Creates the socket with the \fBO_NONBLOCK\fR flag set, causing the socket to
provide nonblocking semantics as described for \fBO_NONBLOCK\fR in \fBopen\fR(2).
.RE
.sp
.LP
There must be an entry in the \fBnetconfig\fR(5) file for at least each
protocol family and type required. If a non-zero protocol has been specified
but no exact match for the protocol family, type, and protocol is found, then
the first entry containing the specified family and type with a \fIprotocol\fR
value of zero will be used.
.sp
.LP
A \fBSOCK_STREAM\fR type provides sequenced, reliable, two-way connection-based
byte streams. An out-of-band data transmission mechanism may be supported. A
\fBSOCK_DGRAM\fR socket supports datagrams (connectionless, unreliable messages
of a fixed (typically small) maximum length). A \fBSOCK_SEQPACKET\fR socket may
provide a sequenced, reliable, two-way connection-based data transmission path
for datagrams of fixed maximum length; a consumer may be required to read an
entire packet with each read system call. This facility is protocol specific,
and presently not implemented for any protocol family. \fBSOCK_RAW\fR sockets
provide access to internal network interfaces. The types \fBSOCK_RAW\fR, which
is available only to a user with the \fBnet_rawaccess\fR privilege, and
\fBSOCK_RDM\fR, for which no implementation currently exists, are not described
here.
.sp
.LP
The \fIprotocol\fR parameter is a protocol-family-specific value which
specifies a particular protocol to be used with the socket. Normally this
value is zero, as commonly only a single protocol exists to support a
particular socket type within a given protocol family. However, multiple
protocols may exist, in which case a particular protocol may be specified in
this manner.
.sp
.LP
Sockets of type \fBSOCK_STREAM\fR are full-duplex byte streams, similar to
pipes. A stream socket must be in a \fIconnected\fR state before any data may
be sent or received on it. A connection to another socket is created with a
\fBconnect\fR(3SOCKET) call. Once connected, data may be transferred using
\fBread\fR(2) and \fBwrite\fR(2) calls or some variant of the
\fBsend\fR(3SOCKET) and \fBrecv\fR(3SOCKET) calls. When a session has been
completed, a \fBclose\fR(2) may be performed. Out-of-band data may also be
transmitted as described on the \fBsend\fR(3SOCKET) manual page and received as
described on the \fBrecv\fR(3SOCKET) manual page.
.sp
.LP
The communications protocols used to implement a \fBSOCK_STREAM\fR insure that
data is not lost or duplicated. If a piece of data for which the peer protocol
has buffer space cannot be successfully transmitted within a reasonable length
of time, then the connection is considered broken and calls will indicate an
error with \(mi1 returns and with \fBETIMEDOUT\fR as the specific code in the
global variable \fBerrno\fR. The protocols optionally keep sockets "warm" by
forcing transmissions roughly every minute in the absence of other activity. An
error is then indicated if no response can be elicited on an otherwise idle
connection for a extended period (for instance 5 minutes). A \fBSIGPIPE\fR
signal is raised if a thread sends on a broken stream; this causes naive
processes, which do not handle the signal, to exit.
.sp
.LP
\fBSOCK_SEQPACKET\fR sockets employ the same system calls as \fBSOCK_STREAM\fR
sockets. The only difference is that \fBread\fR(2) calls will return only the
amount of data requested, and any remaining in the arriving packet will be
discarded.
.sp
.LP
\fBSOCK_DGRAM\fR and \fBSOCK_RAW\fR sockets allow datagrams to be sent to
correspondents named in \fBsendto\fR(3SOCKET) calls. Datagrams are generally
received with \fBrecvfrom\fR(3SOCKET), which returns the next datagram with its
return address.
.sp
.LP
An \fBfcntl\fR(2) call can be used to specify a process group to receive a
\fBSIGURG\fR signal when the out-of-band data arrives. It can also enable
non-blocking I/O.
.sp
.LP
The operation of sockets is controlled by socket level \fIoptions\fR. These
options are defined in the file <\fBsys/socket.h\fR>. \fBsetsockopt\fR(3SOCKET)
and \fBgetsockopt\fR(3SOCKET) are used to set and get options, respectively.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, a descriptor referencing the socket is returned.
Otherwise, -1 is returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
.sp
.LP
The \fBsocket()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 19n
Permission to create a socket of the specified type or protocol is denied.
.RE
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 19n
There were insufficient resources available to complete the operation.
.RE
.sp
.ne 2
.na
\fB\fBEAFNOSUPPORT\fR\fR
.ad
.RS 19n
The specified address family is not supported by the protocol family.
.RE
.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 19n
The per-process descriptor table is full.
.RE
.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 19n
Insufficient user memory is available.
.RE
.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 19n
There were insufficient STREAMS resources available to complete the operation.
.RE
.sp
.ne 2
.na
\fB\fBEPFNOSUPPORT\fR\fR
.ad
.RS 19n
The specified protocol family is not supported.
.RE
.sp
.ne 2
.na
\fB\fBEPROTONOSUPPORT\fR\fR
.ad
.RS 19n
The protocol type is not supported by the address family.
.RE
.sp
.ne 2
.na
\fB\fBEPROTOTYPE\fR\fR
.ad
.RS 19n
The socket type is not supported by the protocol.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
One or more of the specified flags is not supported.
.RE
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
MT-Level Safe
.TE
.SH SEE ALSO
.sp
.LP
.BR nca (1),
.BR close (2),
.BR exec (2),
.BR fcntl (2),
.BR ioctl (2),
.BR open (2),
.BR read (2),
.BR write (2),
.BR in.h (3HEAD),
.BR socket.h (3HEAD),
.BR accept (3SOCKET),
.BR bind (3SOCKET),
.BR connect (3SOCKET),
.BR getsockname (3SOCKET),
.BR getsockopt (3SOCKET),
.BR listen (3SOCKET),
.BR recv (3SOCKET),
.BR send (3SOCKET),
.BR setsockopt (3SOCKET),
.BR shutdown (3SOCKET),
.BR socketpair (3SOCKET),
.BR attributes (7)
.SH NOTES
.sp
.LP
Historically, \fBAF_\fR* was commonly used in places where \fBPF_\fR* was
meant. New code should be careful to use \fBPF_\fR* as necessary.
|