import java.net.*;
import java.io.*;
public class DaytimeClient
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
String[] hostname = new String[]{"", "time.windows.com","time.nist.gov","time-nw.nist.gov","time-a.nist.gov","time-b.nist.gov"};
int i = 1;
boolean RecvFlag = false;
if(args.length > 0)
{
hostname[0] = args[0];
i = 0;
}
while(i <=5 && !RecvFlag)
{
try
{
Socket timeSocket = new Socket(hostname[i], 13);
InputStream timeStream = timeSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while((c=timeStream.read()) != -1)
time.append((char)c);
String timeString = time.toString().trim();
System.out.println("It is " + timeString +" at " + hostname[i] +" on port " + timeSocket.getPort());
RecvFlag = true;
timeSocket.close();
}
catch(UnknownHostException e)
{
System.out.println("Failed to get the information from " + hostname[i]);
System.out.println(e);
i++;
}
catch(Exception e)
{
System.out.println("Failed to get the information from " + hostname[i]);
System.out.println(e);
i++;
}
}
if(i == 6)
System.out.println("Failed to get the information please check your network settings.");
else
System.out.println("Complete.");
}
}