--- experimental/server/XMLReader/UDPReader.java 2000/11/17 11:23:19 1.1 +++ experimental/server/XMLReader/UDPReader.java 2000/11/17 15:25:48 1.3 @@ -10,19 +10,47 @@ public class UDPReader { public static void main(String[] args) throws IOException { - DatagramSocket socket = new DatagramSocket(4589); + // default UDP listening port. + int port = 4589; + final int packetSizeLimit = 8192; + // Allow the user to choose their own port number. + if (args.length == 1){ + try { + port = new Integer(args[0]).intValue(); + } + catch (Exception e){ + // If something went wrong, use the default again. + port = 4589; + } + } + + DatagramSocket socket = null; try { - byte[] buf = new byte[1024]; - // receive request - DatagramPacket packet = new DatagramPacket(buf, buf.length); - socket.receive(packet); - String dataIn = new String(packet.getData()); - System.out.println("Received: " + dataIn); + socket = new DatagramSocket(port); } - catch (IOException e) { - System.out.println("An exception occured!"); - e.printStackTrace(); + catch (BindException e){ + System.out.println("Some other process is already listening on port "+port+"."); + System.out.println("Please specify another port number on the command line."); + System.exit(0); + } + + System.out.println("UDPReader ready and listening for UDP packets on port "+port); + + boolean running = true; + while (running){ + try { + byte[] buf = new byte[packetSizeLimit]; + // receive request + DatagramPacket packet = new DatagramPacket(buf, buf.length); + socket.receive(packet); + UDPReaderThread thread = new UDPReaderThread(packet); + thread.run(); + } + catch (IOException e) { + System.out.println("An exception occured in the UDPReader!"); + e.printStackTrace(); + } } socket.close(); }