#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>

#define TRUE 1

/*
 * Creation: bana, 2007 november, computer networks
 * This program creates a socket and then begins an infinite loop.
 * Each time through the loop it accepts a connection an prints
 * data from it. Whwn the connection breaks, or the client closes
 * the connection, the program accepts a new connection.
 */

void tovabb(char* host, char* port, char buf[1024])
{
  int sock, rval;
  size_t length;
  struct sockaddr_in server;
  struct hostent *hp, *gethostbyname();
  char buff[1024];

  /* Create socket */
  
  sock = socket( AF_INET, SOCK_STREAM, 0 );
  if( sock == -1 ) {
	perror( "opening stream socket" );
	exit(1);
		    }

  /* Connect socket using name specified by command line. */

  server.sin_family = AF_INET;
  hp = gethostbyname( host );

 /*
  * gethostbyname returns a structure including the network address
  * of the specified host.
  */

  if ( hp == (struct hostent *) 0 ) {
	fprintf( stderr, "%s: unknown host \n", host );
	exit(2);
				     }

  memcpy( (void *) &server.sin_addr, (void *) hp->h_addr,
	  hp->h_length );
  server.sin_port = htons( atoi ( port ) );

  if( connect( sock, (struct sockaddr *) &server, sizeof server )
    == -1 ) {
	perror( "connecting stream socket" );
	exit(3);
	     }

  if( send( sock, buf, strlen(buf), 0 ) == -1 )
	perror( "writing on stream socket" );

/*  if(( rval = recv( sock, buff, sizeof buff, 0 )) == -1 )
        perror( "reading stream socket" );
  else { 
	buff[rval] = '\0';
        printf( "-->%s\n", buff );
	}
*/
  shutdown( sock, 1 );

  close( sock );

}

main(int argc, char* argv[])
{
  int sock;
  size_t length;
  struct sockaddr_in server;
  int msgsock;
  char buf[1024];
  int rval;

char *inet_ntoa(struct in_addr in);

  /* Create socket */

  sock = socket( AF_INET, SOCK_STREAM, 0 );
  if( sock == -1 ) {
	perror( "opening stream socket" );
	exit(1);
		    }
 
  /* Bind socket using wildccards. */

  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(atoi(argv[3]));

  if( bind( sock, (struct sockaddr *) &server, sizeof server )
    == -1 ) {
	perror( "binding stream socket" );
	exit(2);
	     }

  /* Find out assigned port number and print it out. */

  length = sizeof server;
  
  if( getsockname( sock, (struct sockaddr *) &server, &length )
    == -1 ) {
	perror( "getting socket name");
	exit(3);
	     }

  printf( "socket port #%d\n", ntohs( server.sin_port ) );

  /* Start accepting connections. */

  listen( sock, 5 );

  do {
	length = sizeof server;
	msgsock = accept( sock, (struct sockaddr *) &server, &length );
	if( msgsock == -1 ) 
		perror( "accept" );

	else

printf( "\nRemote:\n\tsocket port #%d\n", ntohs( server.sin_port ) );
printf( "\tscoket IP   #%s\n", inet_ntoa( server.sin_addr ) );

		memset( (void *)buf, 0, sizeof buf );
		if(( rval = recv( msgsock, buf, 1024, 0 )) == -1 )
			perror( "reading stream socket" );
		else {
			printf( "-->%s\n", buf );
			
			tovabb( argv[1], argv[2], buf );
  		/*	if( send( msgsock, buf, rval, 0 ) == -1 )
 	   	      	  perror( "writing on stream socket" );*/
                }
	     shutdown( msgsock, 1 );

	     close( msgsock ); 
      } while( TRUE );

  /*
   * Since thid program has an infinite loop, the socket "sock" is
   * never explicitly closed. However, all sockets will be closed
   * automatically when a process is killed or terminates normally.
   */
  
  exit(0);

}
