127700Smckusick .\" Copyright (c) 1986 Regents of the University of California. 227700Smckusick .\" All rights reserved. The Berkeley software License Agreement 327700Smckusick .\" specifies the terms and conditions for redistribution. 427700Smckusick .\" 5*27883Skarels .\" @(#)streamwrite.c 6.2 (Berkeley) 05/08/86 627700Smckusick .\" 727700Smckusick #include <sys/types.h> 827700Smckusick #include <sys/socket.h> 927700Smckusick #include <netinet/in.h> 1027700Smckusick #include <netdb.h> 1127700Smckusick #include <stdio.h> 1227700Smckusick 1327700Smckusick #define DATA "Half a league, half a league . . ." 1427700Smckusick 1527700Smckusick /* 1627700Smckusick * This program creates a socket and initiates a connection with the socket 1727700Smckusick * given in the command line. One message is sent over the connection and 1827700Smckusick * then the socket is closed, ending the connection. The form of the command 1927700Smckusick * line is streamwrite hostname portnumber 2027700Smckusick */ 2127700Smckusick 2227700Smckusick main(argc, argv) 23*27883Skarels int argc; 24*27883Skarels char *argv[]; 2527700Smckusick { 26*27883Skarels int sock; 2727700Smckusick struct sockaddr_in server; 2827700Smckusick struct hostent *hp, *gethostbyname(); 29*27883Skarels char buf[1024]; 3027700Smckusick 3127700Smckusick /* Create socket */ 3227700Smckusick sock = socket(AF_INET, SOCK_STREAM, 0); 3327700Smckusick if (sock < 0) { 3427700Smckusick perror("opening stream socket"); 35*27883Skarels exit(1); 3627700Smckusick } 3727700Smckusick /* Connect socket using name specified by command line. */ 3827700Smckusick server.sin_family = AF_INET; 3927700Smckusick hp = gethostbyname(argv[1]); 40*27883Skarels if (hp == 0) { 41*27883Skarels fprintf(stderr, "%s: unknown host\n", argv[1]); 42*27883Skarels exit(2); 43*27883Skarels } 44*27883Skarels bcopy(hp->h_addr, &server.sin_addr, hp->h_length); 4527700Smckusick server.sin_port = htons(atoi(argv[2])); 4627700Smckusick 4727700Smckusick if (connect(sock, &server, sizeof(server)) < 0) { 4827700Smckusick perror("connecting stream socket"); 49*27883Skarels exit(1); 5027700Smckusick } 5127700Smckusick if (write(sock, DATA, sizeof(DATA)) < 0) 5227700Smckusick perror("writing on stream socket"); 5327700Smckusick close(sock); 5427700Smckusick } 55