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*27884Skarels .\"	@(#)ustreamread.c	6.3 (Berkeley) 05/08/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 {
23*27884Skarels 	int sock, msgsock, rval;
2427700Smckusick 	struct sockaddr_un server;
25*27884Skarels 	char buf[1024];
2627700Smckusick 
2727700Smckusick 	/* Create socket */
2827700Smckusick 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
2927700Smckusick 	if (sock < 0) {
3027700Smckusick 		perror("opening stream socket");
31*27884Skarels 		exit(1);
3227700Smckusick 	}
3327700Smckusick 	/* Name socket using file system name */
3427700Smckusick 	server.sun_family = AF_UNIX;
3527700Smckusick 	strcpy(server.sun_path, NAME);
3627700Smckusick 	if (bind(sock, &server, sizeof(struct sockaddr_un))) {
3727700Smckusick 		perror("binding stream socket");
38*27884Skarels 		exit(1);
3927700Smckusick 	}
4027700Smckusick 	printf("Socket has name %s\en", server.sun_path);
4127700Smckusick 	/* Start accepting connections */
4227700Smckusick 	listen(sock, 5);
4327740Smckusick 	for (;;) {
4427700Smckusick 		msgsock = accept(sock, 0, 0);
45*27884Skarels 		if (msgsock == -1)
46*27884Skarels 			perror("accept");
47*27884Skarels 		else do {
48*27884Skarels 			bzero(buf, sizeof(buf));
4927700Smckusick 			if ((rval = read(msgsock, buf, 1024)) < 0)
5027700Smckusick 				perror("reading stream message");
51*27884Skarels 			else if (rval == 0)
5227700Smckusick 				printf("Ending connection\en");
53*27884Skarels 			else
5427700Smckusick 				printf("-->%s\en", buf);
55*27884Skarels 		} while (rval > 0);
5627700Smckusick 		close(msgsock);
5727740Smckusick 	}
5827700Smckusick 	/*
5927700Smckusick 	 * The following statements are not executed, because they follow an
6027700Smckusick 	 * infinite loop.  However, most ordinary programs will not run
6127700Smckusick 	 * forever.  In the UNIX domain it is necessary to tell the file
62*27884Skarels 	 * system that one is through using NAME.  In most programs one uses
6327700Smckusick 	 * the call unlink() as below. Since the user will have to kill this
6427700Smckusick 	 * program, it will be necessary to remove the name by a command from
6527700Smckusick 	 * the shell.
6627700Smckusick 	 */
67*27884Skarels 	close(sock);
6827700Smckusick 	unlink(NAME);
6927700Smckusick }
70