int port = DEFAULT_PORT;
byte[] buffer = new byte[MAX_PACKET_SIZE];
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
}
try {
DatagramSocket server = new DatagramSocket(port);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
try {
server.receive(packet);
String s = new String(packet.getData(), 0, packet.getLength());
System.out.println(packet.getAddress() + " at port "
+ packet.getPort() + " says " + s);
// reset the length for the next packet
packet.setLength(buffer.length);
}
catch (IOException e) {
System.err.println(e);
}
} // end while
} // end try
catch (SocketException se) {
System.err.println(se);
} // end catch