Path: blob/main/share/doc/psd/20.ipctut/ustreamwrite.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 DATA "Half a league, half a league . . ."3334/*35* This program connects to the socket named in the command line and sends a36* one line message to that socket. The form of the command line is37* ustreamwrite pathname38*/39main(argc, argv)40int argc;41char *argv[];42{43int sock;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/* Connect socket using name specified by command line. */54server.sun_family = AF_UNIX;55strcpy(server.sun_path, argv[1]);5657if (connect(sock, &server, sizeof(struct sockaddr_un)) < 0) {58close(sock);59perror("connecting stream socket");60exit(1);61}62if (write(sock, DATA, sizeof(DATA)) < 0)63perror("writing on stream socket");64}656667