import java.net.*;
import java.io.*;
import java.util.*;

public class TcpEcho {
  InetAddress server;
  int port = 7;
  InputStream theInput;

  public static void main(String[] args) {
    if (args.length == 0) {
      System.err.println("Usage: java Echo file1 file2...");
      System.exit(1);
    }
    Vector v = new Vector();
    for (int i = 0; i < args.length; i++) {
      try {
        FileInputStream fis = new FileInputStream(args[i]);
        v.addElement(fis);
      }
      catch (IOException e) {
      }
    }
    InputStream in = new SequenceInputStream(v.elements());
    try {
      TcpEcho d = new TcpEcho("smeagol.cm.Deakin.edu.au", in);
      d.start();
    }
    catch (IOException e) {
      System.err.println(e);
    }
  }
  
  public TcpEcho() throws UnknownHostException {
    this (InetAddress.getLocalHost(), System.in);
  }
  
  public TcpEcho(String name) throws UnknownHostException {
    this(InetAddress.getByName(name), System.in);
  }

  public TcpEcho(String name, InputStream is) throws UnknownHostException {
    this(InetAddress.getByName(name), is);
  }

  public TcpEcho(InetAddress server) {
    this(server, System.in);
  }

  public TcpEcho(InetAddress server, InputStream is) {
    this.server = server;
    theInput = is;
  }
  
  public void start() {
    try {
      Socket s = new Socket(server, port);
      CopyThread toServer = new CopyThread("toServer",
        theInput, s.getOutputStream());
      CopyThread fromServer = new CopyThread("fromServer",
        s.getInputStream(), System.out);
      toServer.start();
      fromServer.start();
    }
    catch (IOException e) {
      System.err.println(e);
    }
  }
}

class CopyThread extends Thread {
  InputStream in;
  OutputStream out;
  
  public CopyThread(String name, InputStream in, OutputStream out) {
    super(name);
    this.in = in;
    this.out = out;
  }

  public void run() {
    byte[] b = new byte[128];
    try {
      while (true) {
        int n = in.available();
        if (n == 0) Thread.yield();
        else System.err.println(n);
        if (n > b.length) n = b.length;
        int m = in.read(b, 0, n);
        if (m == -1) {
          System.out.println(getName() + " done!");
          break;
        }
        out.write(b, 0, n);
      }
    }
    catch (IOException e) {
    }
  }
}