String target = "time.nist.gov";
if (argv.length > 0) {
target = argv [0];
}
ByteBuffer buffer = ByteBuffer.allocate (4);
buffer.flip(); // make it empty (RFC 868)
DatagramChannel channel = DatagramChannel.open();;
channel.configureBlocking (false);
channel.send (buffer, new InetSocketAddress (target, 37));
System.out.println ("Sent one Datagram to " + target);
// would potentially loop forever
for (int i = 0; i < 10; i++) {
buffer.clear();
buffer.putInt (0, 0);
SocketAddress sa = channel.receive (buffer);
if (sa == null) {
System.out.println ("no datagram ready, "
+ "sleeping one second");
Thread.sleep (1000);
} else {
int value = buffer.getInt (0);
buffer.flip();
System.out.println ("Received datagram from "
+ sa + ": " + buffer + ", value="
+ Integer.toHexString (value));
}
}