1 |
27 |
unneback |
NAME
|
2 |
|
|
socket - create an endpoint for communication
|
3 |
|
|
|
4 |
|
|
SYNOPSIS
|
5 |
|
|
#include <network.h>
|
6 |
|
|
|
7 |
|
|
int socket(int domain, int type, int protocol);
|
8 |
|
|
|
9 |
|
|
DESCRIPTION
|
10 |
|
|
Socket creates an endpoint for communication and returns a
|
11 |
|
|
descriptor.
|
12 |
|
|
|
13 |
|
|
The domain parameter specifies a communications domain
|
14 |
|
|
within which communication will take place; this selects
|
15 |
|
|
the protocol family which should be used. These families
|
16 |
|
|
are defined in <sys/socket.h>. The currently understood
|
17 |
|
|
formats include:
|
18 |
|
|
|
19 |
|
|
PF_INET
|
20 |
|
|
IPv4 Internet protocols; see ip(4)
|
21 |
|
|
|
22 |
|
|
The socket has the indicated type, which specifies the
|
23 |
|
|
semantics of communication. Currently defined types are:
|
24 |
|
|
|
25 |
|
|
SOCK_STREAM
|
26 |
|
|
Provides sequenced, reliable, two-way connection-
|
27 |
|
|
based byte streams. An out-of-band data transmis-
|
28 |
|
|
sion mechanism may be supported.
|
29 |
|
|
|
30 |
|
|
SOCK_DGRAM
|
31 |
|
|
Supports datagrams (connectionless, unreliable mes-
|
32 |
|
|
sages of a fixed maximum length).
|
33 |
|
|
|
34 |
|
|
SOCK_SEQPACKET
|
35 |
|
|
Provides a sequenced, reliable, two-way connection-
|
36 |
|
|
based data transmission path for datagrams of fixed
|
37 |
|
|
maximum length; a consumer is required to read an
|
38 |
|
|
entire packet with each read system call.
|
39 |
|
|
|
40 |
|
|
SOCK_RAW
|
41 |
|
|
Provides raw network protocol access.
|
42 |
|
|
|
43 |
|
|
The protocol specifies a particular protocol to be used
|
44 |
|
|
with the socket. Normally only a single protocol exists
|
45 |
|
|
to support a particular socket type within a given proto-
|
46 |
|
|
col family. However, it is possible that many protocols
|
47 |
|
|
may exist, in which case a particular protocol must be
|
48 |
|
|
specified in this manner. The protocol number to use is
|
49 |
|
|
particular to the "communication domain" in which communi-
|
50 |
|
|
cation is to take place; see protocols(5). See getpro-
|
51 |
|
|
toent(3) on how to map protocol name strings to protocol
|
52 |
|
|
numbers.
|
53 |
|
|
|
54 |
|
|
Sockets of type SOCK_STREAM are full-duplex byte streams,
|
55 |
|
|
similar to pipes. A stream socket must be in a connected
|
56 |
|
|
state before any data may be sent or received on it. A
|
57 |
|
|
connection to another socket is created with a connect(2)
|
58 |
|
|
call. Once connected, data may be transferred using
|
59 |
|
|
read(2) and write(2) calls or some variant of the send(2)
|
60 |
|
|
and recv(2) calls. When a session has been completed a
|
61 |
|
|
close(2) may be performed. Out-of-band data may also be
|
62 |
|
|
transmitted as described in send(2) and received as
|
63 |
|
|
described in recv(2).
|
64 |
|
|
|
65 |
|
|
The communications protocols which implement a SOCK_STREAM
|
66 |
|
|
ensure that data is not lost or duplicated. If a piece of
|
67 |
|
|
data for which the peer protocol has buffer space cannot
|
68 |
|
|
be successfully transmitted within a reasonable length of
|
69 |
|
|
time, then the connection is considered When SO_KEEPALIVE
|
70 |
|
|
is enabled on the socket the protocol checks in a proto-
|
71 |
|
|
col-specific manner if the other end is still alive.
|
72 |
|
|
|
73 |
|
|
SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams
|
74 |
|
|
to correspondents named in send(2) calls. Datagrams are
|
75 |
|
|
generally received with recvfrom(2), which returns the
|
76 |
|
|
next datagram with its return address.
|
77 |
|
|
|
78 |
|
|
When the network signals an error condition to the proto-
|
79 |
|
|
col module (e.g. using a ICMP message for IP) the pending
|
80 |
|
|
error flag is set for the socket. The next operation on
|
81 |
|
|
this socket will return the error code of the pending
|
82 |
|
|
error. For some protocols it is possible to enable a per-
|
83 |
|
|
socket error queue to retrieve detailed information about
|
84 |
|
|
the error; see IP_RECVERR in ip(4).
|
85 |
|
|
|
86 |
|
|
The operation of sockets is controlled by socket level
|
87 |
|
|
options. These options are defined in <sys/socket.h>.
|
88 |
|
|
Setsockopt(2) and getsockopt(2) are used to set and get
|
89 |
|
|
options, respectively.
|
90 |
|
|
|
91 |
|
|
RETURN VALUES
|
92 |
|
|
-1 is returned if an error occurs; otherwise the return
|
93 |
|
|
value is a descriptor referencing the socket.
|
94 |
|
|
|
95 |
|
|
ERRORS
|
96 |
|
|
EPROTONOSUPPORT
|
97 |
|
|
The protocol type or the specified protocol is not
|
98 |
|
|
supported within this domain.
|
99 |
|
|
|
100 |
|
|
EMFILE There are too many open files.
|
101 |
|
|
|
102 |
|
|
EACCES Permission to create a socket of the specified
|
103 |
|
|
type and/or protocol is denied.
|
104 |
|
|
|
105 |
|
|
ENOBUFS or ENOMEM
|
106 |
|
|
Insufficient memory is available. The socket can-
|
107 |
|
|
not be created until sufficient resources are
|
108 |
|
|
freed.
|
109 |
|
|
|
110 |
|
|
EINVAL Unknown protocol, or protocol family not avail-
|
111 |
|
|
able.
|