Subversion Repositories programming

Rev

Rev 211 | Rev 215 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 211 Rev 213
Line -... Line 1...
-
 
1
/*******************************************************************************
-
 
2
 * File: P2_Server.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 server which will listen on UDP port 1337 for
-
 
11
 * incoming datagrams. It will first recieve a message telling it to drop
-
 
12
 * every n-th message. After that it will convert every message it recieves
-
 
13
 * from 4B to 5B encoding, and reply back to the client. It will only exit on
-
 
14
 * receiving the message "QUIT".
-
 
15
 ******************************************************************************/
-
 
16
 
1
import java.io.*;
17
import java.io.*;
2
import java.util.*;
18
import java.util.*;
3
import java.net.*;
19
import java.net.*;
4
import java.util.regex.*;
20
import java.util.regex.*;
5
 
21
 
6
public class P2_Server
22
public class P2_Server
7
{
23
{
8
    private static int portNumber = 1337;
24
    private static int portNumber = 1337;
9
    private static int dropEvery = -1;
25
    private static int dropEvery = -1;
10
    private static int sendCount = 1;
26
    private static int sendCount = 0;
11
    private static DatagramPacket packet;
27
    private static DatagramPacket packet;
12
    private static DatagramSocket socket;
28
    private static DatagramSocket socket;
13
 
29
 
-
 
30
    /**
-
 
31
     * Method: readPacket()
-
 
32
     * Purpose: Read a packet from the currently opened socket.
-
 
33
     * Note: Blocks until it recieves data.
-
 
34
     */
14
    public static String readPacket (int maxlen)
35
    public static String readPacket (int maxlen)
15
    {
36
    {
16
        byte[] buf = new byte[maxlen];
37
        byte[] buf = new byte[maxlen];
17
        packet = new DatagramPacket (buf, buf.length);
38
        packet = new DatagramPacket (buf, buf.length);
18
 
39
 
Line 25... Line 46...
25
        }
46
        }
26
 
47
 
27
        System.out.println ("Recieved Packet. Len: " + packet.getLength());
48
        System.out.println ("Recieved Packet. Len: " + packet.getLength());
28
 
49
 
29
        String s = new String (buf, 0, packet.getLength() /*- 1*/);
50
        String s = new String (buf, 0, packet.getLength() /*- 1*/);
30
        System.out.println ("RcvdPacket: |" + s + "|");
-
 
31
        return s;
51
        return s;
32
    }
52
    }
33
 
53
 
-
 
54
    /**
-
 
55
     * Method: writePacket()
-
 
56
     * Purpose: write a message to the last client that sent us a message,
-
 
57
     * while dropping every n-th message, as specified by the client.
-
 
58
     */
34
    public static boolean writePacket (String msg)
59
    public static boolean writePacket (String msg)
35
    {
60
    {
36
        if (sendCount == dropEvery && !msg.equals("QUIT"))
61
        if (sendCount == dropEvery && !msg.equals("QUIT"))
37
        {
62
        {
38
            System.out.println ("Dropped a packet");
63
            System.out.println ("Dropped a packet");
39
            sendCount = 0; // reset the sendCount
64
            sendCount = 0; // reset the sendCount
40
            return true;   // "Fake" send
65
            return true;   // "Fake" send
41
        }
66
        }
42
        else { sendCount++; }
67
        else { sendCount++; }
43
 
68
 
44
        System.out.println ("Num sent: " + sendCount);
-
 
45
 
-
 
46
        InetAddress address = packet.getAddress();
69
        InetAddress address = packet.getAddress();
47
        int port = packet.getPort();
70
        int port = packet.getPort();
48
 
71
 
49
        byte[] buf = msg.getBytes();
72
        byte[] buf = msg.getBytes();
50
        packet = new DatagramPacket (buf, buf.length, address, port);
73
        packet = new DatagramPacket (buf, buf.length, address, port);
Line 58... Line 81...
58
        }
81
        }
59
 
82
 
60
        return true;
83
        return true;
61
    }
84
    }
62
 
85
 
-
 
86
    /**
-
 
87
     * Method: calcDrops()
-
 
88
     * Purpose: Parse a message sent by the client telling us to drop
-
 
89
     * every n-th message.
-
 
90
     */
63
    public static int calcDrops (String s)
91
    public static int calcDrops (String s)
64
    {
92
    {
65
        String dropRegex = "^DROP(\\d*)(.*)$";
93
        String dropRegex = "^DROP(\\d*)(.*)$";
66
        Pattern p = Pattern.compile(dropRegex);
94
        Pattern p = Pattern.compile(dropRegex);
67
        Matcher m = p.matcher(s);
95
        Matcher m = p.matcher(s);
Line 70... Line 98...
70
            return Integer.parseInt(m.group(1));
98
            return Integer.parseInt(m.group(1));
71
 
99
 
72
        return -1;
100
        return -1;
73
    }
101
    }
74
 
102
 
-
 
103
    /**
-
 
104
     * Method: main()
-
 
105
     * Purpose: Open a listening UDP socket, and convert a client's
-
 
106
     * requests from 4B encoding to 5B encoding.
-
 
107
     */
75
    public static void main (String[] args) throws Exception
108
    public static void main (String[] args) throws Exception
76
    {
109
    {
77
        boolean done = false;
110
        boolean done = false;
78
        socket = new DatagramSocket(portNumber);
111
        socket = new DatagramSocket(portNumber);
79
        packet = null;
112
        packet = null;
Line 88... Line 121...
88
            if (dropEvery != -1)
121
            if (dropEvery != -1)
89
            {
122
            {
90
                done = true;
123
                done = true;
91
                System.out.println ("dropEvery: " + dropEvery);
124
                System.out.println ("dropEvery: " + dropEvery);
92
            }
125
            }
-
 
126
 
-
 
127
            writePacket (s);
93
        }
128
        }
94
 
129
 
95
        done = false;
130
        done = false;
96
 
131
 
97
        // Keep reading until we get a quit packet
132
        // Keep reading until we get a quit packet
Line 114... Line 149...
114
                s = "BADVAL";
149
                s = "BADVAL";
115
            }
150
            }
116
 
151
 
117
            writePacket (s);
152
            writePacket (s);
118
        }
153
        }
-
 
154
 
-
 
155
        // Close the socket
-
 
156
        socket.close();
119
    }
157
    }
120
}
158
}
-
 
159