Subversion Repositories programming

Rev

Rev 212 | Go to most recent revision | Details | 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
    {
60
        socket = new DatagramSocket();
61
        socket.connect (InetAddress.getLocalHost(), portNumber);
62
        socket.setSoTimeout (soTimeout);
63
 
64
        BufferedReader kbd = new BufferedReader (
65
                                 new InputStreamReader (
66
                                     System.in));
67
 
68
        writePacket ("DROP3");
69
 
70
        System.out.print ("% decode: ");
71
        String msg = kbd.readLine();
72
        String reply;
73
        boolean done = false;
74
 
75
        while (!done)
76
        {
77
            writePacket (msg);
78
            reply = readPacket(5);
79
 
80
            while (reply == null)
81
            {
82
                System.out.println ("Packet lost, retransmitting...");
83
                writePacket (msg);
84
                reply = readPacket(5);
85
            }
86
 
87
            if (reply.equals("QUIT"))
88
            {
89
                done = true;
90
                continue;
91
            }
92
 
93
            System.out.println ("-> reply: " + reply);
94
            System.out.print ("% decode: ");
95
            msg = kbd.readLine();
96
        }
97
    }
98
}
99