// CS 1520 Summer 2011 Java Example 4 // This simple client program will connect to the simple server (from // program JRServer.java) and get the current date/time. import java.net.*; import java.io.*; public class ex4 { //Choose a port outside of the range 1-1024 (but same as server): static final int port = 8800; public static void main( String [] args) { try { // Find IP address of host machine. It can either be as a name, // such as: InetAddress addr = InetAddress.getByName("java2b.javalab.cs.pitt.edu"); // or it can be as a raw address, such as: //InetAddress addr = // InetAddress.getByName("130.49.223.208"); // If we are not on the network, we can use localhost // instead, also in two ways: //InetAddress addr = // InetAddress.getByName("localhost"); // or //InetAddress addr = // InetAddress.getLocalHost(); System.out.println("Server address = " + addr); Socket socket = new Socket(addr, port); System.out.println(" Connected to socket: " + socket); // Use a BufferedReader on the socket's InputStream to read the // string sent by the server BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); System.out.print("Receiving date: "); String str = in.readLine(); // Input above corresponds to a println in the JRServer // program. The idea is that output onto one end of the // socket is input to the other end System.out.println(str); // Close socket when finished socket.close(); } catch( Exception e) { System.out.println("Some wacky error!"); e.printStackTrace(); } } }