Subversion Repositories programming

Rev

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

Rev Author Line No. Line
211 ira 1
 
2
import java.io.*;
3
import java.net.*;
4
import java.util.*;
5
 
6
public class P2_Client
7
{
8
    private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
9
    private static DatagramSocket socket;
10
    private static DatagramPacket packet;
11
    private static int portNumber = 1337;
12
 
13
    public static String readPacket (int maxlen)
14
    {
15
        byte[] buf = new byte[maxlen];
16
        packet = new DatagramPacket (buf, buf.length);
17
 
18
        try {
19
            socket.receive (packet);
20
        } catch (SocketTimeoutException e) {
21
            System.out.println ("Read timed out, requesting retransmit");
22
            return null;
23
        } catch (PortUnreachableException e) {
24
            System.out.println ("Caught PortUnreachableException, are you" +
25
                                " sure the server is running?");
26
            System.exit(1);
27
        } catch (IOException e) {
28
            System.out.println ("Caught exception in readPacket()");
29
            e.printStackTrace();
30
            return null;
31
        }
32
 
33
        return new String (buf, 0, packet.getLength()/*- 1*/);
34
    }
35
    public static boolean writePacket (String msg) throws Exception
36
    {
37
        InetAddress address = InetAddress.getLocalHost();
38
        int port = portNumber;
39
 
40
        byte[] buf = msg.getBytes();
41
        packet = new DatagramPacket (buf, buf.length, address, port);
42
 
43
        try {
44
            socket.send (packet);
45
        } catch (PortUnreachableException e) {
46
            System.out.println ("Caught PortUnreachableException, are " +
47
                                "you sure the server is running?");
48
            System.exit(1);
49
        } catch (IOException e) {
50
            System.out.println ("Caught exception in writePacket()");
51
            e.printStackTrace();
52
            return false;
53
        }
54
 
55
        return true;
56
    }
57
 
58
    public static void main (String[] args) throws Exception
59
    {
212 ira 60
        int dropHowMany = 0;
211 ira 61
        socket = new DatagramSocket();
62
        socket.connect (InetAddress.getLocalHost(), portNumber);
63
        socket.setSoTimeout (soTimeout);
64
 
65
        BufferedReader kbd = new BufferedReader (
66
                                 new InputStreamReader (
67
                                     System.in));
68
 
212 ira 69
        try {
70
            dropHowMany = Integer.parseInt(args[0]);
71
        } catch (ArrayIndexOutOfBoundsException e) {
72
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
73
            System.exit(1);
74
        }
211 ira 75
 
212 ira 76
        writePacket ("DROP" + dropHowMany);
77
 
211 ira 78
        System.out.print ("% decode: ");
79
        String msg = kbd.readLine();
80
        String reply;
81
        boolean done = false;
82
 
83
        while (!done)
84
        {
85
            writePacket (msg);
86
            reply = readPacket(5);
87
 
88
            while (reply == null)
89
            {
90
                System.out.println ("Packet lost, retransmitting...");
91
                writePacket (msg);
92
                reply = readPacket(5);
93
            }
94
 
95
            if (reply.equals("QUIT"))
96
            {
97
                done = true;
98
                continue;
99
            }
100
 
101
            System.out.println ("-> reply: " + reply);
102
            System.out.print ("% decode: ");
103
            msg = kbd.readLine();
104
        }
105
    }
106
}
107