Subversion Repositories programming

Rev

Rev 217 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 217 Rev 218
Line 3... Line 3...
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * License: GNU General Public License v2
4
 * License: GNU General Public License v2
5
 * Class: CS380 - Computer Networking
5
 * Class: CS380 - Computer Networking
6
 *
6
 *
7
 * Assignment: Project #2
7
 * Assignment: Project #2
8
 * Date Last Modified: 2006-02-05
8
 * Date Last Modified: 2006-02-08
9
 *
9
 *
10
 * Purpose: Implement a simple client allowing the user to send datagrams
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
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.
12
 * will retransmit any packets that do not recieve a reply.
13
 ******************************************************************************/
13
 ******************************************************************************/
Line 22... Line 22...
22
    private static DatagramSocket socket;
22
    private static DatagramSocket socket;
23
    private static DatagramPacket packet;
23
    private static DatagramPacket packet;
24
    private static int portNumber = 1337;
24
    private static int portNumber = 1337;
25
    private static int packetNum = 0;
25
    private static int packetNum = 0;
26
 
26
 
-
 
27
    /**
-
 
28
     * Method: verifyInput()
-
 
29
     * Purpose: Verify the user's input before it is sent out to the
-
 
30
     * server for translation.
-
 
31
     */
27
    public static boolean verifyInput (String s)
32
    public static boolean verifyInput (String s)
28
    {
33
    {
29
        int i;
34
        int i;
30
        
35
        
31
        // Check for QUIT message
36
        // Check for QUIT message
32
        if (s.equals("QUIT"))
37
        if (s.equals("QUIT"))
33
            return true;
38
            return true;
34
 
39
 
35
        // If it's not a QUIT, it must be 0's and 1's only
40
        // If it's not a QUIT, it must be 0's and 1's only
36
        for (i=0; i<s.length; i++)
41
        for (i=0; i<s.length(); i++)
37
            if (!(s.charAt(i) == '0' || s.charAt(i) == '1'))
42
            if (!(s.charAt(i) == '0' || s.charAt(i) == '1'))
38
                return false;
43
                return false;
39
 
44
 
40
        // Everything was a 0 or 1, good!
45
        // Everything was a 0 or 1, good!
41
        return true;
46
        return true;
42
    }
47
    }
43
 
48
 
44
    /**
49
    /**
45
     * Method: readPacket()
50
     * Method: readPacket()
46
     * Purpose: Read a packet on the currently opened DatagramSocket,
51
     * Purpose: Read a packet on the currently opened DatagramSocket,
47
     * and return it as a String.
52
     * and return it as a P2_Packet.
48
     */
53
     */
49
    public static P2_Packet readPacket ()
54
    public static P2_Packet readPacket ()
50
    {
55
    {
51
        byte[] buf = new byte[256];
56
        byte[] buf = new byte[256];
52
        packet = new DatagramPacket (buf, buf.length);
57
        packet = new DatagramPacket (buf, buf.length);
53
 
58
 
-
 
59
        // Try to recieve a packet from the server, and catch all possible
-
 
60
        // exceptions that can happen.
54
        try {
61
        try {
55
            socket.receive (packet);
62
            socket.receive (packet);
56
        } catch (SocketTimeoutException e) {
63
        } catch (SocketTimeoutException e) {
57
            System.out.println ("Read timed out, requesting retransmit");
64
            System.out.println ("Read timed out, requesting retransmit");
58
            return null;
65
            return null;
Line 64... Line 71...
64
            System.out.println ("Caught exception in readPacket()");
71
            System.out.println ("Caught exception in readPacket()");
65
            e.printStackTrace();
72
            e.printStackTrace();
66
            return null;
73
            return null;
67
        }
74
        }
68
 
75
 
-
 
76
        // Convert the bytes to a P2_Packet, then return them.
69
        return new P2_Packet (packet.getData());
77
        return new P2_Packet (packet.getData());
70
    }
78
    }
71
 
79
 
