| 1 | .\" Copyright (c) 1986 The Regents of the University of California. |
| 2 | .\" All rights reserved. |
| 3 | .\" |
| 4 | .\" Redistribution and use in source and binary forms, with or without |
| 5 | .\" modification, are permitted provided that the following conditions |
| 6 | .\" are met: |
| 7 | .\" 1. Redistributions of source code must retain the above copyright |
| 8 | .\" notice, this list of conditions and the following disclaimer. |
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 10 | .\" notice, this list of conditions and the following disclaimer in the |
| 11 | .\" documentation and/or other materials provided with the distribution. |
| 12 | .\" 3. All advertising materials mentioning features or use of this software |
| 13 | .\" must display the following acknowledgement: |
| 14 | .\" This product includes software developed by the University of |
| 15 | .\" California, Berkeley and its contributors. |
| 16 | .\" 4. Neither the name of the University nor the names of its contributors |
| 17 | .\" may be used to endorse or promote products derived from this software |
| 18 | .\" without specific prior written permission. |
| 19 | .\" |
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. |
| 31 | .\" |
| 32 | .\" from: @(#)strchkread.c 6.4 (Berkeley) 4/17/91 |
| 33 | .\" strchkread.c,v 1.2 1993/08/01 18:23:29 mycroft Exp |
| 34 | .\" |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <netinet/in.h> |
| 39 | #include <netdb.h> |
| 40 | #include <stdio.h> |
| 41 | #define TRUE 1 |
| 42 | |
| 43 | /* |
| 44 | * This program uses select() to check that someone is trying to connect |
| 45 | * before calling accept(). |
| 46 | */ |
| 47 | |
| 48 | main() |
| 49 | { |
| 50 | int sock, length; |
| 51 | struct sockaddr_in server; |
| 52 | int msgsock; |
| 53 | char buf[1024]; |
| 54 | int rval; |
| 55 | fd_set ready; |
| 56 | struct timeval to; |
| 57 | |
| 58 | /* Create socket */ |
| 59 | sock = socket(AF_INET, SOCK_STREAM, 0); |
| 60 | if (sock < 0) { |
| 61 | perror("opening stream socket"); |
| 62 | exit(1); |
| 63 | } |
| 64 | /* Name socket using wildcards */ |
| 65 | server.sin_family = AF_INET; |
| 66 | server.sin_addr.s_addr = INADDR_ANY; |
| 67 | server.sin_port = 0; |
| 68 | if (bind(sock, &server, sizeof(server))) { |
| 69 | perror("binding stream socket"); |
| 70 | exit(1); |
| 71 | } |
| 72 | /* Find out assigned port number and print it out */ |
| 73 | length = sizeof(server); |
| 74 | if (getsockname(sock, &server, &length)) { |
| 75 | perror("getting socket name"); |
| 76 | exit(1); |
| 77 | } |
| 78 | printf("Socket has port #%d\en", ntohs(server.sin_port)); |
| 79 | |
| 80 | /* Start accepting connections */ |
| 81 | listen(sock, 5); |
| 82 | do { |
| 83 | FD_ZERO(&ready); |
| 84 | FD_SET(sock, &ready); |
| 85 | to.tv_sec = 5; |
| 86 | if (select(sock + 1, &ready, 0, 0, &to) < 0) { |
| 87 | perror("select"); |
| 88 | continue; |
| 89 | } |
| 90 | if (FD_ISSET(sock, &ready)) { |
| 91 | msgsock = accept(sock, (struct sockaddr *)0, (int *)0); |
| 92 | if (msgsock == -1) |
| 93 | perror("accept"); |
| 94 | else do { |
| 95 | bzero(buf, sizeof(buf)); |
| 96 | if ((rval = read(msgsock, buf, 1024)) < 0) |
| 97 | perror("reading stream message"); |
| 98 | else if (rval == 0) |
| 99 | printf("Ending connection\en"); |
| 100 | else |
| 101 | printf("-->%s\en", buf); |
| 102 | } while (rval > 0); |
| 103 | close(msgsock); |
| 104 | } else |
| 105 | printf("Do something else\en"); |
| 106 | } while (TRUE); |
| 107 | } |