|
Description
The socket() function creates a communications endpoint and returns a descriptor.
The domain parameter defines the communications domain in which communications are to take place. This also defines the protocol family to be used. The protocol family generally corresponds to the family of the addresses used for later operations on the socket. These families are defined in the <sys/socket.h> header file. The AF_INET, AF_INET6 and AF_UNIX protocol families are supported.
The type parameter defines the type of the socket and the semantics of the communications. The two following socket types are defined:
SOCK_STREAM
SOCK_DGRAM
The type SOCK_STREAM socket provides a sequential, secured, bidirectional connection. A socket of type SOCK_DGRAM supports the transfer of datagrams, which are connectionless, unsecured messages with a fixed maximum length.
The protocol parameter defines a specific protocol that is to be used for the socket. Since this implementation only supports the TCP/IP protocol family, only the values 0 (standard protocol), IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP and IPPROTO_UDP are permitted here for general use. Other protocols are reserved for internal use.
Sockets of type SOCK_STREAM are full duplex data streams, similar to pipes. A stream socket must be in a connected state before any data can be sent or received over it. A connection to another socket is set up with the connect() function. Once two sockets are connected together, data can be transferred with read() and write() calls or similar functions such as send() and recv(). The program should call the close() function when a session is finished.
The communications protocols used for a stream socket ensure that data is not lost or duplicated.
Type SOCK_DGRAM sockets allow the connectionless sending and receiving of datagrams with sendto() and recvfrom() or sendmsg() and recvmsg(). When these functions are called, the address of the communications partner is passed as a parameter.
The program can specify a process group with the fcntl() function to receive a SIGIO signal when input/output operations or connection setup requests arrive.
Socket operations are controlled by socket level options and defined in the <sys/socket.h> header file. The program can query and modify these options with the getsockopt() and setsockopt() functions respectively.
Return value
≥0:
Designates a non-negative descriptor if successful.
-1:
If errors occur. errno is set to indicate the error.
Errors
EACCES
No permission is granted for creating a socket of the specified type or protocol.
EMFILE
The table of descriptors per process is full.
ENFILE
The system file table is full.
ENOBUFS
Not enough space in the buffer. The socket cannot be created until enough storage resources are freed.
EPROTONOSUPPORT
The protocol type or the specified protocol is not supported in this domain.
EPROTOTYPE
Wrong protocol type for the socket.
EAFNOSUPPORT
The address family specified in the domain parameter is not supported on this system. See also "Dependencies on the BS2000 transport system BCAM" for more information.
See also
accept(), bind(), connect(), getsockname(), getsockopt(), listen(), recv(), send(), shutdown(), socketpair(), close(), fcntl(), ioctl(), read(), select(), write()