// FahrenheitServer.java /** * Routine to accept a request from FahrenheitClient 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 FahrenheitServer { public final static int DEFAULT_PORT = 2056; public static void main(String[] args) { try { ServerSocket server = new ServerSocket(DEFAULT_PORT); Socket connection = null; BufferedReader clientIn = null; while (true) { try { connection = server.accept(); // accept connections // Input client request and convert to a double clientIn = new BufferedReader( new InputStreamReader(connection.getInputStream())); String input = " "; input = clientIn.readLine(); Double theDouble = new Double(input); double celsius = theDouble.doubleValue(); double fahrenheit = 1.80 * celsius + 32.0; // compute Fahrernheit // Output Celsius and Fahrenheit to client OutputStreamWriter serverOut = new OutputStreamWriter(connection.getOutputStream()); serverOut.write("Celsius: " + celsius + "\r\n"); serverOut.write("Fahrenheit: " + fahrenheit + "\r\n"); serverOut.flush(); connection.close(); } catch (IOException e) {} finally { try { if (connection != null) connection.close(); } catch (IOException e) {} } } } // end try catch (IOException e) { System.err.println(e); } // end catch } // end main } // end FahrenheitServer