Previous


1.1 Changes: EchoTest Example

The EchoTest example uses the DataInputStream.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 use DataInputStream.readLine can make a simple change to use the same method from the new BufferedReader class instead. Simply replace code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d = new BufferedReader(new InputStreamReader(in));
EchoTest is one of those programs which can make this simple change. Here is the new version of EchoTest:
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");
            }
        }
    }
}


Previous