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