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.6 by pjm2, Tue Nov 21 09:15:06 2000 UTC

# Line 3 | Line 3 | import java.net.*;
3   import java.util.*;
4  
5   // This class contains the main method to be run by
6 < // the filter children.  It harvests UDP traffic
6 > // the filter children.  It harvests UDP traffic.
7   //
8   //
9 < public class UDPReader {
9 > public class UDPReader extends Thread{
10  
11 <    public static void main(String[] args) throws IOException {
11 >    // It is normal to use this constructor in preference
12 >    // to any other in this class.
13 >    public UDPReader(int port, Logger logger){
14 >        this.logger = logger;
15 >        this.port = port;
16 >    }
17 >    
18 >    public UDPReader(Logger logger){
19 >        this(4589, logger);
20 >    }
21  
22 <        DatagramSocket socket = new DatagramSocket(4589);
22 >    public void run() {
23  
24 +        DatagramSocket socket = null;
25          try {
26 <            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);
26 >            socket = new DatagramSocket(port);
27          }
28 <        catch (IOException e) {
29 <            System.out.println("An exception occured!");
30 <            e.printStackTrace();
28 >        catch (BindException e){
29 >            System.out.println("Some other process is already listening on port "+port+".");
30 >            System.out.println("Please specify another port number on the command line.");
31 >            return;
32          }
33 +        catch (Exception e){
34 +            System.out.println("An exception occured while creating the DatagramSocket.");
35 +            return;
36 +        }
37 +        
38 +        System.out.println("UDPReader ready and listening for UDP packets on port "+port);
39 +
40 +        byte[] buf;
41 +        UDPReaderThread t = new UDPReaderThread();
42 +        
43 +        boolean running = true;
44 +        while (running){
45 +            try {
46 +                buf = new byte[packetSizeLimit];
47 +                // receive request
48 +                DatagramPacket packet = new DatagramPacket(buf, buf.length);
49 +                socket.receive(packet);
50 +                t.run(packet);
51 +            }
52 +            catch (IOException e) {
53 +                System.out.println("An exception occured in the UDPReader!");
54 +                e.printStackTrace();
55 +            }
56 +        }
57          socket.close();
58      }
59 +    
60 +    
61 +    Logger logger;
62 +    int port;
63 +    
64 +    final int packetSizeLimit = 8192;
65 +    
66   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines