Rev 224 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/******************************************************************************** File: P3_Client.java* Author: Ira W. Snyder (devel@irasnyder.com)* License: GNU General Public License v2* Class: CS380 - Computer Networking** Assignment: Project #3* Date Last Modified: 2006-02-15** Purpose: Implement a client that will request an IP Address from the* P3_Server running on TCP Port 1337. Once it gets a lease, it waits for* the server to tell it that the lease has expired.******************************************************************************/import java.net.*;import java.io.*;public class P3_Client{/* Instance Variables */private static IPAddr ip;private static String hw;private static final String hostName = "localhost";private static final int portNumber = 1337;/*** Method: main* Purpose: The main() function, which handles requesting an IP Address,* and then closes when the lease expires.*/public static void main (String[] args) throws Exception{Socket socket = null;PrintWriter out = null;BufferedReader in = null;BufferedReader kbd = null;String s = new String();boolean running = true;// Set up the socket, and input and output streamstry{socket = new Socket (hostName, portNumber);out = new PrintWriter (socket.getOutputStream(), true);in = new BufferedReader (new InputStreamReader (socket.getInputStream()));kbd = new BufferedReader (new InputStreamReader (System.in));}catch (UnknownHostException e){System.err.printf ("Can't resolve hostname: %s\n", hostName);System.exit (1);}catch (IOException e){System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);System.err.printf ("Is the P3_Server running?\n");System.exit (2);}// Ask the user for the Hardware AddressSystem.out.print ("Please type your Hardware Address: ");hw = kbd.readLine();// Send Hardware Address to the serverout.printf ("req IP %s\n", hw);// Print every message sent back from the server, until the lease expireswhile (running){// Get messages from the servers = in.readLine();System.out.println (s);// Stop when the lease expiresif (s.equals("IP Lease Time Expired"))running = false;}// Tell the user that we are quittingSystem.out.println ("Client Exiting");// Clean up all the socketsout.close();in.close();socket.close();}}