1 |
pjm2 |
1.1 |
import java.io.*; |
2 |
|
|
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 |
7 |
|
|
// |
8 |
|
|
// |
9 |
|
|
public class UDPReader { |
10 |
|
|
|
11 |
|
|
public static void main(String[] args) throws IOException { |
12 |
|
|
|
13 |
pjm2 |
1.2 |
// default UDP listening port. |
14 |
|
|
int port = 4589; |
15 |
|
|
final int packetSizeLimit = 8192; |
16 |
pjm2 |
1.1 |
|
17 |
pjm2 |
1.2 |
// 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 |
pjm2 |
1.3 |
DatagramSocket socket = null; |
29 |
|
|
try { |
30 |
|
|
socket = new DatagramSocket(port); |
31 |
|
|
} |
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 |
pjm2 |
1.2 |
System.out.println("UDPReader ready and listening for UDP packets on port "+port); |
39 |
|
|
|
40 |
pjm2 |
1.4 |
byte[] buf; |
41 |
|
|
UDPReaderThread t = new UDPReaderThread(); |
42 |
|
|
|
43 |
pjm2 |
1.2 |
boolean running = true; |
44 |
|
|
while (running){ |
45 |
pjm2 |
1.4 |
System.gc(); |
46 |
pjm2 |
1.2 |
try { |
47 |
pjm2 |
1.4 |
buf = new byte[packetSizeLimit]; |
48 |
pjm2 |
1.1 |
// receive request |
49 |
pjm2 |
1.2 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
50 |
|
|
socket.receive(packet); |
51 |
pjm2 |
1.4 |
t.run(packet); |
52 |
pjm2 |
1.2 |
} |
53 |
|
|
catch (IOException e) { |
54 |
|
|
System.out.println("An exception occured in the UDPReader!"); |
55 |
|
|
e.printStackTrace(); |
56 |
|
|
} |
57 |
pjm2 |
1.1 |
} |
58 |
|
|
socket.close(); |
59 |
|
|
} |
60 |
|
|
} |