Path: blob/main/share/doc/psd/20.ipctut/ustreamread.c
39530 views
.\" Copyright (c) 1986, 19931.\" The Regents of the University of California. All rights reserved.2.\"3.\" Redistribution and use in source and binary forms, with or without4.\" modification, are permitted provided that the following conditions5.\" are met:6.\" 1. Redistributions of source code must retain the above copyright7.\" notice, this list of conditions and the following disclaimer.8.\" 2. Redistributions in binary form must reproduce the above copyright9.\" notice, this list of conditions and the following disclaimer in the10.\" documentation and/or other materials provided with the distribution.11.\" 3. Neither the name of the University nor the names of its contributors12.\" may be used to endorse or promote products derived from this software13.\" without specific prior written permission.14.\"15.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25.\" SUCH DAMAGE.26.\"27#include <sys/types.h>28#include <sys/socket.h>29#include <sys/un.h>30#include <stdio.h>3132#define NAME "socket"3334/*35* This program creates a socket in the UNIX domain and binds a name to it.36* After printing the socket's name it begins a loop. Each time through the37* loop it accepts a connection and prints out messages from it. When the38* connection breaks, or a termination message comes through, the program39* accepts a new connection.40*/41main()42{43int sock, msgsock, rval;44struct sockaddr_un server;45char buf[1024];4647/* Create socket */48sock = socket(AF_UNIX, SOCK_STREAM, 0);49if (sock < 0) {50perror("opening stream socket");51exit(1);52}53/* Name socket using file system name */54server.sun_family = AF_UNIX;55strcpy(server.sun_path, NAME);56if (bind(sock, &server, sizeof(struct sockaddr_un))) {57perror("binding stream socket");58exit(1);59}60printf("Socket has name %s\en", server.sun_path);61/* Start accepting connections */62listen(sock, 5);63for (;;) {64msgsock = accept(sock, 0, 0);65if (msgsock == -1)66perror("accept");67else do {68bzero(buf, sizeof(buf));69if ((rval = read(msgsock, buf, 1024)) < 0)70perror("reading stream message");71else if (rval == 0)72printf("Ending connection\en");73else74printf("-->%s\en", buf);75} while (rval > 0);76close(msgsock);77}78/*79* The following statements are not executed, because they follow an80* infinite loop. However, most ordinary programs will not run81* forever. In the UNIX domain it is necessary to tell the file82* system that one is through using NAME. In most programs one uses83* the call unlink() as below. Since the user will have to kill this84* program, it will be necessary to remove the name by a command from85* the shell.86*/87close(sock);88unlink(NAME);89}909192