Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'winsup/cygwin/socket_tests/us_xfr_v2_sv.c')
-rw-r--r--winsup/cygwin/socket_tests/us_xfr_v2_sv.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/winsup/cygwin/socket_tests/us_xfr_v2_sv.c b/winsup/cygwin/socket_tests/us_xfr_v2_sv.c
new file mode 100644
index 000000000..517129a4b
--- /dev/null
+++ b/winsup/cygwin/socket_tests/us_xfr_v2_sv.c
@@ -0,0 +1,57 @@
+/*************************************************************************\
+* Copyright (C) Michael Kerrisk, 2018. *
+* *
+* This program is free software. You may use, modify, and redistribute it *
+* under the terms of the GNU General Public License as published by the *
+* Free Software Foundation, either version 3 or (at your option) any *
+* later version. This program is distributed without any warranty. See *
+* the file COPYING.gpl-v3 for details. *
+\*************************************************************************/
+
+/* Solution for Exercise 59-3:d */
+
+/* us_xfr_v2_sv.c
+
+ An example UNIX stream socket server. Accepts incoming connections and
+ copies data sent from clients to stdout. This program is similar to
+ us_xfr_sv.c, except that it uses the functions in unix_sockets.c to
+ simplify working with UNIX domain sockets.
+
+ See also us_xfr_v2_cl.c.
+*/
+#include "us_xfr_v2.h"
+
+int
+main(int argc, char *argv[])
+{
+ int sfd, cfd;
+ ssize_t numRead;
+ char buf[BUF_SIZE];
+
+ if (remove (SV_SOCK_PATH) < 0 && errno != ENOENT)
+ errExit ("remove");
+
+ sfd = unixBind(SV_SOCK_PATH, SOCK_STREAM);
+ if (sfd == -1)
+ errExit("unixBind");
+
+ if (listen(sfd, 5) == -1)
+ errExit("listen");
+
+ for (;;) { /* Handle client connections iteratively */
+ cfd = accept(sfd, NULL, NULL);
+ if (cfd == -1)
+ errExit("accept");
+
+ /* Transfer data from connected socket to stdout until EOF */
+
+ while ((numRead = read(cfd, buf, BUF_SIZE)) > 0)
+ if (write(STDOUT_FILENO, buf, numRead) != numRead)
+ fatal("partial/failed write");
+
+ if (numRead == -1)
+ errExit("read");
+ if (close(cfd) == -1)
+ errMsg("close");
+ }
+}