|
|
BothKnockKnockServerKnockKnockClient example use API that have been deprecated in 1.1. Both programs use the same deprecated API so it makes sense to talk about them at the same time.First, the client and the server both use the
DataInputStream.readLinemethod which has been deprecated in the JDK 1.1 because it does not convert correctly between bytes and characters. Most programs that useDataInputStream.readLinecan make a simple change to use the same method from the newBufferedReaderclass instead. Simply replace code of the form:with:DataInputStream d = new DataInputStream(in);Both programs can make this simple change.BufferedReader d = new BufferedReader(new InputStreamReader(in));Second, both the client and the server explicitly create a
PrintStreamfor writing to theSocket. Using aPrintStreamhas been deprecated in favor ofPrintWriter.So, here are new versions of both programs that fix these problems:
import java.net.*; import java.io.*; class KnockKnockServer { public static void main(String[] args) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: " + 4444 + ", " + e); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: " + 4444 + ", " + e); System.exit(1); } try { BufferedReader br = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); PrintWriter pw = new PrintWriter( new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false); KKState kks = new KKState(); String inputLine, outputLine; outputLine = kks.processInput(null); pw.println(outputLine); pw.flush(); while ((inputLine = br.readLine()) != null) { outputLine = kks.processInput(inputLine); pw.println(outputLine); pw.flush(); if (outputLine.equals("Bye.")) break; } pw.close(); br.close(); clientSocket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }import java.io.*; import java.net.*; public class KnockKnockClient { public static void main(String[] args) { Socket kkSocket = null; PrintWriter pw = null; BufferedReader br = null; try { kkSocket = new Socket("taranis", 4444); pw = new PrintWriter(kkSocket.getOutputStream()); br = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: taranis"); } if (kkSocket != null && pw != null && br != null) { try { StringBuffer buf = new StringBuffer(50); int c; String fromServer; while ((fromServer = br.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("Bye.")) break; while ((c = System.in.read()) != '\n') { buf.append((char)c); } System.out.println("Client: " + buf); pw.println(buf.toString()); pw.flush(); buf.setLength(0); } pw.close(); br.close(); kkSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } }KKMultiServerThreadwhich implements a Knock Knock joke server that supports multipleKnockKnockClientconnections uses the exact same deprecated APIs and must be modified in a similar fashion. Here's the new 1.1 version ofKKMultiServerThread.java:import java.net.*; import java.io.*; class KKMultiServerThread extends Thread { Socket socket = null; KKMultiServerThread(Socket socket) { super("KKMultiServerThread"); this.socket = socket; } public void run() { try { BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter pw = new PrintWriter( new BufferedOutputStream(socket.getOutputStream(), 1024), false); KKState kks = new KKState(); String inputLine, outputLine; outputLine = kks.processInput(null); pw.println(outputLine); pw.flush(); while ((inputLine = br.readLine()) != null) { outputLine = kks.processInput(inputLine); pw.println(outputLine); pw.flush(); if (outputLine.equals("Bye")) break; } pw.close(); br.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
|
|