ptg10805159
620 Network IPC: Sockets Chapter 16
The other choice we have is to deal with the error by letting the user retry the
command. For simple applications this might be adequate, but for complex
applications it usually isn’t a viable alternative. Thus, it is generally better to use
connection-oriented sockets in this case.
The drawbacks to connection-oriented sockets arethat morework and time are
needed to establish a connection, and each connection consumes moreresources from
the operating system.
Example — Connectionless Client
The program in Figure16.19 is a version of theuptimeclient command that uses the
datagram socket interface.
#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#define BUFLEN 128
#define TIMEOUT 20
void
sigalrm(int signo)
{
}
void
print_uptime(int sockfd, struct addrinfo *aip)
{
int n;
char buf[BUFLEN];
buf[0] = 0;
if (sendto(sockfd, buf, 1, 0, aip->ai_addr, aip->ai_addrlen) < 0)
err_sys("sendto error");
alarm(TIMEOUT);
if ((n = recvfrom(sockfd, buf, BUFLEN, 0, NULL, NULL)) < 0) {
if (errno != EINTR)
alarm(0);
err_sys("recv error");
}
alarm(0);
write(STDOUT_FILENO, buf, n);
}
int
main(int argc, char *argv[])
{
struct addrinfo *ailist, *aip;
struct addrinfo hint;