127700Smckusick .\" Copyright (c) 1986 Regents of the University of California.
227700Smckusick .\" All rights reserved.  The Berkeley software License Agreement
327700Smckusick .\" specifies the terms and conditions for redistribution.
427700Smckusick .\"
5*27740Smckusick .\"	@(#)ustreamread.c	6.2 (Berkeley) 05/06/86
627700Smckusick .\"
727700Smckusick #include <sys/types.h>
827700Smckusick #include <sys/socket.h>
927700Smckusick #include <sys/un.h>
1027700Smckusick #include <stdio.h>
1127700Smckusick 
1227700Smckusick #define NAME "socket"
1327700Smckusick 
1427700Smckusick /*
1527700Smckusick  * This program creates a socket in the UNIX domain and binds a name to it.
1627700Smckusick  * After printing the socket's name it begins a loop. Each time through the
1727700Smckusick  * loop it accepts a connection and prints out messages from it.  When the
1827700Smckusick  * connection breaks, or a termination message comes through, the program
1927700Smckusick  * accepts a new connection.
2027700Smckusick  */
2127700Smckusick main()
2227700Smckusick {
2327700Smckusick 	int             sock;
2427700Smckusick 	struct sockaddr_un server;
2527700Smckusick 	int             msgsock;
2627700Smckusick 	char            buf[1024];
27*27740Smckusick 	int             rval, i;
2827700Smckusick 
2927700Smckusick 	/* Create socket */
3027700Smckusick 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
3127700Smckusick 	if (sock < 0) {
3227700Smckusick 		perror("opening stream socket");
3327700Smckusick 		exit(0);
3427700Smckusick 	}
3527700Smckusick 	/* Name socket using file system name */
3627700Smckusick 	server.sun_family = AF_UNIX;
3727700Smckusick 	strcpy(server.sun_path, NAME);
3827700Smckusick 	if (bind(sock, &server, sizeof(struct sockaddr_un))) {
3927700Smckusick 		perror("binding stream socket");
4027700Smckusick 	}
4127700Smckusick 	printf("Socket has name %s\en", server.sun_path);
4227700Smckusick 	/* Start accepting connections */
4327700Smckusick 	listen(sock, 5);
44*27740Smckusick 	for (;;) {
4527700Smckusick 		msgsock = accept(sock, 0, 0);
4627700Smckusick 		do {
4727700Smckusick 			for (i = 0; i < 1024; i++)
4827700Smckusick 				buf[i] = '\e0';
4927700Smckusick 			if ((rval = read(msgsock, buf, 1024)) < 0)
5027700Smckusick 				perror("reading stream message");
5127700Smckusick 			if (rval == 0) {
5227700Smckusick 				printf("Ending connection\en");
5327700Smckusick 			} else {
5427700Smckusick 				printf("-->%s\en", buf);
5527700Smckusick 			};
5627700Smckusick 		} while (rval != 0);
5727700Smckusick 		close(msgsock);
58*27740Smckusick 	}
5927700Smckusick 	/*
6027700Smckusick 	 * The following statements are not executed, because they follow an
6127700Smckusick 	 * infinite loop.  However, most ordinary programs will not run
6227700Smckusick 	 * forever.  In the UNIX domain it is necessary to tell the file
6327700Smckusick 	 * system that one is through using NAME. in most programs one uses
6427700Smckusick 	 * the call unlink() as below. Since the user will have to kill this
6527700Smckusick 	 * program, it will be necessary to remove the name by a command from
6627700Smckusick 	 * the shell.
6727700Smckusick 	 */
6827700Smckusick 	unlink(NAME);
6927700Smckusick 	close(sock);
7027700Smckusick }
71