编写一个客户端应用程序,在它打印新值之后将机器时间设置为给定主机 约会该应用程序使用TCP端口37中可用的主机服务的标准日期。 参数是主机名和端口号。端口号的默认值是37.如果是 程序运行时没有参数,它与localhost通信。程序使用标准 输出到打印信息。 重要的提示: dateAtHost服务给出一个带符号的32位整数,其自1月1日起的秒数, 1990年。 DateAtHost服务器 编写一个侦听给定TCP端口的服务器应用程序,它以32位整数发送日期 格式化到客户端,如主机服务的标准日期。程序参数是端口号。 端口号的默认值为37.程序使用标准输出来打印信息。
DateAthostclient.java
package DateAthostclient;
import java.net.*;
import java.io.*;
import java.util.*;
public class DateAthostclient {
static final int defaultPort = 37;
static final long offset = 2208988800L;
public static void main(String[] args) {
// TODO code application logic here
int portNumber;
Socket clientSocket;
DataInputStream timeStream;
String hostName;
switch(args.length) {
case 1: hostName = args[0];
portNumber = defaultPort;//dateAtHost Port;
break;
case 2: hostName = args[0];
portNumber = new Integer(args[1]).intValue();
break;
default:
hostName = "localhost";
portNumber = defaultPort;//dateAtHost Port;
}
try {
clientSocket = new Socket(hostName,portNumber);
timeStream = new DataInputStream(clientSocket.getInputStream());
int dateAtHost = timeStream.readInt() + (int)(1L<<32);
new Date().setTime((dateAtHost-offset)*1000);
System.out.println("It is " + new Date().toString() + " at " +hostName);
timeStream.close();
clientSocket.close();
}
catch (UnknownHostException {
System.err.println(" Unknown host error");
}
catch (ConnectException e) {
System.out.println(" Service unavailable on port "+portNumber+"of host "+hostName);
}
catch (IOException e) {
System.err.println(" Communication error occured\r\n "+e);
}
}
}
dayAtHostserver.java
package dayAtHostserver;
import java.net.*;
import java.io.*;
import java.util.*;
public class dayAtHostserver {
public final static int daytimePort = 13;
public static void main(String[] args) {
// TODO code application logic here
ServerSocket theServerSocket;
Socket theConnectionSocket;
PrintWriter out;
try {
theServerSocket = new ServerSocket(daytimePort);
System.out.println("Timeserver ready at port "+daytimePort);
try {
while (true) {
theConnectionSocket = theServerSocket.accept();
System.out.println("Request arrived!");
out = new PrintWriter(theConnectionSocket.getOutputStream(),true);
out.println(new Date());
theConnectionSocket.close();
}
}
catch (IOException e) {
theServerSocket.close();
System.err.println(e);
}
}
catch (IOException e) {
System.err.println(e);
}
}
}
我有两个问题 1)我需要了解什么是(1L&lt; 32)以及为什么他在这里使用它? 2)当我运行服务器并且客户端日期未在客户端中收到时