xref: /freebsd/share/doc/psd/20.ipctut/streamwrite.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 .\" Copyright (c) 1986, 1993
2 .\"	The Regents of the University of California.  All rights reserved.
3 .\"
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.
31 .\"
32 .\"	@(#)streamwrite.c	8.1 (Berkeley) 6/8/93
33 .\"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38 #include <stdio.h>
39 
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 
49 main(argc, argv)
50 	int argc;
51 	char *argv[];
52 {
53 	int sock;
54 	struct sockaddr_in server;
55 	struct hostent *hp, *gethostbyname();
56 	char buf[1024];
57 
58 	/* Create socket */
59 	sock = socket(AF_INET, SOCK_STREAM, 0);
60 	if (sock < 0) {
61 		perror("opening stream socket");
62 		exit(1);
63 	}
64 	/* Connect socket using name specified by command line. */
65 	server.sin_family = AF_INET;
66 	hp = gethostbyname(argv[1]);
67 	if (hp == 0) {
68 		fprintf(stderr, "%s: unknown host\en", argv[1]);
69 		exit(2);
70 	}
71 	bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
72 	server.sin_port = htons(atoi(argv[2]));
73 
74 	if (connect(sock, &server, sizeof(server)) < 0) {
75 		perror("connecting stream socket");
76 		exit(1);
77 	}
78 	if (write(sock, DATA, sizeof(DATA)) < 0)
79 		perror("writing on stream socket");
80 	close(sock);
81 }
82