z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


socket()

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

The socket() call creates an endpoint for communication and returns a socket descriptor representing that endpoint. Different types of sockets provide different communication services.

#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
 
int socket(int domain, int type, int protocol)
Parameter
Description
domain
The address domain requested. It is either AF_INET or AF_IUCV.
type
The type of socket created, either SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW.
protocol
The protocol requested. Possible values are 0, IPPROTO_UDP, or IPPROTO_TCP.

The domain parameter specifies the communication domain within which communication is to take place. This parameter specifies the address family (format of addresses within a domain) to be used. The families supported are AF_INET, which is the internet domain, and AF_IUCV, which is the IUCV domain. These constants are defined in the SOCKET.H header file.

The type parameter specifies the type of socket created. The type is analogous to the communication requested. These socket type constants are defined in the SOCKET.H header file. The types supported are:
Socket Type
Description
SOCK_STREAM
Provides sequenced, two-way byte streams that are reliable and connection-oriented. They support a mechanism for out-of-band data. This type is supported in both the AF_INET and AF_IUCV domains.
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 repeatedly. This type is supported in the AF_INET domain only.
SOCK_RAW
Provides the interface to internal protocols (such as IP and ICMP). This type is supported in the AF_INET domain only.
Note: To use raw sockets, the application must be APF-authorized.

The protocol parameter specifies the particular protocol to be used with the socket. In most cases, a single protocol exists to support a particular type of socket within a particular addressing family (not true with raw sockets). If the protocol parameter is set to 0, the system selects the default protocol number for the domain and socket type requested. Protocol numbers are found in the hlq.ETC.PROTO data set. Alternatively, the getprotobyname() call can be used to get the protocol number for a protocol with a known name. The protocol field must be set to 0 if the domain parameter is set to AF_IUCV. The protocol defaults are TCP for stream sockets and UDP for datagram sockets. There is no default for raw sockets.

SOCK_STREAM sockets model duplex byte streams. They provide reliable, flow-controlled connections between peer applications. Stream sockets are either active or passive. Active sockets are used by clients who initiate connection requests using connect(). By default, socket() creates active sockets. Passive sockets are used by servers to accept connection requests from the connect() call. An active socket is transformed into a passive socket by binding a name to the socket using the bind() call, and by indicating a willingness to accept connections with the listen() call. If a socket is passive, it cannot be used to initiate connection requests.

In the AF_INET domain, the bind() call applied to a stream socket lets the application specify the networks from which it will accept connection requests. The application can fully specify the network interface by setting the internet address field in the address structure to the internet address of a network interface. Alternatively, the application can use a wildcard to specify that it wants to receive connection requests from any network. This is done by setting the internet address field within the address structure to the constant INADDR_ANY, as defined in the SOCKET.H header file.

After a connection has been established between stream sockets, any of the data transfer calls can be used: (read(), write(), send(), recv(), readv(), writev(), sendto(), recvfrom(), sendmsg(), and recvmsg()). Usually, the read-write or send-recv pairs are used to send data on stream sockets. If out-of-band data is to be exchanged, the send-recv pair is normally used.

SOCK_DGRAM sockets model datagrams. They provide connectionless message-exchange without guarantee of reliability. Messages sent are limited in size. Datagram sockets are not supported in the AF_IUCV domain.

There is no active or passive analogy to stream sockets with datagram sockets. Servers must still call bind() to name a socket and to specify the network interface from which it wants to receive packets. Wildcard addressing, as described for stream sockets, applies to datagram sockets also. Because datagram sockets are connectionless, the listen() call has no meaning for them and must not be used with them.

After an application has received a datagram socket, it can exchange datagrams using the sendto() and recvfrom(), or sendmsg() and recvmsg() calls. If the application goes one step further by calling connect() and fully specifying the name of the peer with which all messages are to be exchanged, then the other data transfer calls of read(), write(), readv(), writev(), send(), and recv() can be used also. See connect() for more information about placing a socket into the connected state.

Datagram sockets allow messages to be broadcast to multiple recipients. Setting the destination address to a broadcast address depends on the class of address, and whether subnets are used. The constant INADDR_BROADCAST, defined in socket.h, can be used to broadcast to the primary network when the primary network configured supports broadcast.

SOCK_RAW sockets give the application an interface to lower layer protocols, such as IP and ICMP. This interface is often used to bypass the transport layer when direct access to lower layer protocols is needed. Raw sockets are also used to test new protocols. Raw sockets are not supported in the AF_IUCV domain.

Raw sockets are connectionless and data transfer semantics are the same as those described previously for datagram sockets. The connect() call can be used similarly to specify the peer.

Outgoing packets have an IP header prefixed to them. IP options can be set and inspected using the setsockopt() and getsockopt() calls respectively. Incoming packets are received with the IP header and options intact.

Notes:
  1. Sockets are deallocated using the close() call.
  2. Only SOCK_STREAM sockets are supported in the AF_IUCV domain.
  3. The setsockopt() and getsockopt() calls are not supported for sockets in the AF_IUCV domain.
  4. The flags field in the send(), recv(), sendto(), recvfrom(), sendmsg(), and recvmsg() calls is not supported in the AF_IUCV domain.

Return values

A nonnegative socket descriptor indicates success. The value -1 indicates an error. Errno identifies the specific error.
Errno
Description
EPROTONOSUPPORT
The protocol is not supported in this domain or this socket type.
EACCES
Access denied. The application is not an APF-authorized application.
EAFNOSUPPORT
The specified address family is not supported by this protocol family.

Example

int s;
struct protoent *p;
struct protoent *getprotobyname(char *name);
int socket(int domain, int type, int protocol);
⋮
/* Get stream socket in internetdomain with default protocol */
s = socket(AF_INET, SOCK_STREAM, 0);
⋮
/* Get stream socket in iucvdomain with default protocol */
s = socket(AF_IUCV, SOCK_STREAM, 0);
⋮
/* Get raw socket in internetdomain for ICMP protocol */
p = getprotobyname(“iucv”);
s = socket(AF_INET, SOCK_RAW, p->p_proto);

Related calls

accept(), bind(), close() connect(), fcntl(), getprotobyname(), getsockname(), getsockopt(), ioctl(), maxdesc(), read(), readv(), recv(), recvfrom(), recvmsg(), select(), selectex(), send(), sendmsg(), sendto(), shutdown(), write(), writev()

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014