NAME | SYNOPSIS | FEATURES | DESCRIPTION | RETURN VALUE | ATTRIBUTES | SEE ALSO
#include <sys/types.h> #include <sys/socket.h>int socket(int domain, int type, int protocol);
POSIX_SOCKETS
The socket system call creates an endpoint for communication and returns a descriptor.
The domain parameter specifies a communications domain within which communication will take place; this selects the protocol family which should be used. The protocol family generally is the same as the address family for the addresses supplied in later operations on the socket. These families are defined in the include file <sys/socket.h>. The formats currently understood are:
AF_INET (ARPA Internet protocols),
The socket has the typeindicated, which specifies the semantics of communication. The types currently defined are:
SOCK_STREAM SOCK_DGRAM SOCK_RAW
A SOCK_STREAM type provides sequenced, reliable, two-way, connection based byte streams. An out-of-band data transmission mechanism may be supported. A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed (typically small) maximum length). The SOCK_RAW sockets provide access to internal network protocols and interfaces. The SOCK_RAW type, which is available only to the super-user, is not described.
The protocol specifies a particular protocol to be used with the socket. However, as the current implementation only supports the SOCK_STREAM, SOCK_DGRAM and SOCK_RAW types within the PF_INET family, protocol may be supplied as 0, as only a single protocol exists to support a particular socket type within the PF_INET familly.
Sockets of the type SOCK_STREAM are full-duplex byte streams. A stream socket must be in a connected state before any data may be sent or received. A connection to another socket is created with a connect(2POSIX) call. Once connected, data may be transferred using the read(2POSIX) and write(2POSIX) calls or variants of the send(2POSIX) and recv(2POSIX) calls. When a session has been completed, a close(2POSIX) may be performed. Out-of-band data may also be transmitted as described in send(2POSIX) and received as described in recv(2POSIX).
The communications protocols used to implement a SOCK_STREAM ensure that data is not lost or duplicated. If a piece of data for which the peer protocol has buffer space cannot be transmitted successfully within a reasonable length of time, the connection is considered broken. Calls will indicate an error by returning a value of -1, and setting errno to ETIMEOUT.. 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 an extended period (for exmaple, 5 minutes).
The SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to correspondents named in send(2POSIX) calls. Datagrams are generally received using recvfrom(2K), which returns the next datagram with its return address.
The operation of sockets is controlled by the socket level options. These options are defined in the file <sys/socket.h>. The setsockopt(2K) and getsockopt(2POSIX) functions are used to set and get options, respectively.
Upon successful completion, socket returns a non-negative integer that is the descriptor of the new socket; otherwise it returns -1 and sets errno to indicate one of the following error conditions:
The protocol type or the specified protocol is not supported within this domain.
The c_actor descriptor table is full.
The system file table is full.
Permission to create a socket of the specified type and/or protocol is denied.
Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
---|---|
Interface Stability | Evolving |
accept(2POSIX), bind(2POSIX), connect(2POSIX), getsockname(2POSIX), getsockopt(2POSIX), ioctl(2POSIX), listen(2POSIX), read(2POSIX), recv(2POSIX), select(2POSIX), send(2POSIX), shutdown(2POSIX), write(2POSIX)
NAME | SYNOPSIS | FEATURES | DESCRIPTION | RETURN VALUE | ATTRIBUTES | SEE ALSO