socket Subroutine

Purpose

Creates an end point for communication and returns a descriptor.

Library

Standard C Library (libc.a)

Syntax

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
int socket ( AddressFamily,  Type,  Protocol)
int AddressFamily, Type, Protocol;

Description

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.

Parameters

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:
AF_UNIX
Denotes the operating system path names.
AF_INET
Denotes the ARPA Internet addresses.
AF_INET6
Denotes the IPv6 and IPv4 addresses.
AF_NS
Denotes the XEROX Network Systems protocol.
AF_BYPASS
Denotes the kernel-bypass protocol domain (for example, the protocols that operate on the InfiniBand domain).
Type Specifies the semantics of communication. The /usr/include/sys/socket.h file defines the socket types. The operating system supports the following types:
SOCK_STREAM
Provides sequenced, two-way byte streams with a transmission mechanism for out-of-band data.
SOCK_DGRAM
Provides datagrams, which are connectionless messages of a fixed maximum length (usually short).
SOCK_RAW
Provides access to internal network protocols and interfaces. This type of socket is available only to the root user, or to non-root users who have the CAP_NUMA_ATTACH capability. (For non-root raw socket access, the CAP_NUMA_ATTACH capability, along with CAP_PROPAGATE, is assigned using the chuser command. For more information about the chuser command, see chuser Command in Commands Reference, Volume 1.)
SOCK_SEQPACKET
Provides sequenced, reliable, and unduplicated flow of information. This type of socket is used for UDP-style socket creation in case of Stream Control Transmission Protocol and Reliable Datagram Sockets (RDS) Protocol.
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.

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:

  • Returns a value of -1 to the calling program.
  • Moves an error code, indicating the specific error, into the errno global variable. For further explanation of the errno variable see Error Notification Object Class in General Programming Concepts: Writing and Debugging Programs.

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.
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.

Examples

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

Implementation Specifics

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.