/* * To start the Client in console mode use one of the following command * > java Client * > java Client username * > java Client username portNumber * > java Client username portNumber serverAddress * at the console prompt * If the portNumber is not specified 1500 is used * If the serverAddress is not specified "localHost" is used * If the username is not specified "Anonymous" is used * > java Client * is equivalent to * > java Client Anonymous 1500 localhost * are equivalent * * In console mode, if an error occurs the program simply stops * when a GUI id used, the GUI is informed of the disconnection */ public static void main(String[] args) { int portNumber = 1500; String serverAddress = "localhost"; String userName = "******"; switch (args.length) { // > javac Client username portNumber serverAddr case 3: serverAddress = args[2]; // > javac Client username portNumber case 2: try { portNumber = Integer.parseInt(args[1]); } catch (Exception e) { System.out.println("Invalid port number."); System.out.println("Usage is: > java Client [username] [portNumber] [serverAddress]"); return; } // > javac Client username case 1: userName = args[0]; // > java Client case 0: break; // invalid number of arguments default: System.out.println("Usage is: > java Client [username] [portNumber] {serverAddress]"); return; } Client client = new Client(serverAddress, portNumber, userName); if (!client.start()) { return; } Scanner scan = new Scanner(System.in); while (true) { System.out.print("> "); String msg = scan.nextLine(); if (msg.equalsIgnoreCase("LOGOUT")) { client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, "")); break; } else if (msg.equalsIgnoreCase("WHOISIN")) { client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, "")); } else { client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, msg)); } } client.disconnect(); }