| 221 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* File: P3_Client.java
|
|
|
3 |
* Author: Ira W. Snyder (devel@irasnyder.com)
|
|
|
4 |
* License: GNU General Public License v2
|
|
|
5 |
* Class: CS380 - Computer Networking
|
|
|
6 |
*
|
|
|
7 |
* Assignment: Project #3
|
|
|
8 |
* Date Last Modified: 2006-02-15
|
|
|
9 |
*
|
| 260 |
ira |
10 |
* Purpose: Implement a client that will request an IP Address from the
|
|
|
11 |
* P3_Server running on TCP Port 1337. Once it gets a lease, it waits for
|
|
|
12 |
* the server to tell it that the lease has expired.
|
| 221 |
ira |
13 |
******************************************************************************/
|
|
|
14 |
|
| 223 |
ira |
15 |
import java.net.*;
|
|
|
16 |
import java.io.*;
|
|
|
17 |
|
|
|
18 |
public class P3_Client
|
|
|
19 |
{
|
| 224 |
ira |
20 |
/* Instance Variables */
|
| 223 |
ira |
21 |
private static IPAddr ip;
|
|
|
22 |
private static String hw;
|
|
|
23 |
|
|
|
24 |
private static final String hostName = "localhost";
|
|
|
25 |
private static final int portNumber = 1337;
|
|
|
26 |
|
| 224 |
ira |
27 |
/**
|
|
|
28 |
* Method: main
|
|
|
29 |
* Purpose: The main() function, which handles requesting an IP Address,
|
|
|
30 |
* and then closes when the lease expires.
|
|
|
31 |
*/
|
| 223 |
ira |
32 |
public static void main (String[] args) throws Exception
|
|
|
33 |
{
|
|
|
34 |
Socket socket = null;
|
|
|
35 |
PrintWriter out = null;
|
|
|
36 |
BufferedReader in = null;
|
|
|
37 |
BufferedReader kbd = null;
|
|
|
38 |
String s = new String();
|
|
|
39 |
boolean running = true;
|
|
|
40 |
|
| 224 |
ira |
41 |
// Set up the socket, and input and output streams
|
| 223 |
ira |
42 |
try
|
|
|
43 |
{
|
|
|
44 |
socket = new Socket (hostName, portNumber);
|
|
|
45 |
out = new PrintWriter (socket.getOutputStream(), true);
|
|
|
46 |
in = new BufferedReader (new InputStreamReader (
|
|
|
47 |
socket.getInputStream()));
|
|
|
48 |
kbd = new BufferedReader (new InputStreamReader (System.in));
|
|
|
49 |
}
|
|
|
50 |
catch (UnknownHostException e)
|
|
|
51 |
{
|
|
|
52 |
System.err.printf ("Can't resolve hostname: %s\n", hostName);
|
|
|
53 |
System.exit (1);
|
|
|
54 |
}
|
|
|
55 |
catch (IOException e)
|
|
|
56 |
{
|
|
|
57 |
System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);
|
| 260 |
ira |
58 |
System.err.printf ("Is the P3_Server running?\n");
|
| 223 |
ira |
59 |
System.exit (2);
|
|
|
60 |
}
|
|
|
61 |
|
| 224 |
ira |
62 |
// Ask the user for the Hardware Address
|
| 223 |
ira |
63 |
System.out.print ("Please type your Hardware Address: ");
|
|
|
64 |
hw = kbd.readLine();
|
|
|
65 |
|
| 224 |
ira |
66 |
// Send Hardware Address to the server
|
| 223 |
ira |
67 |
out.printf ("req IP %s\n", hw);
|
|
|
68 |
|
| 224 |
ira |
69 |
// Print every message sent back from the server, until the lease expires
|
| 223 |
ira |
70 |
while (running)
|
|
|
71 |
{
|
| 224 |
ira |
72 |
// Get messages from the server
|
| 223 |
ira |
73 |
s = in.readLine();
|
|
|
74 |
System.out.println (s);
|
|
|
75 |
|
| 224 |
ira |
76 |
// Stop when the lease expires
|
| 223 |
ira |
77 |
if (s.equals("IP Lease Time Expired"))
|
|
|
78 |
running = false;
|
|
|
79 |
}
|
|
|
80 |
|
| 224 |
ira |
81 |
// Tell the user that we are quitting
|
|
|
82 |
System.out.println ("Client Exiting");
|
| 223 |
ira |
83 |
|
| 224 |
ira |
84 |
// Clean up all the sockets
|
| 223 |
ira |
85 |
out.close();
|
|
|
86 |
in.close();
|
|
|
87 |
socket.close();
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|