import java.net.*;
import java.io.*;

/*
 This is a bidirectional network client.
 It takes two or three command line parameters.
 The first parameter is the name of the remote host.
 The second parameter is the port number to use.
 If a third parameter is given, it is the name of an input file.
 If no third parameter is given, input comes from standard input (System.in).

 The program makes a connection to the specified server and in parallel does
 the following two operations:

  1) All data coming from the server is printed (System.out).
  2) The input is sent to the server until an end of file is reached.
     At this point the connection is closed.

 All transfers are done using Copy2Files.

 Note: This is a demonstration program that does extra output to the
       screen to describe what is happening.
 */


public class NetworkClient {

   private static final int BUFSIZE = 1024;

   private boolean success = false;
   private DataInputStream in = null;
   private DataOutputStream out = null;
   private DataInputStream fileIn;
   private Socket sock = null;

   public NetworkClient(String hostName, int port, DataInputStream fileIn) {
      this.fileIn = fileIn;
      try {
         sock = new Socket(hostName, port);
      } catch (IOException e) {}
      if (sock != null) {
         try {
            in = new DataInputStream(sock.getInputStream());
            out = new DataOutputStream(sock.getOutputStream());
            success = true;
         } catch (IOException e) {}
      }
   }

   public boolean getSuccess() {
      return success;
   }

   public void communicate() {
      Copy2Files copy2Files;
      DataOutputStream standardOutput;
      standardOutput = new DataOutputStream(System.out);
      copy2Files = new Copy2Files(in, standardOutput, fileIn, out);
      copy2Files.setAbort1When2Done(true);
      copy2Files.copyAll();
      System.out.println("Bytes transferred: " + copy2Files.getCount1() +
                         " and " + copy2Files.getCount2());
   }

   public static void main(String[] args) {
      int port;
      NetworkClient client;
      DataInputStream fileIn = null;
      if ((args.length != 2) && (args.length != 3)) {
         System.out.println("Usage: java NetworkClient hostname port");
         System.out.println("Usage: java NetworkClient hostname port infile");
         System.exit(1);
      }
      port = Integer.parseInt(args[1]);
      System.out.println("Host is " + args[0] + " and port is " + port);
      if (args.length == 2)
         fileIn = new DataInputStream(System.in);
      else
         try {
            fileIn = new DataInputStream(new FileInputStream(args[2]));
         } catch (IOException e) {}
      if (fileIn == null) {
         System.out.println("Failed to open file " + args[2]);
         System.exit(1);
      }
      client = new NetworkClient(args[0], port, fileIn);

      if (!client.getSuccess()) {
         System.out.println("Failed to make connection to server");
         System.exit(1);
      }
      client.communicate();
      System.out.println("Main done");
   }


}
