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