1*27700Smckusick .\" Copyright (c) 1986 Regents of the University of California.
2*27700Smckusick .\" All rights reserved.  The Berkeley software License Agreement
3*27700Smckusick .\" specifies the terms and conditions for redistribution.
4*27700Smckusick .\"
5*27700Smckusick .\"	@(#)ustreamread.c	6.1 (Berkeley) 05/04/86
6*27700Smckusick .\"
7*27700Smckusick #include <sys/types.h>
8*27700Smckusick #include <sys/socket.h>
9*27700Smckusick #include <sys/un.h>
10*27700Smckusick #include <stdio.h>
11*27700Smckusick 
12*27700Smckusick #define TRUE 1
13*27700Smckusick #define NAME "socket"
14*27700Smckusick 
15*27700Smckusick /*
16*27700Smckusick  * This program creates a socket in the UNIX domain and binds a name to it.
17*27700Smckusick  * After printing the socket's name it begins a loop. Each time through the
18*27700Smckusick  * loop it accepts a connection and prints out messages from it.  When the
19*27700Smckusick  * connection breaks, or a termination message comes through, the program
20*27700Smckusick  * accepts a new connection.
21*27700Smckusick  */
22*27700Smckusick 
23*27700Smckusick main()
24*27700Smckusick {
25*27700Smckusick 	int             sock;
26*27700Smckusick 	struct sockaddr_un server;
27*27700Smckusick 	int             msgsock;
28*27700Smckusick 	char            buf[1024];
29*27700Smckusick 	int             rval;
30*27700Smckusick 	int             i;
31*27700Smckusick 
32*27700Smckusick 	/* Create socket */
33*27700Smckusick 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
34*27700Smckusick 	if (sock < 0) {
35*27700Smckusick 		perror("opening stream socket");
36*27700Smckusick 		exit(0);
37*27700Smckusick 	}
38*27700Smckusick 	/* Name socket using file system name */
39*27700Smckusick 	server.sun_family = AF_UNIX;
40*27700Smckusick 	strcpy(server.sun_path, NAME);
41*27700Smckusick 	if (bind(sock, &server, sizeof(struct sockaddr_un))) {
42*27700Smckusick 		perror("binding stream socket");
43*27700Smckusick 	}
44*27700Smckusick 	printf("Socket has name %s\en", server.sun_path);
45*27700Smckusick 
46*27700Smckusick 	/* Start accepting connections */
47*27700Smckusick 	listen(sock, 5);
48*27700Smckusick 	do {
49*27700Smckusick 		msgsock = accept(sock, 0, 0);
50*27700Smckusick 		do {
51*27700Smckusick 			for (i = 0; i < 1024; i++)
52*27700Smckusick 				buf[i] = '\e0';
53*27700Smckusick 			if ((rval = read(msgsock, buf, 1024)) < 0)
54*27700Smckusick 				perror("reading stream message");
55*27700Smckusick 			if (rval == 0) {
56*27700Smckusick 				printf("Ending connection\en");
57*27700Smckusick 			} else {
58*27700Smckusick 				printf("-->%s\en", buf);
59*27700Smckusick 			};
60*27700Smckusick 		} while (rval != 0);
61*27700Smckusick 		close(msgsock);
62*27700Smckusick 	} while (TRUE);
63*27700Smckusick 	/*
64*27700Smckusick 	 * The following statements are not executed, because they follow an
65*27700Smckusick 	 * infinite loop.  However, most ordinary programs will not run
66*27700Smckusick 	 * forever.  In the UNIX domain it is necessary to tell the file
67*27700Smckusick 	 * system that one is through using NAME. in most programs one uses
68*27700Smckusick 	 * the call unlink() as below. Since the user will have to kill this
69*27700Smckusick 	 * program, it will be necessary to remove the name by a command from
70*27700Smckusick 	 * the shell.
71*27700Smckusick 	 */
72*27700Smckusick 	unlink(NAME);
73*27700Smckusick 	close(sock);
74*27700Smckusick }
75