Rev 260 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/******************************************************************************** File: P3_ServerThread.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 thread that takes an accepted TCP socket, and handles* all of the communication between the DHCP Server and the Client program.* It waits until the DHCP Server expires the lease to tell the client that* the lease has expired, and then exits.******************************************************************************/import java.net.*;import java.io.*;public class P3_ServerThread extends Thread{/* Instance Variables */private Socket socket = null;private DHCPTable table = null;/*** Method: P3_ServerThread constructor* Purpose: Construct a new thread that will handle dealing with a* DHCP client.*/public P3_ServerThread (Socket socket, DHCPTable table){super ("P3_ServerThread");this.socket = socket;this.table = table;}/*** Method: run* Purpose: Communicate with the client*/public void run (){BufferedReader in = null;PrintWriter out = null;// Set up communication to the servertry{out = new PrintWriter (socket.getOutputStream(), true);in = new BufferedReader (new InputStreamReader (socket.getInputStream()));}catch (IOException ex){ex.printStackTrace();}String inputLine, outputLine;DHCPTableEntry e, expired;String[] splitInput = new String[1];// Try to get "req IP <HWAddr>" messagetry{inputLine = in.readLine();splitInput = inputLine.split(" ");}catch (IOException ex){ex.printStackTrace();}// Make sure the request was okif (splitInput.length != 3 &&!splitInput[0].equals("req") &&!splitInput[1].equals("IP")){System.err.println ("REQ was not the first message!");out.println ("BAD REQUEST");return; // Leave uncleanly}// Get the next entry in the DHCPTablee = table.addEntry (splitInput[2]);// Print out DHCP Server infoSystem.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());// Send out the client's IP Addressout.println ("IPAddr: " + e.getIPAddr());// wait for timeout of this entryexpired = table.timeoutEntry (e.getIPAddr());while (expired == null){expired = table.timeoutEntry (e.getIPAddr());}// Print DHCP Server infoSystem.out.printf ("Client Expired: IP=%s -- HW=%s\n",expired.getIPAddr(), expired.getHWAddr());// Tell the client that it expiredout.println ("IP Lease Time Expired");// Try to cleanup socketstry{out.close();in.close();socket.close();}catch (IOException ex){ex.printStackTrace();}}}