sockets/is_echo_cl.cThis is sockets/is_echo_cl.c (Listing 61-2, page 1258), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
+/* is_echo_cl.c + + A simple client to communicate with the standard "echo" server. + + In many Linux distributions, the "echo" server is not started by default. + Normally it is implemented internally by inetd(8), and to enable we must + do the following *as root*: + + 1. Edit the file /etc/inetd.conf, and uncomment the two lines + for the the "echo" service by removing the '#' character at the + beginning of the line. The lines typically look like this: + + # echo stream tcp nowait root internal + # echo dgram udp wait root internal + + and we must change them to this: + + echo stream tcp nowait root internal + echo dgram udp wait root internal + + 2. Inform inetd(8) of the change using the following command: + + bash# killall -HUP inetd + + If your system uses xinetd(8) instead of inetd(8), then read the + xinetd.conf(5) manual page. (You may also find that your system has a GUI + admin tool that allows you to easily enable/disable the "echo" service.) + + See also is_echo_sv.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); } }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.