1 .\" Copyright (c) 1986, 1993
2 .\"	The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %sccs.include.redist.roff%
5 .\"
6 .\"	@(#)streamwrite.c	8.1 (Berkeley) 06/08/93
7 .\"
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <stdio.h>
13 
14 #define DATA "Half a league, half a league . . ."
15 
16 /*
17  * This program creates a socket and initiates a connection with the socket
18  * given in the command line.  One message is sent over the connection and
19  * then the socket is closed, ending the connection. The form of the command
20  * line is streamwrite hostname portnumber
21  */
22 
23 main(argc, argv)
24 	int argc;
25 	char *argv[];
26 {
27 	int sock;
28 	struct sockaddr_in server;
29 	struct hostent *hp, *gethostbyname();
30 	char buf[1024];
31 
32 	/* Create socket */
33 	sock = socket(AF_INET, SOCK_STREAM, 0);
34 	if (sock < 0) {
35 		perror("opening stream socket");
36 		exit(1);
37 	}
38 	/* Connect socket using name specified by command line. */
39 	server.sin_family = AF_INET;
40 	hp = gethostbyname(argv[1]);
41 	if (hp == 0) {
42 		fprintf(stderr, "%s: unknown host\n", argv[1]);
43 		exit(2);
44 	}
45 	bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
46 	server.sin_port = htons(atoi(argv[2]));
47 
48 	if (connect(sock, &server, sizeof(server)) < 0) {
49 		perror("connecting stream socket");
50 		exit(1);
51 	}
52 	if (write(sock, DATA, sizeof(DATA)) < 0)
53 		perror("writing on stream socket");
54 	close(sock);
55 }
56