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.
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:
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:
Return valuesA nonnegative socket descriptor
indicates success. The value -1 indicates
an error. Errno identifies the specific error.
Example
Related callsaccept(), bind(), close() connect(), fcntl(), getprotobyname(), getsockname(), getsockopt(), ioctl(), maxdesc(), read(), readv(), recv(), recvfrom(), recvmsg(), select(), selectex(), send(), sendmsg(), sendto(), shutdown(), write(), writev() |
Copyright IBM Corporation 1990, 2014
|