1socket(3SOCKET)            Sockets Library Functions           socket(3SOCKET)
2
3
4

NAME

6       socket - create an endpoint for communication
7

SYNOPSIS

9       cc [ flag ... ] file ... -lsocket  -lnsl  [ library ... ]
10       #include <sys/types.h>
11       #include <sys/socket.h>
12
13       int socket(int domain, int type, int protocol);
14
15

DESCRIPTION

17       The socket() function creates an endpoint for communication and returns
18       a descriptor.
19
20
21       The domain argument specifies the protocol family within which communi‐
22       cation  takes  place.  The protocol family is generally the same as the
23       address family for the addresses supplied in later  operations  on  the
24       socket. These families are defined in <sys/socket.h>.
25
26
27       The currently supported protocol families are:
28
29       PF_UNIX     UNIX system internal protocols
30
31
32       PF_INET     Internet Protocol Version 4 (IPv4)
33
34
35       PF_INET6    Internet Protocol Version 6 (IPv6)
36
37
38       PF_NCA      Network Cache and Accelerator (NCA) protocols
39
40
41
42       The  socket  has  the indicated type, which specifies the communication
43       semantics. Currently defined types are:
44
45         SOCK_STREAM
46         SOCK_DGRAM
47         SOCK_RAW
48         SOCK_SEQPACKET
49         SOCK_RDM
50
51
52
53       There must be an entry in the netconfig(4) file for at least each  pro‐
54       tocol family and type required. If  a non-zero protocol has been speci‐
55       fied but no exact match for the protocol family, type, and protocol  is
56       found,  then  the  first entry containing the specified family and type
57       with a protocol value of zero will be used.
58
59
60       A SOCK_STREAM type provides sequenced,  reliable,  two-way  connection-
61       based  byte  streams. An out-of-band data transmission mechanism may be
62       supported. A  SOCK_DGRAM  socket  supports  datagrams  (connectionless,
63       unreliable  messages  of  a  fixed (typically small) maximum length). A
64       SOCK_SEQPACKET socket may provide a sequenced, reliable,  two-way  con‐
65       nection-based  data  transmission  path  for datagrams of fixed maximum
66       length; a consumer may be required to read an entire packet  with  each
67       read system call. This facility is protocol specific, and presently not
68       implemented for any protocol family. SOCK_RAW sockets provide access to
69       internal  network  interfaces.  The  types SOCK_RAW, which is available
70       only to a user with the  net_rawaccess  privilege,  and  SOCK_RDM,  for
71       which no implementation currently exists, are not described here.
72
73
74       The protocol parameter is a protocol-family-specific value which speci‐
75       fies a particular protocol to be used with the socket.   Normally  this
76       value  is  zero, as commonly only a single protocol exists to support a
77       particular socket type within a given protocol family. However,  multi‐
78       ple  protocols  may  exist,  in which case a particular protocol may be
79       specified in this manner.
80
81
82       Sockets of type SOCK_STREAM are full-duplex byte  streams,  similar  to
83       pipes. A stream socket must be in a connected state before any data may
84       be sent or received on it. A connection to another  socket  is  created
85       with  a  connect(3SOCKET) call. Once connected, data may be transferred
86       using read(2) and write(2) calls or some variant of  the  send(3SOCKET)
87       and  recv(3SOCKET) calls. When a session has been completed, a close(2)
88       may be performed. Out-of-band data may also be transmitted as described
89       on  the  send(3SOCKET)  manual  page  and  received as described on the
90       recv(3SOCKET) manual page.
91
92
93       The communications protocols used to  implement  a  SOCK_STREAM  insure
94       that  data is not lost or duplicated.  If a piece of data for which the
95       peer protocol has  buffer  space  cannot  be  successfully  transmitted
96       within  a  reasonable length of time, then the connection is considered
97       broken and calls will indicate  an  error  with  −1  returns  and  with
98       ETIMEDOUT as the specific code in the global variable errno. The proto‐
99       cols optionally keep sockets "warm" by  forcing  transmissions  roughly
100       every  minute  in the absence of other activity. An error is then indi‐
101       cated if no response can be elicited on an  otherwise  idle  connection
102       for  a  extended  period  (for instance 5 minutes). A SIGPIPE signal is
103       raised if a thread sends on a broken stream;  this  causes  naive  pro‐
104       cesses, which do not handle the signal, to exit.
105
106
107       SOCK_SEQPACKET  sockets  employ  the  same  system calls as SOCK_STREAM
108       sockets. The only difference is that  read(2) calls  will  return  only
109       the  amount of data requested, and any remaining in the arriving packet
110       will be discarded.
111
112
113       SOCK_DGRAM and SOCK_RAW sockets allow datagrams to be  sent  to  corre‐
114       spondents  named  in  sendto(3SOCKET)  calls.  Datagrams  are generally
115       received with recvfrom(3SOCKET), which returns the next  datagram  with
116       its return address.
117
118
119       An  fcntl(2)  call  can be used to specify a process group to receive a
120       SIGURG signal when the out-of-band data arrives.  It  can  also  enable
121       non-blocking I/O.
122
123
124       The  operation  of sockets is controlled by socket level options. These
125       options are defined in the file <sys/socket.h>. setsockopt(3SOCKET) and
126       getsockopt(3SOCKET) are used to set and get options, respectively.
127

RETURN VALUES

129       Upon  successful  completion,  a  descriptor  referencing the socket is
130       returned. Otherwise, -1 is returned and errno is set  to  indicate  the
131       error.
132

ERRORS

134       The socket() function will fail if:
135
136       EACCES             Permission  to create a socket of the specified type
137                          or protocol is denied.
138
139
140       EAGAIN             There were insufficient resources available to  com‐
141                          plete the operation.
142
143
144       EAFNOSUPPORT       The specified address family is not supported by the
145                          protocol family.
146
147
148       EMFILE             The per-process descriptor table is full.
149
150
151       ENOMEM             Insufficient user memory is available.
152
153
154       ENOSR              There were insufficient STREAMS resources  available
155                          to complete the operation.
156
157
158       EPFNOSUPPORT       The specified protocol family is not supported.
159
160
161       EPROTONOSUPPORT    The  protocol  type  is not supported by the address
162                          family.
163
164
165       EPROTOTYPE         The socket type is not supported by the protocol.
166
167

ATTRIBUTES

169       See attributes(5) for descriptions of the following attributes:
170
171
172
173
174       ┌─────────────────────────────┬─────────────────────────────┐
175       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
176       ├─────────────────────────────┼─────────────────────────────┤
177       │MT-Level                     │Safe                         │
178       └─────────────────────────────┴─────────────────────────────┘
179

SEE ALSO

181       nca(1),    close(2),    fcntl(2),    ioctl(2),    read(2),    write(2),
182       accept(3SOCKET), bind(3SOCKET), connect(3SOCKET), getsockname(3SOCKET),
183       getsockopt(3SOCKET), in.h(3HEAD),listen(3SOCKET),  recv(3SOCKET),  set‐
184       sockopt(3SOCKET),  send(3SOCKET),  shutdown(3SOCKET),  socket.h(3HEAD),
185       socketpair(3SOCKET), attributes(5)
186

NOTES

188       Historically, AF_* was commonly used in places where  PF_*  was  meant.
189       New code should be careful to use PF_* as necessary.
190
191
192
193SunOS 5.11                        28 Jan 2009                  socket(3SOCKET)
Impressum