// allocate a buffer to hold a long value
ByteBuffer longBuffer = ByteBuffer.allocate (8);
// assure big-endian (network) byte order
longBuffer.order (ByteOrder.BIG_ENDIAN);
// zero the whole buffer to be sure
longBuffer.putLong (0, 0);
// position to first byte of the low-order 32 bits
longBuffer.position (4);
// slice the buffer, gives view of the low-order 32 bits
ByteBuffer buffer = longBuffer.slice();
while (true) {
buffer.clear();
SocketAddress sa = this.channel.receive (buffer);
if (sa == null) {
continue; // defensive programming
}
// ignore content of recived datagram per rfc 868
System.out.println ("Time request from " + sa);
buffer.clear(); // sets pos/limit correctly
// set 64-bit value, slice buffer sees low 32 bits
longBuffer.putLong (0,
(System.currentTimeMillis() / 1000) + DIFF_1900);
this.channel.send (buffer, sa);
}