Previous


1.1 Changes: Quote Example

Both QuoteClientQuoteServerThread example use API that have been deprecated in 1.1. Each of them use different, but related, deprecated API.

As with many other programs you've seen in this trail and in the I/O trail, QuoteServerThread uses the DataInputStream.readLine method which 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));
QuoteServerThread is one such program that can make this simple change.

QuoteServerThread uses the deprecated String method getBytes and QuoteClient uses the deprecated String constructor String(byte[], int). Both of the method and the constructor convert from bytes to characters incorrectly. Programmers should now use the corresponding method or constructor that uses a character encoding.

The QuoteServerThread uses the new getBytes method that uses the default character encoding and returns a byte array: byte[] getBytes().

import java.io.*;
import java.net.*;
import java.util.*;

class QuoteServerThread extends Thread {
    private DatagramSocket socket = null;
    private BufferedReader qfs = null;

    QuoteServerThread() {
        super("QuoteServer");
        try {
            socket = new DatagramSocket();
            System.out.println("QuoteServer listening on port: " + socket.getLocalPort());
        } catch (java.io.IOException e) {
            System.err.println("Could not create datagram socket.");
        }
        this.openInputFile();
    }

    public void run() {
        if (socket == null)
            return;

        while (true) {
            try {
                byte[] buf = new byte[256];
                DatagramPacket packet;
                InetAddress address;
                int port;
                String dString = null;

                    // receive request
                packet = new DatagramPacket(buf, 256);
                socket.receive(packet);
                address = packet.getAddress();
                port = packet.getPort();

                    // send response
                if (qfs == null)
                    dString = new Date().toString();
                else
                    dString = getNextQuote();
                buf = dString.getBytes();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
                e.printStackTrace();
            }
        }
    }
    protected void finalize() {
        if (socket != null) {
                socket.close();
                socket = null;
                System.out.println("Closing datagram socket.");
        }
    }

    private void openInputFile() {
        try {
            qfs = new BufferedReader(new InputStreamReader(new FileInputStream("one-liners.txt")));
        } catch (java.io.FileNotFoundException e) {
            System.err.println("Could not open quote file. Serving time instead.");
        }
    }
    private String getNextQuote() {
        String returnValue = null;
        try {
            if ((returnValue = qfs.readLine()) == null) {
                qfs.close();
                this.openInputFile();
                returnValue = qfs.readLine();    // we know the file has at least one input line!
            }
        } catch (IOException e) {
            returnValue = "IOException occurred in server.";
        }
        return returnValue;
    }

}

The new 1.1 version of QuoteClient uses the String constructor that takes a byte array and uses the default character encoding: String(byte[]).

import java.io.*;
import java.net.*;
import java.util.*;

class QuoteClient {

    public static void main(String[] args) {

        int port;
        InetAddress address;
        DatagramSocket socket = null;
        DatagramPacket packet;
        byte[] sendBuf = new byte[256];

        if (args.length != 2) {
             System.out.println("Usage: java QuoteClient <hostname> <port#>");
             return;
        }
        try {
                // bind to the socket
            socket = new DatagramSocket();
        } catch (java.io.IOException e) {
            System.err.println("Could not create datagram socket.");
        }

        if (socket != null) {
            try {
                    // send request
                port = Integer.parseInt(args[1]);
                address = InetAddress.getByName(args[0]);
                packet = new DatagramPacket(sendBuf, 256, address, port);
                socket.send(packet);
    
                    // get response
                packet = new DatagramPacket(sendBuf, 256);
                socket.receive(packet);
                String received = new String(packet.getData());
                System.out.println("Quote of the Moment: " + received);
    
                socket.close();
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
                e.printStackTrace();
            }
        }
    }

}


Previous