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 |
< |
try { |
18 |
< |
byte[] buf = new byte[1024]; |
19 |
< |
// receive request |
20 |
< |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
21 |
< |
socket.receive(packet); |
22 |
< |
String dataIn = new String(packet.getData()); |
23 |
< |
System.out.println("Received: " + dataIn); |
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 |
< |
catch (IOException e) { |
28 |
< |
System.out.println("An exception occured!"); |
29 |
< |
e.printStackTrace(); |
27 |
> |
|
28 |
> |
DatagramSocket socket = new DatagramSocket(port); |
29 |
> |
System.out.println("UDPReader ready and listening for UDP packets on port "+port); |
30 |
> |
|
31 |
> |
boolean running = true; |
32 |
> |
while (running){ |
33 |
> |
try { |
34 |
> |
byte[] buf = new byte[packetSizeLimit]; |
35 |
> |
// receive request |
36 |
> |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
37 |
> |
socket.receive(packet); |
38 |
> |
UDPReaderThread thread = new UDPReaderThread(packet); |
39 |
> |
thread.run(); |
40 |
> |
} |
41 |
> |
catch (IOException e) { |
42 |
> |
System.out.println("An exception occured in the UDPReader!"); |
43 |
> |
e.printStackTrace(); |
44 |
> |
} |
45 |
|
} |
46 |
|
socket.close(); |
47 |
|
} |