// FahrenheitClient.java /** * Routine to send a request to FahrenheitServer to convert temperature * in Celsius to Fahrenheit. * @author R.N. Ciminero * @version 1.0 02-27-2001 */ import java.net.*; import java.io.*; public class FahrenheitClient { public final static int DEFAULT_PORT = 2056; public static void main(String[] args) { String hostname = "localhost"; // Use local host name, if none on the command line if (args.length > 0) { hostname = args[0]; } PrintWriter clientOut = null; BufferedReader serverIn = null; System.out.println("FahrenheitClient"); System.out.print("Temperature: "); try { Socket theSocket = new Socket(hostname, DEFAULT_PORT); // Accept Celsius temperature from console and output to server BufferedReader userIn = new BufferedReader( new InputStreamReader(System.in)); clientOut = new PrintWriter(theSocket.getOutputStream()); String theLine = userIn.readLine(); clientOut.println(theLine); clientOut.flush(); // Input Fahrenheit Server response serverIn = new BufferedReader( new InputStreamReader(theSocket.getInputStream())); String input = " "; input = serverIn.readLine(); // Output Celsius and Fahrenheit temperatures to console while ( input != null ) { System.out.println(input); input = serverIn.readLine(); } } // end try catch (IOException e) { System.err.println(e); } finally { try { if (serverIn != null) serverIn.close(); if (clientOut != null) clientOut.close(); } catch (IOException e) {} } } // end main } // end FahrenheitClient