The Java Program: Server.java

  1 // Server.java -- A typical server
  2 
  3 import Connection;       // Connection forked for each clinet
  4 
  5 import java.net.Socket;
  6 import java.net.ServerSocket;
  7 import java.io.IOException;
  8 
  9 public class Server extends Thread {
 10    public final static int DEFAULT_PORT = 56789;
 11 
 12    // Start the server up, listening on an optionally specified port
 13    public static void main (String[] args) {
 14       int port = DEFAULT_PORT;
 15       if (args.length == 1) {
 16          try {
 17             port = Integer.parseInt(args[0]);
 18          } catch (NumberFormatException e) {
 19             e.printStackTrace (System.err);
 20             System.exit(1);
 21          }
 22       }
 23 
 24       try {
 25          final ServerSocket listen_socket = new ServerSocket(port);
 26          System.out.println ("Server: listening on port " + port);
 27          for (;;) {
 28             final Socket client_socket = listen_socket.accept();
 29             final Connection c = new Connection (client_socket);
 30          }
 31       } catch (IOException e) {
 32          e.printStackTrace (System.err);
 33       }
 34 
 35    }
 36 }