remoteHosts.add (sa); } } // ————————————————————– public static
remoteHosts.add (sa); } } // ————————————————————– public static void main (String [] argv) throws Exception { TimeClient client = new TimeClient (argv); client.sendRequests(); client.getReplies(); } } The program in Example 3-10 is an RFC 868 time server. This code answers requests from the client in Example 3-9 and shows how a DatagramChannel binds to a well-known port and then listens for requests from clients. This time server listens only for datagram (UDP) requests. The rdate command available on most Unix and Linux systems uses TCP to connect to an RFC 868 time service. Example 3-10. DatagramChannel time server package com.ronsoft.books.nio.channels; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.DatagramChannel; import java.net.SocketAddress; import java.net.InetSocketAddress; import java.net.SocketException; /** * Provide RFC 868 time service (http://www.ietf.org/rfc/rfc0868.txt). * This code implements an RFC 868 listener to provide time * service. The defined port for time service is 37. On most * unix systems, root privilege is required to bind to ports * below 1024. You can either run this code as root or * provide another port number on the command line. Use * “-p port#” with TimeClient if you choose an alternate port. * * Note: The familiar rdate command on unix will probably not work * with this server. Most versions of rdate use TCP rather than UDP * to request the time. * * @author Ron Hitchens (ron@ronsoft.com) */ public class TimeServer{ private static final int DEFAULT_TIME_PORT = 37; private static final long DIFF_1900 = 2208988800L; protected DatagramChannel channel; 119
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services