import java.io.*;
import java.net.*;

/**
 * Example of how to make a very simple java client.
 * Run this as: java ClientListen &lt;host&gt; &lt;port&gt; &lt;filename&gt;
 * 
 * The client dumps the file down the socket, and
 * then prints the server response.
 *
 * @author Rimon Barr
 * @version 1.0, 02/11/99
 */
public class ClientListen {
  public static void main(String args[]) {
    try {
      // get port number from command line arguments
      int port=Integer.valueOf(args[1]).intValue();
      // open a socket to destination specified in command line
      Socket s=new Socket(args[0], port);
      // create a file reader
      BufferedReader fin=new BufferedReader(new FileReader(args[2]));
      String line=fin.readLine();
      // create readers/writers through the socket
      PrintWriter out=new PrintWriter(s.getOutputStream());
      BufferedReader sin=new BufferedReader(new InputStreamReader(s.getInputStream()));
      // dump the file into the socket
      while(line!=null) {
        out.println(line);
        line=fin.readLine();
      }
      out.flush();
      // dump out the response from the socket to stdout
      line=sin.readLine();
      while(line!=null) {
        System.out.println(line);
        line=sin.readLine();
      }
    }
    catch (NumberFormatException e) {
      System.err.println("Invalid port number: "+args[0]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.err.println("Must pass in a hostname, port, filename");
    }
    catch (IOException e) { System.err.println(e); }
  }
}
