有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程Java客户端服务器时钟

我有点被一个问题困住了。我用Java开发了一个客户端-服务器应用程序,多个客户端可以连接到一个服务器。现在我有一个循环操作,它获取当前时间(对应于服务器端的ClockTask)。但我真的不知道如何将这次的数据传输到所有连接的客户端。我想应该通过ObjectOutputStream以某种方式完成,但如果有人能给我提供线索就好了

以下是我的服务器代码,以及运行客户端连接的线程:

 public class Server {

 public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;

        boolean listeningSocket = true;
        try {
            serverSocket = new ServerSocket(11111);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 11111");
        }

        while(listeningSocket){
            System.out.println("Waiting for a client to connect...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected!");
            ConnectThread ct = new ConnectThread(clientSocket);
            ct.start();

        }
        serverSocket.close();       
    }
}

连接线程:

public class ConnectThread extends Thread{

private Socket socket = null;


public ConnectThread(Socket socket) {

super("ConnectThread");
this.socket = socket;

}

@Override
public void run(){
ObjectOutputStream serverOutputStream = null;
ObjectInputStream serverInputStream = null;
try {
    System.out.println("check");
    serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
    System.out.println("check");
    serverInputStream = new ObjectInputStream(socket.getInputStream());
    serverOutputStream.writeInt(42);
    System.out.println("check");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
finally{
    try {
        serverOutputStream.close();
        serverInputStream.close();              
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

}

客户:

public class Client {

public static void main(String[] arg) {


Socket socketConnection = null;
ObjectOutputStream clientOutputStream = null;
ObjectInputStream clientInputStream = null;
        try {

    socketConnection = new Socket("127.0.0.1", 11111);

    clientOutputStream = new ObjectOutputStream(
            socketConnection.getOutputStream());
    clientInputStream = new ObjectInputStream(
            socketConnection.getInputStream());
    System.out.println("check");
    System.out.println(clientInputStream.readInt()); // HERE'S WHERE THE EXCEPTION OCCURS

} catch (Exception e) {
    System.out.println("The following exception has occured and was caught:");
    System.out.println(e);
}

finally{
    try {
        clientOutputStream.close();
        clientInputStream.close();
        socketConnection.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
}

时钟任务:

public class ClockTask extends TimerTask {


@Override
public void run() {
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    Calendar c = Calendar.getInstance();
    System.out.println(dateFormat.format(c.getTime()));
    //some object output stream here??
}

}

共 (1) 个答案

  1. # 1 楼答案

    我不建议发送日历对象,因为它是一个非常昂贵的对象,使用大约2900字节。相反,您可以通过DataOutputStream发送一个长值,该流将使用8个字节

    注意:您需要更正客户端和服务器之间的延迟,否则时间将始终延迟

    解决这个问题的一个简单方法是,客户端向服务器发送一条带有时间戳的消息,只要服务器用自己的时间戳响应,就可以假设延迟是往返时间的一半。然后,您可以应用EWMA(指数加权移动平均)来获得服务器和客户端时钟差异的原因平均值