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


Using sendto() and recvfrom() calls

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

If the socket is not in a connected state, additional address information must be passed to sendto() and can be (optionally) returned from recvfrom(). An example of the sendto() and recvfrom() calls is listed in Figure 1:
Figure 1. An application using the sendto() and recvfrom() Calls
int sendto(int socket, char *buf, int buflen, int flags,
           struct sockaddr *addr, int addrlen);
int recvfrom(int socket, char *buf, int buflen, int flags,
           struct sockaddr *addr, int *addrlen);
⋮int bytes_sent;
int bytes_received;
char data_sent[256];
char data_received[256];
struct sockaddr_in to;
struct sockaddr from;
int addrlen;
int s;
⋮
memset(&to, 0, sizeof(to));
to.sin_family = AF_INET;
to.sin_addr   = inet_addr(“129.5.24.1”);
to.sin_port   = htons(1024);
⋮
bytes_sent = sendto(s, data_sent, sizeof(data_sent), 0,
           (struct sockaddr*)&to, sizeof(to));
⋮
addrlen = sizeof(from); /* must be initialized */
bytes_received = recvfrom(s, data_received,
   sizeof(data_received), 0, &from, &addrlen)
The sendto() and recvfrom() calls take additional parameters to allow the caller to specify the recipient of the data, or to be notified of the sender of the data. See recvfrom(), sendmsg(), and sendto() for more information about these additional parameters. Usually, sendto() and recvfrom() are used for datagram sockets, and send() and recv() are used for stream sockets.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014