String hostName;
if (argv.length == 0)
hostName = "localhost";
else
hostName = argv[0];
try {
Socket sock = new Socket(hostName, TIME_PORT);
ObjectInputStream is = new ObjectInputStream(new
BufferedInputStream(sock.getInputStream()));
// Read and validate the Object
Object o = is.readObject();
if (o == null) {
System.err.println("Read null from server!");
} else if ((o instanceof Date)) {
// Valid, so cast to Date, and print
Date d = (Date) o;
System.out.println("Server host is " + hostName);
System.out.println("Time there is " + d.toString());
} else {
throw new IllegalArgumentException("Wanted Date, got " + o);
}
} catch (ClassNotFoundException e) {
System.err.println("Wanted date, got INVALID CLASS (" + e + ")");
} catch (IOException e) {
System.err.println(e);
}