new doc makefiles
[unix-history] / usr / src / share / doc / psd / 20.ipctut / strchkread.c
CommitLineData
6b54e9a9
KB
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
19char 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
25static char sccsid[] = "@(#)strchkread.c 6.3 (Berkeley) %G%";
26#endif /* not lint */
27
23237d2e
KM
28#include <sys/types.h>
29#include <sys/socket.h>
a43acb6b 30#include <sys/time.h>
23237d2e
KM
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
41main()
42{
a43acb6b 43 int sock, length;
23237d2e 44 struct sockaddr_in server;
a43acb6b
MK
45 int msgsock;
46 char buf[1024];
47 int rval;
48 fd_set ready;
49 struct timeval to;
23237d2e
KM
50
51 /* Create socket */
52 sock = socket(AF_INET, SOCK_STREAM, 0);
53 if (sock < 0) {
54 perror("opening stream socket");
a43acb6b 55 exit(1);
23237d2e
KM
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");
a43acb6b 63 exit(1);
23237d2e
KM
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");
a43acb6b 69 exit(1);
23237d2e
KM
70 }
71 printf("Socket has port #%d\en", ntohs(server.sin_port));
72
73 /* Start accepting connections */
74 listen(sock, 5);
75 do {
a43acb6b
MK
76 FD_ZERO(&ready);
77 FD_SET(sock, &ready);
23237d2e 78 to.tv_sec = 5;
a43acb6b
MK
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));
23237d2e
KM
89 if ((rval = read(msgsock, buf, 1024)) < 0)
90 perror("reading stream message");
a43acb6b 91 else if (rval == 0)
23237d2e
KM
92 printf("Ending connection\en");
93 else
94 printf("-->%s\en", buf);
a43acb6b 95 } while (rval > 0);
23237d2e
KM
96 close(msgsock);
97 } else
98 printf("Do something else\en");
99 } while (TRUE);
100}