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[] = "@(#)strchkread.c	6.3 (Berkeley) 03/07/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <netinet/in.h>
32 #include <netdb.h>
33 #include <stdio.h>
34 #define TRUE 1
35 
36 /*
37  * This program uses select() to check that someone is trying to connect
38  * before calling accept().
39  */
40 
41 main()
42 {
43 	int sock, length;
44 	struct sockaddr_in server;
45 	int msgsock;
46 	char buf[1024];
47 	int rval;
48 	fd_set ready;
49 	struct timeval to;
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 		FD_ZERO(&ready);
77 		FD_SET(sock, &ready);
78 		to.tv_sec = 5;
79 		if (select(sock + 1, &ready, 0, 0, &to) < 0) {
80 			perror("select");
81 			continue;
82 		}
83 		if (FD_ISSET(sock, &ready)) {
84 			msgsock = accept(sock, (struct sockaddr *)0, (int *)0);
85 			if (msgsock == -1)
86 				perror("accept");
87 			else do {
88 				bzero(buf, sizeof(buf));
89 				if ((rval = read(msgsock, buf, 1024)) < 0)
90 					perror("reading stream message");
91 				else if (rval == 0)
92 					printf("Ending connection\en");
93 				else
94 					printf("-->%s\en", buf);
95 			} while (rval > 0);
96 			close(msgsock);
97 		} else
98 			printf("Do something else\en");
99 	} while (TRUE);
100 }
101