62,616
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.net.*;
public class SimpleDaytimeClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String sHostName;
/*
* Get the name of the server from the command line. No entry,use
* tock.usno.navy.mil
*/
if(args.length>0){
sHostName = args[0];
}
else{
sHostName = "tock.usno.navy.mil";
}
/*
* Opeb a socket to port 13. Prepare to receive the Daytime information.
*/
try{
Socket oSocket = new Socket(sHostName,13);
InputStream oTimeStream =oSocket.getInputStream();
StringBuffer oTime = new StringBuffer();
// Fetch the Daytime information.
int iCharacter;
while((iCharacter = oTimeStream.read()) != -1){
oTime.append((char)iCharacter);
}
// Convert Daytime to a String and output.
String sTime = oTime.toString().trim();
System.out.println("It's" + sTime + "at" + sHostName + ".");
oTimeStream.close();
oSocket.close();
}catch (UnknownHostException e){
System.err.print(e);
}catch (IOException e){
System.err.print(e);
}
}
}