socket: Create an endpoint for communication

The socket function creates an endpoint for communication and returns a socket descriptor representing the endpoint. Different types of sockets provide different communication services.

Last updated

  • Changed for PUT13.
  • Added for PUT00.

Format

#include    <sys/socket.h>
int         socket(int domain,
                   int type,
                   int protocol);
domain
The address domain. Specify a value of AF_INET or AF_UNIX. The domain parameter specifies a domain in which communication is to take place.
type
Specifies the type of socket created. The type is analogous with the semantics of the communication requested. Specify one of the following values:
SOCK_STREAM
Provides sequenced, duplex byte streams that are reliable and connection-oriented. They support a mechanism for out-of-band data.
SOCK_DGRAM
Provides datagrams, which are connectionless messages of a fixed maximum length whose reliability is not guaranteed. Datagrams can be corrupted, received out of order, lost, or delivered multiple times.
SOCK_RAW
Provides the interface to internal protocols (such as IP). This type is not supported for AF_UNIX domain sockets.
protocol
The protocol requested. Specify one of the following values:
0
Default protocol based on the domain and type.
IPPROTO_IP
Specifies the Internet Protocol.
IPPROTO_ICMP
Specifies the Internet Control Message protocol.
IPPROTO_TCP
Specifies the Transmission Control Protocol (TCP). This is the default for a type of SOCK_STREAM.
IPPROTO_UDP
Specifies the User Datagram Protocol (UDP). This is the default for a type of SOCK_DGRAM.
IPPROTO_RAW
Specifies a raw IP packet.
The protocol parameter specifies a particular protocol to be used with the socket. In most cases, a single protocol exists to support a particular type of socket in a particular addressing family (not true with raw sockets). If the protocol field is set to 0, the system selects the default protocol number for the domain and socket type requested. Currently, protocol defaults are TCP for stream sockets and UDP for datagram sockets. There is no default for raw sockets.

Normal return

A nonnegative socket descriptor indicates success.

Error return

A return code equal to -1 indicates an error. You can get the specific error code by calling sock_errno. See z/TPF Transmission Control Protocol/Internet Protocol for more information about socket errors.
SOCNOBUFS
There is not enough space to create a new socket because the socket block table is full.
SOCPROTONOSUPPORT
The protocol is not supported in this domain or this protocol is not supported for this socket type.
SOCAFNOSUPPORT
The address family is not supported.
SOCSOCKTNOSUPPORT
The socket type is not supported.
E1052STATE
The socket was closed because the system was in or cycling down to 1052 state.
ESOCINACT
The socket was not created because socket creation is disabled in the system.
Note: The value of ESOCINACT is equal to the E1052STATE error number to avoid having to update existing applications when using ZSOCK DISABLE.

Programming considerations

  • By default, sockets can be created only in CRAS state or above and are cleaned up if the system cycles below CRAS state. Issue the SetTCP1052 function before the socket function to create and use a socket in 1052 state. Enable the TPF_SURVIVE_CYCLE option on the ioctl function to prevent a socket from being cleaned up when the system cycles below CRAS state.
  • If a TCP listener socket is created with the ability to run in 1052 state, the connected sockets created by the listener also have the ability to run in 1052 state.
  • If you need a connection with remote nodes below CRAS state, define a system state of 1052 for the OSA-Express connection associated with the local IP address of the socket.
  • For AF_UNIX sockets, only the SOCK_STREAM and SOCK_DGRAM types are allowed. The SOCK_RAW type is supported only for AF_INET sockets.

Examples

  1. In the following example, a stream socket is created in the INET domain:
    #include <sys/socket.h>
    ⋮
    int server_sock;
    ⋮
    server_sock = socket(AF_INET, SOCK_STREAM, 0);
  2. In the following example, a stream socket is created in the UNIX domain:
    #include <sys/socket.h>
    ⋮
    int server_sock;
    ⋮
    server_sock = socket (AF_UNIX, SOCK_STREAM, 0);
    if (rc == -1){     
            printf(“SOCKET ERROR = %d\n”, sock_errno());
    }