String message = fromServer.readLine(); toUser.print( message );
// Create the local stream PrintStream toUser = new PrintStream( System.out ); ASCIIInputStream fromUser = new ASCIIInputStream ( System.in );
// Using ASCIIInputStream makes reading input easier PrintStream toUser = new PrintStream( System.out ); ASCIIInputStream fromUser = new ASCIIInputStream ( System.in );
// Create new socket server = new Socket( serverName, portNumber ); InputStream fromServer = server.getInputStream(); OuputStream toServer = server.getOutputStream();
// Connect to server server = new Socket( serverName, portNumber ); InputStream fromServer = server.getInputStream(); OuputStream toServer = server.getOutputStream();
while ( true ) { code deleted to save time }
class CommandLineParser { /** * Returns true if command line contained the given option * Options are single characters * Argument is of form "P" where P could be any character */ public Boolean containsOption( String option ) /** * Returns true if command line contained all the given options * Each character in optionList is treated as a separate option */ public Boolean containsOptions( String optionList) /** * Returns string following given option */ public String get( String option ) }
class FirstClient { static final String hostOption = "H"; static final String portOption = "P"; public static void main( String[] args ) { // Extract options from command line CommandLineParser serverData; serverData = new CommandLineParser( args); if ( ! serverData.containsOptions( hostOption + portOption ) ) properUsageReminder(); String hostName = serverData.get( hostOption ); String portString = serverData.get( portOption ); int port = Integer.parseInt( portString ); // User exits loop via control C while ( true ) { // Connect to server Socket server = new Socket( hostName, port ); stuff not shown } // while } // main private static void properUsageReminder() { System.err.println( "Usage: \n java FirstClient " + portOption + " serverPortNumber " + hostOption + " serverHostName" ); System.exit( 0 ); }