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