Path: blob/main/share/doc/psd/20.ipctut/streamread.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 <netinet/in.h>30#include <netdb.h>31#include <stdio.h>32#define TRUE 13334/*35* This program creates a socket and then begins an infinite loop. Each time36* through the loop it accepts a connection and prints out messages from it.37* When the connection breaks, or a termination message comes through, the38* program accepts a new connection.39*/4041main()42{43int sock, length;44struct sockaddr_in server;45int msgsock;46char buf[1024];47int rval;48int i;4950/* Create socket */51sock = socket(AF_INET, SOCK_STREAM, 0);52if (sock < 0) {53perror("opening stream socket");54exit(1);55}56/* Name socket using wildcards */57server.sin_family = AF_INET;58server.sin_addr.s_addr = INADDR_ANY;59server.sin_port = 0;60if (bind(sock, &server, sizeof(server))) {61perror("binding stream socket");62exit(1);63}64/* Find out assigned port number and print it out */65length = sizeof(server);66if (getsockname(sock, &server, &length)) {67perror("getting socket name");68exit(1);69}70printf("Socket has port #%d\en", ntohs(server.sin_port));7172/* Start accepting connections */73listen(sock, 5);74do {75msgsock = accept(sock, 0, 0);76if (msgsock == -1)77perror("accept");78else do {79bzero(buf, sizeof(buf));80if ((rval = read(msgsock, buf, 1024)) < 0)81perror("reading stream message");82i = 0;83if (rval == 0)84printf("Ending connection\en");85else86printf("-->%s\en", buf);87} while (rval != 0);88close(msgsock);89} while (TRUE);90/*91* Since this program has an infinite loop, the socket "sock" is92* never explicitly closed. However, all sockets will be closed93* automatically when a process is killed or terminates normally.94*/95}969798