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.\" @(#)ustreamread.c 8.1 (Berkeley) 6/8/93 331638Srgrimes.\" 341638Srgrimes#include <sys/types.h> 351638Srgrimes#include <sys/socket.h> 361638Srgrimes#include <sys/un.h> 371638Srgrimes#include <stdio.h> 381638Srgrimes 391638Srgrimes#define NAME "socket" 401638Srgrimes 411638Srgrimes/* 428875Srgrimes * This program creates a socket in the UNIX domain and binds a name to it. 431638Srgrimes * After printing the socket's name it begins a loop. Each time through the 441638Srgrimes * loop it accepts a connection and prints out messages from it. When the 451638Srgrimes * connection breaks, or a termination message comes through, the program 468875Srgrimes * accepts a new connection. 471638Srgrimes */ 481638Srgrimesmain() 491638Srgrimes{ 501638Srgrimes int sock, msgsock, rval; 511638Srgrimes struct sockaddr_un server; 521638Srgrimes char buf[1024]; 531638Srgrimes 541638Srgrimes /* Create socket */ 551638Srgrimes sock = socket(AF_UNIX, SOCK_STREAM, 0); 561638Srgrimes if (sock < 0) { 571638Srgrimes perror("opening stream socket"); 581638Srgrimes exit(1); 591638Srgrimes } 601638Srgrimes /* Name socket using file system name */ 611638Srgrimes server.sun_family = AF_UNIX; 621638Srgrimes strcpy(server.sun_path, NAME); 631638Srgrimes if (bind(sock, &server, sizeof(struct sockaddr_un))) { 641638Srgrimes perror("binding stream socket"); 651638Srgrimes exit(1); 661638Srgrimes } 671638Srgrimes printf("Socket has name %s\en", server.sun_path); 681638Srgrimes /* Start accepting connections */ 691638Srgrimes listen(sock, 5); 701638Srgrimes for (;;) { 711638Srgrimes msgsock = accept(sock, 0, 0); 721638Srgrimes if (msgsock == -1) 731638Srgrimes perror("accept"); 741638Srgrimes else do { 751638Srgrimes bzero(buf, sizeof(buf)); 761638Srgrimes if ((rval = read(msgsock, buf, 1024)) < 0) 771638Srgrimes perror("reading stream message"); 781638Srgrimes else if (rval == 0) 791638Srgrimes printf("Ending connection\en"); 801638Srgrimes else 811638Srgrimes printf("-->%s\en", buf); 821638Srgrimes } while (rval > 0); 831638Srgrimes close(msgsock); 841638Srgrimes } 851638Srgrimes /* 861638Srgrimes * The following statements are not executed, because they follow an 871638Srgrimes * infinite loop. However, most ordinary programs will not run 881638Srgrimes * forever. In the UNIX domain it is necessary to tell the file 891638Srgrimes * system that one is through using NAME. In most programs one uses 901638Srgrimes * the call unlink() as below. Since the user will have to kill this 911638Srgrimes * program, it will be necessary to remove the name by a command from 928875Srgrimes * the shell. 931638Srgrimes */ 941638Srgrimes close(sock); 951638Srgrimes unlink(NAME); 961638Srgrimes} 97