1 /*
2  * Copyright (c) 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1986 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)streamwrite.c	6.3 (Berkeley) 03/07/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 
34 #define DATA "Half a league, half a league . . ."
35 
36 /*
37  * This program creates a socket and initiates a connection with the socket
38  * given in the command line.  One message is sent over the connection and
39  * then the socket is closed, ending the connection. The form of the command
40  * line is streamwrite hostname portnumber
41  */
42 
43 main(argc, argv)
44 	int argc;
45 	char *argv[];
46 {
47 	int sock;
48 	struct sockaddr_in server;
49 	struct hostent *hp, *gethostbyname();
50 	char buf[1024];
51 
52 	/* Create socket */
53 	sock = socket(AF_INET, SOCK_STREAM, 0);
54 	if (sock < 0) {
55 		perror("opening stream socket");
56 		exit(1);
57 	}
58 	/* Connect socket using name specified by command line. */
59 	server.sin_family = AF_INET;
60 	hp = gethostbyname(argv[1]);
61 	if (hp == 0) {
62 		fprintf(stderr, "%s: unknown host\n", argv[1]);
63 		exit(2);
64 	}
65 	bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
66 	server.sin_port = htons(atoi(argv[2]));
67 
68 	if (connect(sock, &server, sizeof(server)) < 0) {
69 		perror("connecting stream socket");
70 		exit(1);
71 	}
72 	if (write(sock, DATA, sizeof(DATA)) < 0)
73 		perror("writing on stream socket");
74 	close(sock);
75 }
76