Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中自动更新服务器端和客户端_Java_Sockets_Distributed System - Fatal编程技术网

如何在java中自动更新服务器端和客户端

如何在java中自动更新服务器端和客户端,java,sockets,distributed-system,Java,Sockets,Distributed System,我正在学习分布式系统的基础知识,目前我正在尝试在one服务器和one客户端之间创建一个简单而现实的messenger我的目的是在每个端点套接字端(服务器和客户端)自动更新文本(就像真正的“消息传递应用程序”)。换句话说,我希望在我写和“发送”消息时,它会自动出现在接收方。我现在看到的是这个模式: 我发送了一条消息(假设来自客户) 要在服务器端查看该消息,我需要首先回复(因为服务器的BufferedReader/客户端的PrintWriter只有在询问答案后才会被读取) 我的代码: public

我正在学习分布式系统的基础知识,目前我正在尝试在one服务器和one客户端之间创建一个简单而现实的messenger我的目的是在每个端点套接字端(服务器和客户端)自动更新文本(就像真正的“消息传递应用程序”)。换句话说,我希望在我写和“发送”消息时,它会自动出现在接收方。我现在看到的是这个模式:

  • 我发送了一条消息(假设来自客户)
  • 要在服务器端查看该消息,我需要首先回复(因为服务器的BufferedReader/客户端的PrintWriter只有在询问答案后才会被读取)
  • 我的代码:

    public class ClientSide {
        public static void main(String [] args){
            String host_name = args[0];
            int port_number = Integer.parseInt(args[1]);
    
            try {
                Socket s = new Socket(host_name, port_number);
    
                PrintWriter out =
                        new PrintWriter(s.getOutputStream(), true);
    
                BufferedReader in = 
                        new BufferedReader(
                            new InputStreamReader(s.getInputStream()));
    
                BufferedReader stdIn = 
                        new BufferedReader(
                            new InputStreamReader(System.in));
    
    
                String answer;
    
                while ((answer = stdIn.readLine()) != null) {
                    out.println(answer);
                    System.out.println("\nlocalhost said\n\t" + in.readLine());
                }
    
            } catch (IOException ex) {
                Logger.getLogger(ClientSide.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
    public class ServerSide {
        public static void main(String [] args){
            int port_number = Integer.parseInt(args[0]);
    
            try {
                ServerSocket ss = new ServerSocket(port_number);
                Socket tcp = ss.accept();
    
                PrintWriter out = 
                        new PrintWriter(tcp.getOutputStream(), true);
    
                BufferedReader in =
                        new BufferedReader(
                            new InputStreamReader(tcp.getInputStream()));
    
                BufferedReader stdIn = 
                        new BufferedReader(
                            new InputStreamReader(System.in));
    
                String answer;
                while ((answer = stdIn.readLine()) != null){
                    out.println(answer);
                    System.out.println("\nClient said\n\t" + in.readLine());
                }
    
            } catch (IOException ex) {
                Logger.getLogger(ServerSide.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    我该怎么做?它是否涉及这方面的先进知识?
    提前谢谢

    核心问题是您需要同时等待两个事件——要么是来自套接字的消息,要么是来自用户的输入

    您希望同时等待这两个消息——如果用户键入消息,您不希望被困在套接字中等待消息;也不必在网络上有新消息时等待用户消息

    要“等待”来自多个流的消息,您必须。我相信这是最正确的方法

    但是,如果要继续使用
    BufferedReader
    ,则有一个
    ready()
    方法,当且仅当有消息等待读取时,该方法才会返回
    true

    在和stdIn声明中的
    之后,您的代码看起来像(我没有测试它!!):

    一些有些有用的随机链接:


    这样的问题已经有了答案->而且Khanh Hua的答案看起来不错,请查看他的存储库
    while(true) {
        if(stdIn.ready()) {
            System.out.println("I said " + stdIn.readLine());
        }
        if(in.ready()) (
            System.out.println("He said " + in.readLine());
        }
    }