import java.net.*;
import java.io.*;
import java.util.*;

public class DayTimeServer extends Thread {

   ServerSocket 	listen;
   static String 	hostName = "yps";
   int			port     = 4242;

   public DayTimeServer(int port)	{
   }

   public DayTimeServer(int port)	{
        try { 
            listen = new ServerSocket(port);
            System.out.println ("Listening on port: "
                + listen.getLocalPort());
        } catch(Exception e) {
            System.out.println(e);
        }
   }

    private void printMessage()	{
	System.out.println("-h		---->	help");
	System.out.println("[-host 		hostName");
	System.out.println(" -port 		port");
	System.out.println(" {-port 		port}");
	System.out.println("or ");
	System.out.println(" no argument");
   }
	
   /**
     * Parse the commandlind arguments and sets variables.
     */
   public void parseArgs(String args[]) {

	for (int i = 0; i < args.length; i ++) {
	   	if (args[i].equals("-h")) 
			printMessage();
	   	else if (args[i].equals("-host")) 
			hostName = args[++i];
	   	else if (args[i].equals("-port")) {
			port = new Integer(args[++i]).intValue();
			new DayTimeServer(port).start();
		}
	}
   }
   
   public void run()	{
        try {
            for(;;) {
                Socket clnt = listen.accept();
                System.out.println(clnt.toString());
                PrintWriter out = new PrintWriter
                    (clnt.getOutputStream (), true);
                out.println("It is now: " + new Date());
                clnt.close();
            }
        } catch(Exception e) {
            System.out.println(e);
	    e.printStackTrace();
        }
   }

    public static void main(String argv[]) {
	if ( argv.length == 0 )
		new DayTimeServer(0).start();
	else
		new DayTimeServer().parseArgs(argv);
    }
}

