import java.net.*; import java.io.* ; class Server{ public static void main (String args[]) { ServerSocket Rendezvous ; Socket theConnection ; PrintWriter RemoteOutput ; int port = 5193 ; int visits = 0 ; if ( args.length >= 1) { try { port = Integer.parseInt(args[0]) ; } catch (NumberFormatException nfe) { port = -1 ; // sure to fail } } try { Rendezvous = new ServerSocket(port) ; while (true) { try { theConnection = Rendezvous.accept() ; RemoteOutput = new PrintWriter(theConnection.getOutputStream(), true) ; ++visits ; RemoteOutput.println("This server has been contacted " + visits + " times.") ; theConnection.close() ; } catch ( IOException ioe ) { System.out.println("Accept failed") ; return ; } } } catch ( IOException ioe ) { System.out.println("Unable to open socket on port " + port) ; } catch ( IllegalArgumentException iae ) { // inconsistent with docs System.out.println("bad port " + port) ; } } }