/*****************************************************************************
  TCPtt_c: TCP Typewriter Example Client

  Copyright (C) 1996, Michael J. Andrews
  ALL RIGHTS RESERVED
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include "netinfo.h"

void main(int argc, char *argv[])
{
  int                 fdSocket;      /* Holds the socket file descriptor
					number for our connection to the
					Server */ 
  struct sockaddr_in  AddrServ;      /* Address information structure for
					the Server */
  struct hostent      *hostdata;     /* Will point to data returned by a
					gethostbyname() call */
  char                buffer[255];   /* A character buffer */

                         /* Print out a header */
  printf("\nTCP Typewriter Client v1.0\n");
  printf("Connecting to host %s...\n", argv[1]);

                         /* Fill out address structure for server */

  bzero((char *)&AddrServ, sizeof(AddrServ)); /* Erase anything that
		                                 might be hanging out
						 in our address
						 structure */
  AddrServ.sin_family=AF_INET;         /* AF_INET is the Internet
					  Protocol Family.  */
  hostdata=gethostbyname(argv[1]);     /* Given any DNS or /etc/hosts
					  registered hostname,
					  gethostbyname() will return
					  useful information,
					  including the 32-bit IP that
					  we need */
  if (hostdata==NULL)
    {
      perror("TCPtt_c - trying to resolve hostname");
      exit(1);
    }                                  /* Make sure our hostname is valid */

  AddrServ.sin_addr.s_addr=((struct in_addr
			     *)hostdata->h_addr_list[0])->s_addr; 
                                       /* Sets the IP address of the
					  server to the 32-bit IP
					  associated with the hostname
					  of the server. */
  AddrServ.sin_port=htons(PORT_SERVER); /* The port number on the IP
					   that the server is bound
					   to. */

  if((fdSocket=socket(AF_INET, SOCK_STREAM, 0)) < 0)
    perror("TCPtt_c - Trying to open socket"); 
                     /* Open our socket, selecting the Internet
			Protocol family.  SOCK_STREAM initializes the
			socket as a "stream", which in the case of the
			Internet Protocol family is TCP. */

  if(connect(fdSocket, (struct sockaddr *)&AddrServ, sizeof(AddrServ)) <0)
    perror("TCPtt_c - Trying to connect to server");
                     /* Using our opened socket, we try to connect to
			the server using our "filled out" AddrServ
			structure */
  
  /******************** MAIN LOOP ***********************************/

  printf("\nType lines ended by an enter that will be sent to the server:\n\n");
  while(1)
    {
      gets(buffer);  /* Get a indefinite-length string, terminated by
			a newline character.  The newline is replaced
			with a NULL. */
      write(fdSocket, buffer, strlen(buffer)+1); /* Write the string
						    out to our
						    connection stream,
						    including the NULL
						    */
      if (!strcmp(buffer, "quit")) break;        /* If we get "quit",
						    blow out */
    }

  printf("\nThank you for using TCP Typewriter!\n");
  close(fdSocket);
  exit(0);
}




