[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

socket Subroutine


Creates an end point for communication and returns a descriptor.


Standard C Library (libc.a)


#include <sys/socket.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.

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.


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:
Denotes the operating system path names.
Denotes the ARPA Internet addresses.
Denotes the XEROX Network Systems protocol.
Type Specifies the semantics of communication. The /usr/include/sys/socket.h file defines the socket types. The operating system supports the following types:
Provides sequenced, two-way byte streams with a transmission mechanism for out-of-band data.
Provides datagrams, which are connectionless messages of a fixed maximum length (usually short).
Provides access to internal network protocols and interfaces. This type of socket is available only to the root user.
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.

Return Values

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:

Error Codes

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.
ESOCKTNOSUPPORT The socket in the specified address family is not supported.
EMFILE The per-process descriptor table is full.
ENOBUFS Insufficient resources were available in the system to complete the call.


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);

Related Information

The accept subroutine, bind subroutine, connect subroutine, getsockname subroutine, getsockopt subroutine, ioctl subroutine, listen subroutine, recv subroutine, recvfrom subroutine, recvmsg subroutine, select subroutine, send subroutine, sendmsg subroutine, sendto subroutine, setsockopt subroutine, shutdown subroutine, socketpair subroutine.

Initiating Internet Stream Connections Example Program, Sockets Overview, Understanding Socket Creation in AIX 5L Version 5.2 Communications Programming Concepts.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]