BSD 4_3_Net_2 release
[unix-history] / usr / src / share / doc / ps1 / 07.ipctut / streamwrite.c
CommitLineData
af359dea
C
1.\" Copyright (c) 1986 The Regents of the University of California.
2.\" All rights reserved.
3.\"
c0567266
KB
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
af359dea
C
31.\"
32.\" @(#)streamwrite.c 6.4 (Berkeley) 4/17/91
33.\"
23237d2e
KM
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <netdb.h>
38#include <stdio.h>
39
23237d2e
KM
40#define DATA "Half a league, half a league . . ."
41
42/*
43 * This program creates a socket and initiates a connection with the socket
44 * given in the command line. One message is sent over the connection and
45 * then the socket is closed, ending the connection. The form of the command
46 * line is streamwrite hostname portnumber
47 */
48
49main(argc, argv)
a43acb6b
MK
50 int argc;
51 char *argv[];
23237d2e 52{
a43acb6b 53 int sock;
23237d2e
KM
54 struct sockaddr_in server;
55 struct hostent *hp, *gethostbyname();
a43acb6b 56 char buf[1024];
23237d2e
KM
57
58 /* Create socket */
59 sock = socket(AF_INET, SOCK_STREAM, 0);
60 if (sock < 0) {
61 perror("opening stream socket");
a43acb6b 62 exit(1);
23237d2e
KM
63 }
64 /* Connect socket using name specified by command line. */
65 server.sin_family = AF_INET;
66 hp = gethostbyname(argv[1]);
a43acb6b
MK
67 if (hp == 0) {
68 fprintf(stderr, "%s: unknown host\n", argv[1]);
69 exit(2);
70 }
71 bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
23237d2e
KM
72 server.sin_port = htons(atoi(argv[2]));
73
74 if (connect(sock, &server, sizeof(server)) < 0) {
23237d2e 75 perror("connecting stream socket");
a43acb6b 76 exit(1);
23237d2e
KM
77 }
78 if (write(sock, DATA, sizeof(DATA)) < 0)
79 perror("writing on stream socket");
80 close(sock);
81}