.\" Copyright (c
) 1986 Regents of the University of California
.
.\" All rights reserved
. The Berkeley software License Agreement
.\" specifies the terms
and conditions
for redistribution
.
.\" @
(#)streamwrite.c 6.2 (Berkeley) 5/8/86
#define DATA "Half a league, half a league . . ."
* This program creates a socket and initiates a connection with the socket
* given in the command line. One message is sent over the connection and
* then the socket is closed, ending the connection. The form of the command
* line is streamwrite hostname portnumber
struct sockaddr_in server
;
struct hostent
*hp
, *gethostbyname();
sock
= socket(AF_INET
, SOCK_STREAM
, 0);
perror("opening stream socket");
/* Connect socket using name specified by command line. */
server
.sin_family
= AF_INET
;
hp
= gethostbyname(argv
[1]);
fprintf(stderr
, "%s: unknown host\n", argv
[1]);
bcopy(hp
->h_addr
, &server
.sin_addr
, hp
->h_length
);
server
.sin_port
= htons(atoi(argv
[2]));
if (connect(sock
, &server
, sizeof(server
)) < 0) {
perror("connecting stream socket");
if (write(sock
, DATA
, sizeof(DATA
)) < 0)
perror("writing on stream socket");