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