import java.io.*;
import java.net.*;

/**
 * Example of how to make a very simple java server.
 * Run this as: java ServerListen &lt;port&gt;
 * 
 * The server binds to a port, and accepts connection.
 * It prints out what each client sends, and then
 * closes the connection without responding.
 *
 * @author Rimon Barr
 * @version 1.0, 02/11/99
 */
public class ServerListen {
  public static void main(String args[]) {
    try {
      int port=Integer.valueOf(args[0]).intValue();
      ServerSocket ss=new ServerSocket(port);
      while (true) {
        System.out.println("Listening on port "+port);
        Socket s=ss.accept();
        dump(s.getInputStream(), s.getOutputStream());
        s.close();
      }
    }
    catch (NumberFormatException e) {
      System.err.println("Invalid port number: "+args[0]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.err.println("Must pass in a port number");
    }
    catch (IOException e) {
      System.err.println(e);
    }
  }

  public static void dump(InputStream i, OutputStream o) {
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(i));
      String l = in.readLine();
      while(l!=null) {
        System.out.println(l);
        l=in.readLine();
      }
    }
    catch (IOException e) {}
  }
}
