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. 12263142Seadler.\" 3. Neither the name of the University nor the names of its contributors 131638Srgrimes.\" may be used to endorse or promote products derived from this software 141638Srgrimes.\" without specific prior written permission. 151638Srgrimes.\" 161638Srgrimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 171638Srgrimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 181638Srgrimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 191638Srgrimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 201638Srgrimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 211638Srgrimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 221638Srgrimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 231638Srgrimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 241638Srgrimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251638Srgrimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261638Srgrimes.\" SUCH DAMAGE. 271638Srgrimes.\" 281638Srgrimes.\" @(#)streamread.c 8.1 (Berkeley) 6/8/93 291638Srgrimes.\" 301638Srgrimes#include <sys/types.h> 311638Srgrimes#include <sys/socket.h> 321638Srgrimes#include <netinet/in.h> 331638Srgrimes#include <netdb.h> 341638Srgrimes#include <stdio.h> 351638Srgrimes#define TRUE 1 361638Srgrimes 371638Srgrimes/* 381638Srgrimes * This program creates a socket and then begins an infinite loop. Each time 398875Srgrimes * through the loop it accepts a connection and prints out messages from it. 401638Srgrimes * When the connection breaks, or a termination message comes through, the 418875Srgrimes * program accepts a new connection. 421638Srgrimes */ 431638Srgrimes 441638Srgrimesmain() 451638Srgrimes{ 461638Srgrimes int sock, length; 471638Srgrimes struct sockaddr_in server; 481638Srgrimes int msgsock; 491638Srgrimes char buf[1024]; 501638Srgrimes int rval; 511638Srgrimes int i; 521638Srgrimes 531638Srgrimes /* Create socket */ 541638Srgrimes sock = socket(AF_INET, SOCK_STREAM, 0); 551638Srgrimes if (sock < 0) { 561638Srgrimes perror("opening stream socket"); 571638Srgrimes exit(1); 581638Srgrimes } 591638Srgrimes /* Name socket using wildcards */ 601638Srgrimes server.sin_family = AF_INET; 611638Srgrimes server.sin_addr.s_addr = INADDR_ANY; 621638Srgrimes server.sin_port = 0; 631638Srgrimes if (bind(sock, &server, sizeof(server))) { 641638Srgrimes perror("binding stream socket"); 651638Srgrimes exit(1); 661638Srgrimes } 671638Srgrimes /* Find out assigned port number and print it out */ 681638Srgrimes length = sizeof(server); 691638Srgrimes if (getsockname(sock, &server, &length)) { 701638Srgrimes perror("getting socket name"); 711638Srgrimes exit(1); 721638Srgrimes } 731638Srgrimes printf("Socket has port #%d\en", ntohs(server.sin_port)); 741638Srgrimes 751638Srgrimes /* Start accepting connections */ 761638Srgrimes listen(sock, 5); 771638Srgrimes do { 781638Srgrimes msgsock = accept(sock, 0, 0); 791638Srgrimes if (msgsock == -1) 801638Srgrimes perror("accept"); 811638Srgrimes else do { 821638Srgrimes bzero(buf, sizeof(buf)); 831638Srgrimes if ((rval = read(msgsock, buf, 1024)) < 0) 841638Srgrimes perror("reading stream message"); 851638Srgrimes i = 0; 861638Srgrimes if (rval == 0) 871638Srgrimes printf("Ending connection\en"); 881638Srgrimes else 891638Srgrimes printf("-->%s\en", buf); 901638Srgrimes } while (rval != 0); 911638Srgrimes close(msgsock); 921638Srgrimes } while (TRUE); 931638Srgrimes /* 941638Srgrimes * Since this program has an infinite loop, the socket "sock" is 951638Srgrimes * never explicitly closed. However, all sockets will be closed 968875Srgrimes * automatically when a process is killed or terminates normally. 971638Srgrimes */ 981638Srgrimes} 99