Subversion Repositories programming

Rev

Rev 223 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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