FileDocCategorySizeDatePackage
DaytimeServer.javaAPI DocExample1714Sun Feb 08 21:33:56 GMT 2004None

DaytimeServer

public class DaytimeServer extends Object
DaytimeServer - server for the standard binary time service.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: DaytimeServer.java,v 1.4 2004/02/09 03:33:55 ian Exp $

Fields Summary
ServerSocket
sock
Our server-side rendezvous socket
public static final int
PORT
The port number to use by default
Constructors Summary
public DaytimeServer(int port)
Construct a DaytimeServer on the given port number

		try {
			sock = new ServerSocket(port);
		} catch (IOException e) {
			System.err.println("I/O error in setup\n" + e);
			System.exit(1);
		}
	
Methods Summary
public static voidmain(java.lang.String[] argv)
main: construct and run


	     
	     
		new DaytimeServer(PORT).runService();
	
protected voidrunService()
This handles the connections

		Socket ios = null;
		DataOutputStream os = null;
		while (true) {
			try {
				System.out.println("Waiting for connection on port " + PORT);
				ios = sock.accept();
				System.err.println("Accepted from " +
					ios.getInetAddress().getHostName());
				os = new DataOutputStream(ios.getOutputStream());
				long time = System.currentTimeMillis();

				time /= DaytimeBinary.MSEC;	// Daytime Protocol is in seconds

				// Convert to Java time base.
				time += DaytimeBinary.BASE_DIFF;

				// Write it, truncating cast to int since it is using
				// the Internet Daytime protocol which uses 4 bytes.
				// This will fail in the year 2038, along with all
				// 32-bit timekeeping systems based from 1970.
				// Remember, you read about the Y2038 crisis here first!
				os.writeInt((int)time);
				os.close();
			} catch (IOException e) {
				System.err.println(e);
			}
		}