summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/OR_USING_C/11.1.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-01-27 11:37:44 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-01-27 11:37:44 +0100
commit7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch)
treeb1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/CONTRIB/OR_USING_C/11.1.c
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/CONTRIB/OR_USING_C/11.1.c')
-rw-r--r--reference/C/CONTRIB/OR_USING_C/11.1.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/OR_USING_C/11.1.c b/reference/C/CONTRIB/OR_USING_C/11.1.c
new file mode 100644
index 0000000..34be61e
--- /dev/null
+++ b/reference/C/CONTRIB/OR_USING_C/11.1.c
@@ -0,0 +1,91 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdio.h>
+
+#define NSTRS 3 /* no. of strings */
+#define ADDRESS "mysocket" /* addr to connect */
+
+/*
+ * Strings we send to the server.
+ */
+char *strs[NSTRS] = {
+ "This is the first string from the client.\n",
+ "This is the second string from the client.\n",
+ "This is the third string from the client.\n"
+};
+
+main()
+{
+ char c;
+ FILE *fp;
+ register int i, s, len;
+ struct sockaddr_un saun;
+
+ /*
+ * Get a socket to work with. This socket will
+ * be in the UNIX domain, and will be a
+ * stream socket.
+ */
+ if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("client: socket");
+ exit(1);
+ }
+
+ /*
+ * Create the address we will be connecting to.
+ */
+ saun.sun_family = AF_UNIX;
+ strcpy(saun.sun_path, ADDRESS);
+
+ /*
+ * Try to connect to the address. For this to
+ * succeed, the server must already have bound
+ * this address, and must have issued a listen()
+ * request.
+ *
+ * The third argument indicates the "length" of
+ * the structure, not just the length of the
+ * socket name.
+ */
+ len = sizeof(saun.sun_family) + strlen(saun.sun_path);
+
+ if (connect(s, &saun, len) < 0) {
+ perror("client: connect");
+ exit(1);
+ }
+
+ /*
+ * We'll use stdio for reading
+ * the socket.
+ */
+ fp = fdopen(s, "r");
+
+ /*
+ * First we read some strings from the server
+ * and print them out.
+ */
+ for (i = 0; i < NSTRS; i++) {
+ while ((c = fgetc(fp)) != EOF) {
+ putchar(c);
+
+ if (c == '\n')
+ break;
+ }
+ }
+
+ /*
+ * Now we send some strings to the server.
+ */
+ for (i = 0; i < NSTRS; i++)
+ send(s, strs[i], strlen(strs[i]), 0);
+
+ /*
+ * We can simply use close() to terminate the
+ * connection, since we're done with both sides.
+ */
+ close(s);
+
+ exit(0);
+}
+