客户端无法在 Java 中从服务器获取日期

Client cannot get date from Server in Java

我正在编写代码以获取从服务器到客户端的日期。但是当我 运行 时,结果是 null.
我不明白为什么,而且我在从服务器到客户端的格式化日期方面也遇到了麻烦。

我可以同步时钟,但我不能同步日期。
我需要帮助:我想从服务器同步客户端上的日期和时钟

服务器:

public class ServerDemo{
public static void main(String []args) throws IOException{
    ServerSocket s = new ServerSocket(50555);
    int server_timeout = 10000;
    s.setSoTimeout(server_timeout);
    displayBanner();
    System.out.println("|\n|-Server is running...");

    try{
        while(true){
            Socket  s1 = s.accept();
            System.out.println("|\n|-Connection Received : " + s1);
            BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
            
            SimpleDateFormat outDate = new SimpleDateFormat("dd/MM/yyyy");

            //DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
            
            Date dt = new Date();
            System.out.println("    |\n    |-Sent to client :  " + dt);
            dos.write(outDate.format(dt) + " ");
            //dos.writeChars(timeFormat.format(dt) + '\n');

            System.out.println("    |\n    |-Connection Terminated : " + s1);
            s1.close();
            dos.close();
            
        }
    } catch(SocketTimeoutException e){
        System.out.println("|\nTerminating server due to timeout...");
        System.out.println("Server is terminated!");
        s.close();
    }
    
}


    System.out.println("[+]--Clock Synchronization Server v1.0.1--- initializing...");
}

客户:

public class ClientDemo{
public static void main(String []args) throws Exception{
    
    Socket s = new Socket("192.168.1.190", 50555);

    BufferedReader InFromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter OutToServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    
    String date = InFromServer.readLine();
    System.out.println(date);
    
    /*
    DateFormat Format = new SimpleDateFormat("dd/MM/yyyy");
    //parse the data from the server
    Date serverDate = Format.parse(date);
    //format the server for the Runtime commands
    DateFormat newFormatDate = new SimpleDateFormat("dd-MM-yy");
    String sysdate = "" + newFormatDate.format(serverDate);
    System.out.println(sysdate);
    */
    
    InFromServer.close();
    s.close();
    
}

dos.flush(); 是关键。这是工作 类(略有更改):

服务器端:

public class ServerDemo {

    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(50555);
        int server_timeout = 30000;
        s.setSoTimeout(server_timeout);
        System.out.println("|\n|-Server is running...");

        try {
            while (true) {
                Socket s1 = s.accept();
                System.out.println("|\n|-Connection Received : " + s1);
                BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));

                SimpleDateFormat outDate = new SimpleDateFormat("dd/MM/yyyy");

                Date dt = new Date();
                System.out.println("    |\n    |-Sent to client :  " + dt);
                dos.write(outDate.format(dt) + " ");
                dos.flush(); //<- this is essential
                dos.close();
                s1.close();
            }
        } catch (SocketTimeoutException e) {
            System.out.println("|\nTerminating server due to timeout...");
            System.out.println("Server is terminated!");
            s.close();
        }
    }
}

客户端:

package serverClient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientDemo {

    public static void main(String[] args) throws Exception {
        Socket s = new Socket("127.0.0.1", 50555);
        BufferedReader InFromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String date = InFromServer.readLine();
        System.out.println(date);
        InFromServer.close();
        s.close();
    }
}