'\" te .\" Copyright (C) 2009, Sun Microsystems, Inc. All Rights Reserved. .\" Copyright 1989 AT&T .\" 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 #include \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\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 There must be an entry in the \fBnetconfig\fR(4) 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 .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) 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 \fBnca\fR(1), \fBclose\fR(2), \fBfcntl\fR(2), \fBioctl\fR(2), \fBread\fR(2), \fBwrite\fR(2), \fBaccept\fR(3SOCKET), \fBbind\fR(3SOCKET), \fBconnect\fR(3SOCKET), \fBgetsockname\fR(3SOCKET), \fBgetsockopt\fR(3SOCKET), \fBin.h\fR(3HEAD),\fBlisten\fR(3SOCKET), \fBrecv\fR(3SOCKET), \fBsetsockopt\fR(3SOCKET), \fBsend\fR(3SOCKET), \fBshutdown\fR(3SOCKET), \fBsocket.h\fR(3HEAD), \fBsocketpair\fR(3SOCKET), \fBattributes\fR(5) .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.