18549Sjkh.\" Copyright (c) 1986, 1993 28549Sjkh.\" The Regents of the University of California. All rights reserved. 38549Sjkh.\" 48549Sjkh.\" Redistribution and use in source and binary forms, with or without 58549Sjkh.\" modification, are permitted provided that the following conditions 68549Sjkh.\" are met: 729501Sjkh.\" 1. Redistributions of source code must retain the above copyright 88549Sjkh.\" notice, this list of conditions and the following disclaimer. 98549Sjkh.\" 2. Redistributions in binary form must reproduce the above copyright 108549Sjkh.\" notice, this list of conditions and the following disclaimer in the 118549Sjkh.\" documentation and/or other materials provided with the distribution. 128549Sjkh.\" 3. All advertising materials mentioning features or use of this software 138549Sjkh.\" must display the following acknowledgement: 148549Sjkh.\" This product includes software developed by the University of 158549Sjkh.\" California, Berkeley and its contributors. 168881Srgrimes.\" 4. Neither the name of the University nor the names of its contributors 178881Srgrimes.\" may be used to endorse or promote products derived from this software 188549Sjkh.\" without specific prior written permission. 198549Sjkh.\" 208549Sjkh.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 218549Sjkh.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 228549Sjkh.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 238549Sjkh.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 248549Sjkh.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 258549Sjkh.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 268549Sjkh.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 278549Sjkh.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 288549Sjkh.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 298549Sjkh.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 308549Sjkh.\" SUCH DAMAGE. 318549Sjkh.\" 328549Sjkh.\" @(#)streamwrite.c 8.1 (Berkeley) 6/8/93 338549Sjkh.\" 348549Sjkh#include <sys/types.h> 358549Sjkh#include <sys/socket.h> 368549Sjkh#include <netinet/in.h> 378549Sjkh#include <netdb.h> 388549Sjkh#include <stdio.h> 398549Sjkh 4010882Speter#define DATA "Half a league, half a league . . ." 4110882Speter 428549Sjkh/* 438549Sjkh * This program creates a socket and initiates a connection with the socket 448549Sjkh * given in the command line. One message is sent over the connection and 458549Sjkh * then the socket is closed, ending the connection. The form of the command 468549Sjkh * line is streamwrite hostname portnumber 478549Sjkh */ 488549Sjkh 498549Sjkhmain(argc, argv) 508549Sjkh int argc; 518549Sjkh char *argv[]; 528622Sjkh{ 538549Sjkh int sock; 548549Sjkh struct sockaddr_in server; 558702Sjkh struct hostent *hp, *gethostbyname(); 568549Sjkh char buf[1024]; 578672Sjkh 5812661Speter /* Create socket */ 598549Sjkh sock = socket(AF_INET, SOCK_STREAM, 0); 6012661Speter if (sock < 0) { 6112661Speter perror("opening stream socket"); 6212661Speter exit(1); 6312661Speter } 6412661Speter /* Connect socket using name specified by command line. */ 6512661Speter server.sin_family = AF_INET; 6612661Speter hp = gethostbyname(argv[1]); 6712661Speter if (hp == 0) { 6812661Speter fprintf(stderr, "%s: unknown host\en", argv[1]); 6915440Sjkh exit(2); 7029249Sjkh } 7115440Sjkh bcopy(hp->h_addr, &server.sin_addr, hp->h_length); 7215440Sjkh server.sin_port = htons(atoi(argv[2])); 738549Sjkh 748549Sjkh if (connect(sock, &server, sizeof(server)) < 0) { 758549Sjkh perror("connecting stream socket"); 768549Sjkh exit(1); 778549Sjkh } 788549Sjkh if (write(sock, DATA, sizeof(DATA)) < 0) 798549Sjkh perror("writing on stream socket"); 8029249Sjkh close(sock); 8129249Sjkh} 8229249Sjkh