remoteHosts.add (sa); } } // ————————————————————– public static

public TimeServer (int port) throws Exception { this.channel = DatagramChannel.open(); this.channel.socket().bind (new InetSocketAddress (port)); System.out.println (”Listening on port ” + port + ” for time requests”); } public void listen() throws Exception { // 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 received 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); } } // ————————————————————– public static void main (String [] argv) throws Exception { int port = DEFAULT_TIME_PORT; if (argv.length > 0) { port = Integer.parseInt (argv [0]); 120
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

Comments are closed.