Subversion Repositories programming

Rev

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

Rev Author Line No. Line
213 ira 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
 ******************************************************************************/
211 ira 14
 
15
import java.io.*;
16
import java.net.*;
17
import java.util.*;
18
 
19
public class P2_Client
20
{
21
    private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
22
    private static DatagramSocket socket;
23
    private static DatagramPacket packet;
24
    private static int portNumber = 1337;
25
 
213 ira 26
    /**
27
     * Method: readPacket()
28
     * Purpose: Read a packet on the currently opened DatagramSocket,
29
     * and return it as a String.
30
     */
215 ira 31
    public static P2_Packet readPacket ()
211 ira 32
    {
215 ira 33
        byte[] buf = new byte[256];
211 ira 34
        packet = new DatagramPacket (buf, buf.length);
35
 
36
        try {
37
            socket.receive (packet);
38
        } catch (SocketTimeoutException e) {
39
            System.out.println ("Read timed out, requesting retransmit");
40
            return null;
41
        } catch (PortUnreachableException e) {
42
            System.out.println ("Caught PortUnreachableException, are you" +
43
                                " sure the server is running?");
44
            System.exit(1);
45
        } catch (IOException e) {
46
            System.out.println ("Caught exception in readPacket()");
47
            e.printStackTrace();
48
            return null;
49
        }
50
 
215 ira 51
        return new P2_Packet (packet.getData());
211 ira 52
    }
213 ira 53
 
54
    /**
55
     * Method: writePacket()
56
     * Purpose: Send a packet to localhost:portNumber.
57
     */
215 ira 58
    public static boolean writePacket (byte type, byte[] data)
211 ira 59
    {
215 ira 60
        InetAddress address = null;
211 ira 61
        int port = portNumber;
62
 
215 ira 63
        try {
64
            address = InetAddress.getLocalHost();
65
        } catch (UnknownHostException e) {
66
            System.err.println ("Caught UnknownHostException in writePacket(), " +
67
                                "are you sure the system is working?");
68
            System.exit(1);
69
        }
70
 
71
        byte[] buf = P2_Packet.createPacket (type, data);
211 ira 72
        packet = new DatagramPacket (buf, buf.length, address, port);
73
 
74
        try {
75
            socket.send (packet);
76
        } catch (PortUnreachableException e) {
77
            System.out.println ("Caught PortUnreachableException, are " +
78
                                "you sure the server is running?");
79
            System.exit(1);
80
        } catch (IOException e) {
81
            System.out.println ("Caught exception in writePacket()");
82
            e.printStackTrace();
83
            return false;
84
        }
85
 
86
        return true;
87
    }
88
 
215 ira 89
    public static boolean writePacket (byte type)
90
    {
91
        byte[] pData = { 0 };
92
        return writePacket (type, pData);
93
    }
94
 
213 ira 95
    /**
96
     * Method: guaranteedTransmit()
97
     * Purpose: A wrapper around readPacket() and writePacket() that implements
98
     * a reliable transmit mechanism. It allows you to specify a maximum number
99
     * of times to try resending a packet.
100
     */
215 ira 101
    public static P2_Packet guaranteedTransmit (byte type, byte[] data)
213 ira 102
    {
215 ira 103
        P2_Packet reply;
213 ira 104
        int retries = 0;
105
 
215 ira 106
        writePacket (type, data);
107
        reply = readPacket ();
213 ira 108
 
215 ira 109
        while (reply == null)
213 ira 110
        {
111
            System.err.println ("Packet lost, retransmitting...");
215 ira 112
            writePacket (type, data);
113
            reply = readPacket ();
213 ira 114
            retries++;
115
        }
116
 
117
        return reply;
118
    }
119
 
215 ira 120
    public static P2_Packet guaranteedTransmit (byte type)
121
    {
122
        byte[] pData = { 0 };
123
 
124
        return guaranteedTransmit (type, pData);
125
    }
126
 
213 ira 127
    /**
128
     * Method: main()
129
     * Purpose: This will accept input from the user, and attempt to send it
130
     * to the corresponding server, which will convert the messages from 4B
131
     * to 5B encoding.
132
     */
211 ira 133
    public static void main (String[] args) throws Exception
134
    {
212 ira 135
        int dropHowMany = 0;
211 ira 136
        socket = new DatagramSocket();
137
        socket.connect (InetAddress.getLocalHost(), portNumber);
138
        socket.setSoTimeout (soTimeout);
139
 
140
        BufferedReader kbd = new BufferedReader (
141
                                 new InputStreamReader (
142
                                     System.in));
143
 
212 ira 144
        try {
145
            dropHowMany = Integer.parseInt(args[0]);
146
        } catch (ArrayIndexOutOfBoundsException e) {
147
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
148
            System.exit(1);
149
        }
211 ira 150
 
215 ira 151
        String msg;
152
        byte[] pData = { (byte)dropHowMany };
153
        P2_Packet reply = guaranteedTransmit (P2_Packet.DROP, pData);
212 ira 154
 
215 ira 155
        if (reply.getPacketType() != P2_Packet.DROP)
213 ira 156
        {
157
            System.err.println ("Unable to inform the server how often to drop packets");
158
            System.exit(1);
159
        }
160
 
211 ira 161
        System.out.print ("% decode: ");
213 ira 162
        msg = kbd.readLine();
211 ira 163
        boolean done = false;
164
 
165
        while (!done)
166
        {
215 ira 167
            if (msg.equals("QUIT"))
211 ira 168
            {
215 ira 169
                reply = guaranteedTransmit (P2_Packet.QUIT);
211 ira 170
                done = true;
171
                continue;
172
            }
215 ira 173
 
174
            reply = guaranteedTransmit (P2_Packet.TRANSREQ, P2_EncDec.convertToByteArray(msg));
211 ira 175
 
215 ira 176
            System.out.println ("-> reply: " + P2_EncDec.convertToString(reply.getPacketData()));
211 ira 177
            System.out.print ("% decode: ");
178
            msg = kbd.readLine();
179
        }
213 ira 180
 
181
        // Close the socket
182
        socket.close();
211 ira 183
    }
184
}
185