From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/11.2.c | 109 ++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/11.2.c (limited to 'reference/C/CONTRIB/OR_USING_C/11.2.c') diff --git a/reference/C/CONTRIB/OR_USING_C/11.2.c b/reference/C/CONTRIB/OR_USING_C/11.2.c new file mode 100644 index 0000000..18140aa --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/11.2.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include + +#define NSTRS 3 /* no. of strings */ +#define ADDRESS "mysocket" /* addr to connect */ + +/* + * Strings we send to the client. + */ +char *strs[NSTRS] = { + "This is the first string from the server.\n", + "This is the second string from the server.\n", + "This is the third string from the server.\n" +}; + +main() +{ + char c; + FILE *fp; + int fromlen; + register int i, s, ns, len; + struct sockaddr_un saun, fsaun; + + /* + * 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("server: socket"); + exit(1); + } + + /* + * Create the address we will be binding to. + */ + saun.sun_family = AF_UNIX; + strcpy(saun.sun_path, ADDRESS); + + /* + * Try to bind the address to the socket. We + * unlink the name first so that the bind won't + * fail. + * + * The third argument indicates the "length" of + * the structure, not just the length of the + * socket name. + */ + unlink(ADDRESS); + len = sizeof(saun.sun_family) + strlen(saun.sun_path); + + if (bind(s, &saun, len) < 0) { + perror("server: bind"); + exit(1); + } + + /* + * Listen on the socket. + */ + if (listen(s, 5) < 0) { + perror("server: listen"); + exit(1); + } + + /* + * Accept connections. When we accept one, ns + * will be connected to the client. fsaun will + * contain the address of the client. + */ + if ((ns = accept(s, &fsaun, &fromlen)) < 0) { + perror("server: accept"); + exit(1); + } + + /* + * We'll use stdio for reading the socket. + */ + fp = fdopen(ns, "r"); + + /* + * First we send some strings to the client. + */ + for (i = 0; i < NSTRS; i++) + send(ns, strs[i], strlen(strs[i]), 0); + + /* + * Then we read some strings from the client and + * print them out. + */ + for (i = 0; i < NSTRS; i++) { + while ((c = fgetc(fp)) != EOF) { + putchar(c); + + if (c == '\n') + break; + } + } + + /* + * We can simply use close() to terminate the + * connection, since we're done with both sides. + */ + close(s); + + exit(0); +} + -- cgit v1.2.3-54-g00ecf