Subversion Repositories programming

Rev

Rev 223 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 *
10
 * Purpose: //FIXME
11
 ******************************************************************************/
12
 
223 ira 13
import java.net.*;
14
import java.io.*;
15
 
16
public class P3_Client
17
{
224 ira 18
    /* Instance Variables */
223 ira 19
    private static IPAddr ip;
20
    private static String hw;
21
 
22
    private static final String hostName = "localhost";
23
    private static final int portNumber = 1337;
24
 
224 ira 25
    /**
26
     * Method: main
27
     * Purpose: The main() function, which handles requesting an IP Address,
28
     * and then closes when the lease expires.
29
     */
223 ira 30
    public static void main (String[] args) throws Exception
31
    {
32
        Socket socket = null;
33
        PrintWriter out = null;
34
        BufferedReader in = null;
35
        BufferedReader kbd = null;
36
        String s = new String();
37
        boolean running = true;
38
 
224 ira 39
        // Set up the socket, and input and output streams
223 ira 40
        try
41
        {
42
            socket = new Socket (hostName, portNumber);
43
            out = new PrintWriter (socket.getOutputStream(), true);
44
            in = new BufferedReader (new InputStreamReader (
45
                                         socket.getInputStream()));
46
            kbd = new BufferedReader (new InputStreamReader (System.in));
47
        }
48
        catch (UnknownHostException e)
49
        {
50
            System.err.printf ("Can't resolve hostname: %s\n", hostName);
51
            System.exit (1);
52
        }
53
        catch (IOException e)
54
        {
55
            System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);
56
            System.exit (2);
57
        }
58
 
224 ira 59
        // Ask the user for the Hardware Address
223 ira 60
        System.out.print ("Please type your Hardware Address: ");
61
        hw = kbd.readLine();
62
 
224 ira 63
        // Send Hardware Address to the server
223 ira 64
        out.printf ("req IP %s\n", hw);
65
 
224 ira 66
        // Print every message sent back from the server, until the lease expires
223 ira 67
        while (running)
68
        {
224 ira 69
            // Get messages from the server
223 ira 70
            s = in.readLine();
71
            System.out.println (s);
72
 
224 ira 73
            // Stop when the lease expires
223 ira 74
            if (s.equals("IP Lease Time Expired"))
75
                running = false;
76
        }
77
 
224 ira 78
        // Tell the user that we are quitting
79
        System.out.println ("Client Exiting");
223 ira 80
 
224 ira 81
        // Clean up all the sockets
223 ira 82
        out.close();
83
        in.close();
84
        socket.close();
85
    }
86
}
87