/*
*  ECHOCLIENT
* 
*  Copyright (C) 1995 by Sun Microsystems, Inc.
*  All rights reserved.
* 
*  This file is a product of SunSoft, Inc. and is provided for
*  unrestricted use provided that this legend is included on all
*  media and as a part of the software program in whole or part.
*  Users may copy, modify or distribute this file at will.
* 
*  THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
*  THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
*  PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
* 
*  This file is provided with no support and without any obligation on the
*  part of SunSoft, Inc. to assist in its use, correction, modification or
*  enhancement.
* 
*  SUNSOFT AND SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT
*  TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
*  FILE OR ANY PART THEREOF.
* 
*  IN NO EVENT WILL SUNSOFT OR SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
*  LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
*  DAMAGES, EVEN IF THEY HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
*  DAMAGES.
* 
*  SunSoft, Inc.
*  2550 Garcia Avenue
*  Mountain View, California  94043
*/


/*
 * ECHOCLIENT - echo client.
 *
 */

#pragma ident "@(#) echoclient.c 1.3 95/04/27 Richard.Marejka@Canada.Sun.COM"

/*	Include Files	*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

/*
 * Main - 
 *
 */

main( int argc, char *argv[] ) {
	int			sock;
	long			server_addr;
	struct sockaddr_in	saddr;
	struct hostent		*hp;
	char			buf[BUFSIZ];
	char			*hostname;
	unsigned short		port;

	switch ( argc ) {
	  case 1 :
		hostname	= "localhost";
		port		= ECHO_PORT;
		break;

	  case 2 :
		hostname	= argv[1];
		port		= ECHO_PORT;
		break;

	  case 3 :
		hostname	= argv[1];
		port		= atoi( argv[2] );
		break;

	  default:
		fprintf( stderr, "usage: client server port\n" );
		exit( 1 );
	}

	if ( argc != 3 ) {
	}

	if ( !( hp = gethostbyname( hostname ) ) ) {
		perror( "client:gethostbyname" );
		exit( 1 );
	}
	else
		server_addr	= *(u_long *) hp->h_addr;

	if ( ( sock = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
		perror( "client:socket" );
		exit( 1 );
	}

	saddr.sin_port		= htons( port );
	saddr.sin_family	= AF_INET;
	saddr.sin_addr.s_addr	= htonl( server_addr );

	if ( connect( sock, (struct sockaddr *) &saddr, sizeof( struct sockaddr_in ) ) == -1 ) {
		perror( "client:connect" );
		exit( 1 );
	}

	for (;;) {
		char	buf[BUFSIZ];
		int	n;

		if ( isatty( STDIN_FILENO ) )
			printf( "Enter string> " );

		if ( !gets( buf ) )
			break;

		n	= strlen( buf ) + 1;

		if ( write( sock, buf, n ) != n ) {
			perror( "write" );
			exit( 1 );
		}

		n	= read( sock, buf, BUFSIZ );

		if ( n == -1 ) {
			perror( "read" );
			exit( 1 );
		} else if ( n == 0 ) {
			perror( "read(EOF)" );
			break;
		} else
			printf( "%s\n", buf );
	}

	return( 0 );
}
