Subversion Repositories programming

Rev

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

Rev 212 Rev 213
Line -... Line 1...
-
 
1
/*******************************************************************************
-
 
2
 * File: P2_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 #2
-
 
8
 * Date Last Modified: 2006-02-05
-
 
9
 *
-
 
10
 * Purpose: Implement a simple client allowing the user to send datagrams
-
 
11
 * to P2_Server, which will decode them from 4B to 5B encoding. The client
-
 
12
 * will retransmit any packets that do not recieve a reply.
-
 
13
 ******************************************************************************/
1
 
14
 
2
import java.io.*;
15
import java.io.*;
3
import java.net.*;
16
import java.net.*;
4
import java.util.*;
17
import java.util.*;
5
 
18
 
Line 8... Line 21...
8
    private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
21
    private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
9
    private static DatagramSocket socket;
22
    private static DatagramSocket socket;
10
    private static DatagramPacket packet;
23
    private static DatagramPacket packet;
11
    private static int portNumber = 1337;
24
    private static int portNumber = 1337;
12
 
25
 
-
 
26
    /**
-
 
27
     * Method: readPacket()
-
 
28
     * Purpose: Read a packet on the currently opened DatagramSocket,
-
 
29
     * and return it as a String.
-
 
30
     */
13
    public static String readPacket (int maxlen)
31
    public static String readPacket (int maxlen)
14
    {
32
    {
15
        byte[] buf = new byte[maxlen];
33
        byte[] buf = new byte[maxlen];
16
        packet = new DatagramPacket (buf, buf.length);
34
        packet = new DatagramPacket (buf, buf.length);
17
 
35
 
Line 30... Line 48...
30
            return null;
48
            return null;
31
        }
49
        }
32
 
50
 
33
        return new String (buf, 0, packet.getLength()/*- 1*/);
51
        return new String (buf, 0, packet.getLength()/*- 1*/);
34
    }
52
    }
-
 
53
 
-
 
54
    /**
-
 
55
     * Method: writePacket()
-
 
56
     * Purpose: Send a packet to localhost:portNumber.
-
 
57
     */
35
    public static boolean writePacket (String msg) throws Exception
58
    public static boolean writePacket (String msg) throws Exception
36
    {
59
    {
37
        InetAddress address = InetAddress.getLocalHost();
60
        InetAddress address = InetAddress.getLocalHost();
38
        int port = portNumber;
61
        int port = portNumber;
39
 
62
 
Line 53... Line 76...
53
        }
76
        }
54
 
77
 
55
        return true;
78
        return true;
56
    }
79
    }
57
 
80
 
-
 
81
    /**
-
 
82
     * Method: guaranteedTransmit()
-
 
83
     * Purpose: A wrapper around readPacket() and writePacket() that implements
-
 
84
     * a reliable transmit mechanism. It allows you to specify a maximum number
-
 
85
     * of times to try resending a packet.
-
 
86
     */
-
 
87
    public static String guaranteedTransmit (String msg, int maxlen, int maxRetries) throws Exception
-
 
88
    {
-
 
89
        String reply = null;
-
 
90
        int retries = 0;
-
 
91
 
-
 
92
        writePacket (msg);
-
 
93
        reply = readPacket (maxlen);
-
 
94
 
-
 
95
        while (reply == null && retries < maxRetries)
-
 
96
        {
-
 
97
            System.err.println ("Packet lost, retransmitting...");
-
 
98
            writePacket (msg);
-
 
99
            reply = readPacket (maxlen);
-
 
100
            retries++;
-
 
101
        }
-
 
102
 
-
 
103
        return reply;
-
 
104
    }
-
 
105
 
-
 
106
    /**
-
 
107
     * Method: main()
-
 
108
     * Purpose: This will accept input from the user, and attempt to send it
-
 
109
     * to the corresponding server, which will convert the messages from 4B
-
 
110
     * to 5B encoding.
-
 
111
     */
58
    public static void main (String[] args) throws Exception
112
    public static void main (String[] args) throws Exception
59
    {
113
    {
60
        int dropHowMany = 0;
114
        int dropHowMany = 0;
61
        socket = new DatagramSocket();
115
        socket = new DatagramSocket();
62
        socket.connect (InetAddress.getLocalHost(), portNumber);
116
        socket.connect (InetAddress.getLocalHost(), portNumber);
Line 71... Line 125...
71
        } catch (ArrayIndexOutOfBoundsException e) {
125
        } catch (ArrayIndexOutOfBoundsException e) {
72
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
126
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
73
            System.exit(1);
127
            System.exit(1);
74
        }
128
        }
75
 
129
 
76
        writePacket ("DROP" + dropHowMany);
130
        String msg = new String("DROP" + dropHowMany);
-
 
131
        String reply = guaranteedTransmit (msg, 6, 1);
-
 
132
 
-
 
133
        if (!reply.equals(msg))
-
 
134
        {
-
 
135
            System.err.println ("Unable to inform the server how often to drop packets");
-
 
136
            System.exit(1);
-
 
137
        }
77
 
138
 
78
        System.out.print ("% decode: ");
139
        System.out.print ("% decode: ");
79
        String msg = kbd.readLine();
140
        msg = kbd.readLine();
80
        String reply;
-
 
81
        boolean done = false;
141
        boolean done = false;
82
 
142
 
83
        while (!done)
143
        while (!done)
84
        {
144
        {
85
            writePacket (msg);
-
 
86
            reply = readPacket(5);
145
            reply = guaranteedTransmit (msg, 6, 1);
87
 
-
 
88
            while (reply == null)
-
 
89
            {
-
 
90
                System.out.println ("Packet lost, retransmitting...");
-
 
91
                writePacket (msg);
-
 
92
                reply = readPacket(5);
-
 
93
            }
-
 
94
 
146
 
95
            if (reply.equals("QUIT"))
147
            if (reply.equals("QUIT"))
96
            {
148
            {
97
                done = true;
149
                done = true;
98
                continue;
150
                continue;
Line 100... Line 152...
100
 
152
 
101
            System.out.println ("-> reply: " + reply);
153
            System.out.println ("-> reply: " + reply);
102
            System.out.print ("% decode: ");
154
            System.out.print ("% decode: ");
103
            msg = kbd.readLine();
155
            msg = kbd.readLine();
104
        }
156
        }
-
 
157
 
-
 
158
        // Close the socket
-
 
159
        socket.close();
105
    }
160
    }
106
}
161
}
107
 
162