72
    /**
80
    /**
73
     * Method: writePacket()
81
     * Method: writePacket()
Line 76... Line 84...
76
    public static boolean writePacket (byte type, byte[] data, int packetNum)
84
    public static boolean writePacket (byte type, byte[] data, int packetNum)
77
    {
85
    {
78
        InetAddress address = null;
86
        InetAddress address = null;
79
        int port = portNumber;
87
        int port = portNumber;
80
 
88
 
-
 
89
        // Try to get the localhost address. If this fail's, we've got major
-
 
90
        // problems going on, so just exit.
81
        try {
91
        try {
82
            address = InetAddress.getLocalHost();
92
            address = InetAddress.getLocalHost();
83
        } catch (UnknownHostException e) {
93
        } catch (UnknownHostException e) {
84
            System.err.println ("Caught UnknownHostException in writePacket(), " +
94
            System.err.println ("Caught UnknownHostException in writePacket(), " +
85
                                "are you sure the system is working?");
95
                                "are you sure the system is working?");
86
            System.exit(1);
96
            System.exit(1);
87
        }
97
        }
88
 
98
 
-
 
99
        // Create the new packet
89
        byte[] buf = P2_Packet.createPacket (type, data, packetNum);
100
        byte[] buf = P2_Packet.createPacket (type, data, packetNum);
90
        packet = new DatagramPacket (buf, buf.length, address, port);
101
        packet = new DatagramPacket (buf, buf.length, address, port);
91
 
102
 
-
 
103
        // Try to send the packet, and catch all of the possible errors.
92
        try {
104
        try {
93
            socket.send (packet);
105
            socket.send (packet);
94
        } catch (PortUnreachableException e) {
106
        } catch (PortUnreachableException e) {
95
            System.out.println ("Caught PortUnreachableException, are " +
107
            System.out.println ("Caught PortUnreachableException, are " +
96
                                "you sure the server is running?");
108
                                "you sure the server is running?");
Line 99... Line 111...
99
            System.out.println ("Caught exception in writePacket()");
111
            System.out.println ("Caught exception in writePacket()");
100
            e.printStackTrace();
112
            e.printStackTrace();
101
            return false;
113
            return false;
102
        }
114
        }
103
 
115
 
-
 
116
        // We sent successfully, so return true.
104
        return true;
117
        return true;
105
    }
118
    }
106
 
119
 
107
    /**
120
    /**
108
     * Method: guaranteedTransmit()
121
     * Method: guaranteedTransmit()
Line 112... Line 125...
112
    public static P2_Packet guaranteedTransmit (byte type, byte[] data)
125
    public static P2_Packet guaranteedTransmit (byte type, byte[] data)
113
    {
126
    {
114
        P2_Packet reply;
127
        P2_Packet reply;
115
        int retries = 0;
128
        int retries = 0;
116
 
129
 
-
 
130
        // Send a packet, and wait for a reply
117
        writePacket (type, data, packetNum);
131
        writePacket (type, data, packetNum);
118
        reply = readPacket ();
132
        reply = readPacket ();
119
 
133
 
-
 
134
        // If the read timed out, keep trying to send until it gets through
120
        while (reply == null)
135
        while (reply == null)
121
        {
136
        {
122
            System.err.println ("Packet lost, retransmitting...");
137
            System.err.println ("Packet lost, retransmitting...");
123
            writePacket (type, data, packetNum);
138
            writePacket (type, data, packetNum);
124
            reply = readPacket ();
139
            reply = readPacket ();
125
            retries++;
140
            retries++;
126
        }
141
        }
127
 
142
 
-
 
143
        // Reset the packetNum if we had to retry sending a packet.
-
 
144
        // The packetNum is used to control the server's dropping
-
 
145
        // of packets.
128
        if (retries > 0)
146
        if (retries > 0)
129
            packetNum = 0; //reset packetNum
147
            packetNum = 0;
130
 
148
 
131
        packetNum++;
149
        packetNum++;
132
        return reply;
150
        return reply;
133
    }
151
    }
134
 
152
 
Line 152... Line 170...
152
    public static void main (String[] args) throws Exception
170
    public static void main (String[] args) throws Exception
153
    {
171
    {
154
        int dropHowMany = 0;
172
        int dropHowMany = 0;
155
        socket = new DatagramSocket();
173
        socket = new DatagramSocket();
156
        socket.connect (InetAddress.getLocalHost(), portNumber);
174
        socket.connect (InetAddress.getLocalHost(), portNumber);
-
 
175
 
-
 
176
        // Set the socket's timeout to 3 seconds
157
        socket.setSoTimeout (soTimeout);
177
        socket.setSoTimeout (soTimeout);
158
 
178
 
159
        BufferedReader kbd = new BufferedReader (
179
        BufferedReader kbd = new BufferedReader (
160
                                 new InputStreamReader (
180
                                 new InputStreamReader (
161
                                     System.in));
181
                                     System.in));
162
 
182
 
-
 
183
        // Parse the first command-line argument into an int, and use that
-
 
184
        // to inform the server how often to drop packets.
163
        try {
185
        try {
164
            dropHowMany = Integer.parseInt(args[0]);
186
            dropHowMany = Integer.parseInt(args[0]);
165
        } catch (ArrayIndexOutOfBoundsException e) {
187
        } catch (ArrayIndexOutOfBoundsException e) {
166
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
188
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
167
            System.exit(1);
189
            System.exit(1);
Line 169... Line 191...
169
 
191
 
170
        String msg;
192
        String msg;
171
        byte[] pData = { (byte)dropHowMany };
193
        byte[] pData = { (byte)dropHowMany };
172
        P2_Packet reply = guaranteedTransmit (P2_Packet.DROP, pData);
194
        P2_Packet reply = guaranteedTransmit (P2_Packet.DROP, pData);
173
 
195
 
-
 
196
        // Check to make sure our packet informing the server how often to drop
-
 
197
        // packets was recieved successfully.
174
        if (reply.getPacketType() != P2_Packet.DROP)
198
        if (reply.getPacketType() != P2_Packet.DROP)
175
        {
199
        {
176
            System.err.println ("Unable to inform the server how often to drop packets");
200
            System.err.println ("Unable to inform the server how often to drop packets");
177
            System.exit(1);
201
            System.exit(1);
178
        }
202
        }
179
 
203
 
180
        System.out.print ("% decode: ");
-
 
181
        msg = kbd.readLine();
-
 
182
        boolean done = false;
204
        boolean done = false;
183
 
205
 
-
 
206
        // Keep running until the user decides that they don't want to
-
 
207
        // decode any more 4B numbers.
184
        while (!done)
208
        while (!done)
185
        {
209
        {
-
 
210
            System.out.print ("% decode: ");
-
 
211
            msg = kbd.readLine ();
-
 
212
 
-
 
213
            // Check user input
-
 
214
            if (!verifyInput(msg))
-
 
215
            {
-
 
216
                System.out.println ("Bad input, try again...");
-
 
217
                continue;
-
 
218
            }
-
 
219
            
-
 
220
            // Check for a QUIT message
186
            if (msg.equals("QUIT"))
221
            if (msg.equals("QUIT"))
187
            {
222
            {
188
                reply = guaranteedTransmit (P2_Packet.QUIT);
223
                reply = guaranteedTransmit (P2_Packet.QUIT);
189
                done = true;
224
                done = true;
190
                continue;
225
                continue;
191
            }
226
            }
192
            
227
            
193
            reply = guaranteedTransmit (P2_Packet.TRANSREQ, P2_EncDec.convertToByteArray(msg));
228
            reply = guaranteedTransmit (P2_Packet.TRANSREQ, P2_EncDec.convertToByteArray(msg));
194
 
229
 
-
 
230
            // Check for a bad packet from the server
-
 
231
            if (reply.getPacketType() == P2_Packet.BADPACKET)
195
            System.out.println ("-> reply: " + P2_EncDec.convertToString(reply.getPacketData()));
232
                System.out.println ("-> reply: BAD REQUEST");
-
 
233
            else
196
            System.out.print ("% decode: ");
234
                System.out.println ("-> reply: " + 
197
            msg = kbd.readLine();
235
                        P2_EncDec.convertToString(reply.getPacketData()));
198
        }
236
        }
199
 
237
 
200
        // Close the socket
238
        // Close the socket
201
        socket.close();
239
        socket.close();
202
    }
240
    }