Subversion Repositories programming

Rev

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