1 /*
2  * Copyright (c) 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1986 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)streamread.c	6.3 (Berkeley) 03/07/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #define TRUE 1
34 
35 /*
36  * This program creates a socket and then begins an infinite loop. Each time
37  * through the loop it accepts a connection and prints out messages from it.
38  * When the connection breaks, or a termination message comes through, the
39  * program accepts a new connection.
40  */
41 
42 main()
43 {
44 	int sock, length;
45 	struct sockaddr_in server;
46 	int msgsock;
47 	char buf[1024];
48 	int rval;
49 	int i;
50 
51 	/* Create socket */
52 	sock = socket(AF_INET, SOCK_STREAM, 0);
53 	if (sock < 0) {
54 		perror("opening stream socket");
55 		exit(1);
56 	}
57 	/* Name socket using wildcards */
58 	server.sin_family = AF_INET;
59 	server.sin_addr.s_addr = INADDR_ANY;
60 	server.sin_port = 0;
61 	if (bind(sock, &server, sizeof(server))) {
62 		perror("binding stream socket");
63 		exit(1);
64 	}
65 	/* Find out assigned port number and print it out */
66 	length = sizeof(server);
67 	if (getsockname(sock, &server, &length)) {
68 		perror("getting socket name");
69 		exit(1);
70 	}
71 	printf("Socket has port #%d\en", ntohs(server.sin_port));
72 
73 	/* Start accepting connections */
74 	listen(sock, 5);
75 	do {
76 		msgsock = accept(sock, 0, 0);
77 		if (msgsock == -1)
78 			perror("accept");
79 		else do {
80 			bzero(buf, sizeof(buf));
81 			if ((rval = read(msgsock, buf, 1024)) < 0)
82 				perror("reading stream message");
83 			i = 0;
84 			if (rval == 0)
85 				printf("Ending connection\en");
86 			else
87 				printf("-->%s\en", buf);
88 		} while (rval != 0);
89 		close(msgsock);
90 	} while (TRUE);
91 	/*
92 	 * Since this program has an infinite loop, the socket "sock" is
93 	 * never explicitly closed.  However, all sockets will be closed
94 	 * automatically when a process is killed or terminates normally.
95 	 */
96 }
97