ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/UDPReader.java
(Generate patch)

Comparing experimental/server/XMLReader/UDPReader.java (file contents):
Revision 1.1 by pjm2, Fri Nov 17 11:23:19 2000 UTC vs.
Revision 1.3 by pjm2, Fri Nov 17 15:25:48 2000 UTC

# Line 10 | Line 10 | public class UDPReader {
10  
11      public static void main(String[] args) throws IOException {
12  
13 <        DatagramSocket socket = new DatagramSocket(4589);
13 >        // default UDP listening port.
14 >        int port = 4589;
15 >        final int packetSizeLimit = 8192;
16  
17 +        // Allow the user to choose their own port number.
18 +        if (args.length == 1){
19 +            try {
20 +                port = new Integer(args[0]).intValue();
21 +            }
22 +            catch (Exception e){
23 +                // If something went wrong, use the default again.
24 +                port = 4589;
25 +            }
26 +        }
27 +
28 +        DatagramSocket socket = null;
29          try {
30 <            byte[] buf = new byte[1024];
17 <                // receive request
18 <            DatagramPacket packet = new DatagramPacket(buf, buf.length);
19 <            socket.receive(packet);
20 <            String dataIn = new String(packet.getData());
21 <            System.out.println("Received: " + dataIn);
30 >            socket = new DatagramSocket(port);
31          }
32 <        catch (IOException e) {
33 <            System.out.println("An exception occured!");
34 <            e.printStackTrace();
32 >        catch (BindException e){
33 >            System.out.println("Some other process is already listening on port "+port+".");
34 >            System.out.println("Please specify another port number on the command line.");
35 >            System.exit(0);
36 >        }
37 >        
38 >        System.out.println("UDPReader ready and listening for UDP packets on port "+port);
39 >
40 >        boolean running = true;
41 >        while (running){
42 >            try {
43 >                byte[] buf = new byte[packetSizeLimit];
44 >                // receive request
45 >                DatagramPacket packet = new DatagramPacket(buf, buf.length);
46 >                socket.receive(packet);
47 >                UDPReaderThread thread = new UDPReaderThread(packet);
48 >                thread.run();
49 >            }
50 >            catch (IOException e) {
51 >                System.out.println("An exception occured in the UDPReader!");
52 >                e.printStackTrace();
53 >            }
54          }
55          socket.close();
56      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines