The Linux Programming Interface

(nextflipdebug5) #1

1258 Chapter 61


Listing 61-2: A client for the echo service
––––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/is_echo_cl.c
#include "inet_sockets.h"
#include "tlpi_hdr.h"

#define BUF_SIZE 100

int
main(int argc, char *argv[])
{
int sfd;
ssize_t numRead;
char buf[BUF_SIZE];

if (argc != 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s host\n", argv[0]);

sfd = inetConnect(argv[1], "echo", SOCK_STREAM);
if (sfd == -1)
errExit("inetConnect");

switch (fork()) {
case -1:
errExit("fork");

case 0: /* Child: read server's response, echo on stdout */
for (;;) {
numRead = read(sfd, buf, BUF_SIZE);
if (numRead <= 0) /* Exit on EOF or error */
break;
printf("%.*s", (int) numRead, buf);
}
exit(EXIT_SUCCESS);

default: /* Parent: write contents of stdin to socket */
for (;;) {
numRead = read(STDIN_FILENO, buf, BUF_SIZE);
if (numRead <= 0) /* Exit loop on EOF or error */
break;
if (write(sfd, buf, numRead) != numRead)
fatal("write() failed");
}

/* Close writing channel, so server sees EOF */

if (shutdown(sfd, SHUT_WR) == -1)
errExit("shutdown");
exit(EXIT_SUCCESS);
}
}
––––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/is_echo_cl.c
Free download pdf