You can use the following example to write a socket network application.
The example is written using C socket syntax conventions, but the
principles described apply to all of the following APIs:
- TCP/IP C socket API
- X/Open Transport Interface
- Macro API for IPv4 or IPv6 that is written in z/OS® assembler language
- Call instruction API for IPv4 or IPv6 socket applications
- z/OS Communications Server
socket API for REXX
- Pascal language for IPv4 socket API
Clients and servers wanting to transfer data have many calls from
which to choose. The read() and write(), readv() and writev(), and the send() and recv() calls can be used only on sockets that are connected.
The sendto() and recvfrom(), and sendmsg() and recvmsg() calls can
be used at any time. The example listed in
Figure 1 illustrates the use of send()
and recv() calls:
Figure 1. An application
using the send() and recv() callsint send(int socket, char *buf, int buflen, int flags);
int recv(int socket, char *buf, int buflen, int flags);
.
.
.
int bytes_sent;
int bytes_received;
char data_sent[256];
char data_received[256];
int s;
.
.
.
bytes_sent = send(s, data_sent, sizeof(data_sent), 0);
.
.
.
bytes_received = recv(s, data_received, sizeof(data_received), 0);
The example in
Figure 1 shows
an application sending data to a connected socket and receiving data in response.
The flags field can be used to specify additional options to send()
or recv(), such as sending out-of-band data. For more information
about these routines, see the following information:
There are three groups of calls to use for reading and writing
data over sockets:
- read and write
- These calls can be used only with connected sockets. No processing
flags can be passed on these calls.
- recv and send
- These calls also work with connected sockets only. You can pass
processing flags on these calls:
- NOFLAG - Read or write data as a read call or a write call would.
- OOB - Read or write Out Of Band data (expedited data).
- PEEK - Peek at data, but do not remove data from the buffers.
- recvfrom and sendto
- These calls work with both connected and non-connected sockets.
You can pass addressing information directly (as parameters) on these
calls. The available flags are the same as those for recv and send.
A connected socket is either a stream socket for which a connection
has been established, or it is a datagram socket for which you have
issued a connect() call to specify the remote datagram socket address.