11638Srgrimes.\" Copyright (c) 1986, 1993 21638Srgrimes.\" The Regents of the University of California. All rights reserved. 31638Srgrimes.\" 41638Srgrimes.\" Redistribution and use in source and binary forms, with or without 51638Srgrimes.\" modification, are permitted provided that the following conditions 61638Srgrimes.\" are met: 71638Srgrimes.\" 1. Redistributions of source code must retain the above copyright 81638Srgrimes.\" notice, this list of conditions and the following disclaimer. 91638Srgrimes.\" 2. Redistributions in binary form must reproduce the above copyright 101638Srgrimes.\" notice, this list of conditions and the following disclaimer in the 111638Srgrimes.\" documentation and/or other materials provided with the distribution. 121638Srgrimes.\" 3. All advertising materials mentioning features or use of this software 131638Srgrimes.\" must display the following acknowledgement: 141638Srgrimes.\" This product includes software developed by the University of 151638Srgrimes.\" California, Berkeley and its contributors. 161638Srgrimes.\" 4. Neither the name of the University nor the names of its contributors 171638Srgrimes.\" may be used to endorse or promote products derived from this software 181638Srgrimes.\" without specific prior written permission. 191638Srgrimes.\" 201638Srgrimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211638Srgrimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221638Srgrimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231638Srgrimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241638Srgrimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251638Srgrimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261638Srgrimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271638Srgrimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281638Srgrimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291638Srgrimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301638Srgrimes.\" SUCH DAMAGE. 311638Srgrimes.\" 321638Srgrimes.\" @(#)streamread.c 8.1 (Berkeley) 6/8/93 331638Srgrimes.\" 341638Srgrimes#include <sys/types.h> 351638Srgrimes#include <sys/socket.h> 361638Srgrimes#include <netinet/in.h> 371638Srgrimes#include <netdb.h> 381638Srgrimes#include <stdio.h> 391638Srgrimes#define TRUE 1 401638Srgrimes 411638Srgrimes/* 421638Srgrimes * This program creates a socket and then begins an infinite loop. Each time 438875Srgrimes * through the loop it accepts a connection and prints out messages from it. 441638Srgrimes * When the connection breaks, or a termination message comes through, the 458875Srgrimes * program accepts a new connection. 461638Srgrimes */ 471638Srgrimes 481638Srgrimesmain() 491638Srgrimes{ 501638Srgrimes int sock, length; 511638Srgrimes struct sockaddr_in server; 521638Srgrimes int msgsock; 531638Srgrimes char buf[1024]; 541638Srgrimes int rval; 551638Srgrimes int i; 561638Srgrimes 571638Srgrimes /* Create socket */ 581638Srgrimes sock = socket(AF_INET, SOCK_STREAM, 0); 591638Srgrimes if (sock < 0) { 601638Srgrimes perror("opening stream socket"); 611638Srgrimes exit(1); 621638Srgrimes } 631638Srgrimes /* Name socket using wildcards */ 641638Srgrimes server.sin_family = AF_INET; 651638Srgrimes server.sin_addr.s_addr = INADDR_ANY; 661638Srgrimes server.sin_port = 0; 671638Srgrimes if (bind(sock, &server, sizeof(server))) { 681638Srgrimes perror("binding stream socket"); 691638Srgrimes exit(1); 701638Srgrimes } 711638Srgrimes /* Find out assigned port number and print it out */ 721638Srgrimes length = sizeof(server); 731638Srgrimes if (getsockname(sock, &server, &length)) { 741638Srgrimes perror("getting socket name"); 751638Srgrimes exit(1); 761638Srgrimes } 771638Srgrimes printf("Socket has port #%d\en", ntohs(server.sin_port)); 781638Srgrimes 791638Srgrimes /* Start accepting connections */ 801638Srgrimes listen(sock, 5); 811638Srgrimes do { 821638Srgrimes msgsock = accept(sock, 0, 0); 831638Srgrimes if (msgsock == -1) 841638Srgrimes perror("accept"); 851638Srgrimes else do { 861638Srgrimes bzero(buf, sizeof(buf)); 871638Srgrimes if ((rval = read(msgsock, buf, 1024)) < 0) 881638Srgrimes perror("reading stream message"); 891638Srgrimes i = 0; 901638Srgrimes if (rval == 0) 911638Srgrimes printf("Ending connection\en"); 921638Srgrimes else 931638Srgrimes printf("-->%s\en", buf); 941638Srgrimes } while (rval != 0); 951638Srgrimes close(msgsock); 961638Srgrimes } while (TRUE); 971638Srgrimes /* 981638Srgrimes * Since this program has an infinite loop, the socket "sock" is 991638Srgrimes * never explicitly closed. However, all sockets will be closed 1008875Srgrimes * automatically when a process is killed or terminates normally. 1011638Srgrimes */ 1021638Srgrimes} 103