TheEchoTest
example uses theDataInputStream.readLine
method twice. This mehod has been deprecated in the JDK 1.1 because it does not convert correctly between bytes and characters. Most programs that useDataInputStream.readLine
can make a simple change to use the same method from the newBufferedReader
class instead. Simply replace code of the form:with:DataInputStream d = new DataInputStream(in);BufferedReader d = new BufferedReader(new InputStreamReader(in));EchoTest
is one of those programs which can make this simple change. Here is the new version ofEchoTest
:import java.io.*; import java.net.*; public class EchoTest { public static void main(String[] args) { Socket echoSocket = null; DataOutputStream os = null; BufferedReader br = null; BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); try { echoSocket = new Socket("taranis", 7); os = new DataOutputStream(echoSocket.getOutputStream()); br = new BufferedReader(new InputStreamReader(echoSocket.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 (echoSocket != null && os != null && br != null) { try { String userInput; while ((userInput = stdIn.readLine()) != null) { os.writeBytes(userInput); os.writeByte('\n'); System.out.println("echo: " + br.readLine()); } os.close(); br.close(); echoSocket.close(); } catch (IOException e) { System.err.println("I/O failed on the connection to: taranis"); } } } }