1*62785Sbostic .\" Copyright (c) 1986, 1993 2*62785Sbostic .\" The Regents of the University of California. All rights reserved. 348154Sbostic .\" 448154Sbostic .\" %sccs.include.redist.roff% 548154Sbostic .\" 6*62785Sbostic .\" @(#)ustreamread.c 8.1 (Berkeley) 06/08/93 748154Sbostic .\" 827700Smckusick #include <sys/types.h> 927700Smckusick #include <sys/socket.h> 1027700Smckusick #include <sys/un.h> 1127700Smckusick #include <stdio.h> 1227700Smckusick 1327700Smckusick #define NAME "socket" 1427700Smckusick 1527700Smckusick /* 1627700Smckusick * This program creates a socket in the UNIX domain and binds a name to it. 1727700Smckusick * After printing the socket's name it begins a loop. Each time through the 1827700Smckusick * loop it accepts a connection and prints out messages from it. When the 1927700Smckusick * connection breaks, or a termination message comes through, the program 2027700Smckusick * accepts a new connection. 2127700Smckusick */ 2227700Smckusick main() 2327700Smckusick { 2427884Skarels int sock, msgsock, rval; 2527700Smckusick struct sockaddr_un server; 2627884Skarels char buf[1024]; 2727700Smckusick 2827700Smckusick /* Create socket */ 2927700Smckusick sock = socket(AF_UNIX, SOCK_STREAM, 0); 3027700Smckusick if (sock < 0) { 3127700Smckusick perror("opening stream socket"); 3227884Skarels exit(1); 3327700Smckusick } 3427700Smckusick /* Name socket using file system name */ 3527700Smckusick server.sun_family = AF_UNIX; 3627700Smckusick strcpy(server.sun_path, NAME); 3727700Smckusick if (bind(sock, &server, sizeof(struct sockaddr_un))) { 3827700Smckusick perror("binding stream socket"); 3927884Skarels exit(1); 4027700Smckusick } 4127700Smckusick printf("Socket has name %s\en", server.sun_path); 4227700Smckusick /* Start accepting connections */ 4327700Smckusick listen(sock, 5); 4427740Smckusick for (;;) { 4527700Smckusick msgsock = accept(sock, 0, 0); 4627884Skarels if (msgsock == -1) 4727884Skarels perror("accept"); 4827884Skarels else do { 4927884Skarels bzero(buf, sizeof(buf)); 5027700Smckusick if ((rval = read(msgsock, buf, 1024)) < 0) 5127700Smckusick perror("reading stream message"); 5227884Skarels else if (rval == 0) 5327700Smckusick printf("Ending connection\en"); 5427884Skarels else 5527700Smckusick printf("-->%s\en", buf); 5627884Skarels } while (rval > 0); 5727700Smckusick close(msgsock); 5827740Smckusick } 5927700Smckusick /* 6027700Smckusick * The following statements are not executed, because they follow an 6127700Smckusick * infinite loop. However, most ordinary programs will not run 6227700Smckusick * forever. In the UNIX domain it is necessary to tell the file 6327884Skarels * system that one is through using NAME. In most programs one uses 6427700Smckusick * the call unlink() as below. Since the user will have to kill this 6527700Smckusick * program, it will be necessary to remove the name by a command from 6627700Smckusick * the shell. 6727700Smckusick */ 6827884Skarels close(sock); 6927700Smckusick unlink(NAME); 7027700Smckusick } 71