1*62785Sbostic .\" Copyright (c) 1986, 1993
2*62785Sbostic .\"	The Regents of the University of California.  All rights reserved.
348154Sbostic .\"
448154Sbostic .\" %sccs.include.redist.roff%
548154Sbostic .\"
6*62785Sbostic .\"	@(#)streamwrite.c	8.1 (Berkeley) 06/08/93
748154Sbostic .\"
827700Smckusick #include <sys/types.h>
927700Smckusick #include <sys/socket.h>
1027700Smckusick #include <netinet/in.h>
1127700Smckusick #include <netdb.h>
1227700Smckusick #include <stdio.h>
1327700Smckusick 
1427700Smckusick #define DATA "Half a league, half a league . . ."
1527700Smckusick 
1627700Smckusick /*
1727700Smckusick  * This program creates a socket and initiates a connection with the socket
1827700Smckusick  * given in the command line.  One message is sent over the connection and
1927700Smckusick  * then the socket is closed, ending the connection. The form of the command
2027700Smckusick  * line is streamwrite hostname portnumber
2127700Smckusick  */
2227700Smckusick 
2327700Smckusick main(argc, argv)
2427883Skarels 	int argc;
2527883Skarels 	char *argv[];
2627700Smckusick {
2727883Skarels 	int sock;
2827700Smckusick 	struct sockaddr_in server;
2927700Smckusick 	struct hostent *hp, *gethostbyname();
3027883Skarels 	char buf[1024];
3127700Smckusick 
3227700Smckusick 	/* Create socket */
3327700Smckusick 	sock = socket(AF_INET, SOCK_STREAM, 0);
3427700Smckusick 	if (sock < 0) {
3527700Smckusick 		perror("opening stream socket");
3627883Skarels 		exit(1);
3727700Smckusick 	}
3827700Smckusick 	/* Connect socket using name specified by command line. */
3927700Smckusick 	server.sin_family = AF_INET;
4027700Smckusick 	hp = gethostbyname(argv[1]);
4127883Skarels 	if (hp == 0) {
4227883Skarels 		fprintf(stderr, "%s: unknown host\n", argv[1]);
4327883Skarels 		exit(2);
4427883Skarels 	}
4527883Skarels 	bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
4627700Smckusick 	server.sin_port = htons(atoi(argv[2]));
4727700Smckusick 
4827700Smckusick 	if (connect(sock, &server, sizeof(server)) < 0) {
4927700Smckusick 		perror("connecting stream socket");
5027883Skarels 		exit(1);
5127700Smckusick 	}
5227700Smckusick 	if (write(sock, DATA, sizeof(DATA)) < 0)
5327700Smckusick 		perror("writing on stream socket");
5427700Smckusick 	close(sock);
5527700Smckusick }
56