Creates an end point for communication and returns a descriptor.
Standard C Library (libc.a)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
int socket ( AddressFamily, Type, Protocol)
int AddressFamily, Type, Protocol;
The socket subroutine creates a socket in the specified AddressFamily and of the specified type. A protocol can be specified or assigned by the system. If the protocol is left unspecified (a value of 0), the system selects an appropriate protocol from those protocols in the address family that can be used to support the requested socket type.
The socket subroutine returns a descriptor (an integer) that can be used in later subroutines that operate on sockets.
Socket level options control socket operations. The getsockopt and setsockopt subroutines are used to get and set these options, which are defined in the /usr/include/sys/socket.h file.
Item | Description |
---|---|
AddressFamily | Specifies an address family with which addresses specified
in later socket operations should be interpreted. The /usr/include/sys/socket.h file
contains the definitions of the address families. Commonly used families
are:
|
Type | Specifies the semantics of communication. The /usr/include/sys/socket.h file
defines the socket types. The operating system supports the following
types:
|
Protocol | Specifies a particular protocol to be used with the socket. Specifying the Protocol parameter of 0 causes the socket subroutine to default to the typical protocol for the requested type of returned socket. For SCTP sockets, the protocol parameter is IPPROTO_SCTP. For RDS sockets, the Protocol parameter is BYPASSPROTO_RDS. |
Upon successful completion, the socket subroutine returns an integer (the socket descriptor).
If the socket subroutine is unsuccessful, the subroutine handler performs the following functions:
The socket subroutine is unsuccessful if any of the following errors occurs:
Error | Description |
---|---|
EAFNOSUPPORT | The addresses in the specified address family cannot be used with this socket. |
EMFILE | The per-process descriptor table is full. |
ENOBUFS | Insufficient resources were available in the system to complete the call. |
ESOCKTNOSUPPORT | The socket in the specified address family is not supported. |
The following program fragment illustrates the use of the socket subroutine to create a datagram socket for on-machine use:
s = socket(AF_UNIX, SOCK_DGRAM,0);
The socket subroutine is part of Base Operating System (BOS) Runtime.
The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.