【问题标题】:message is not sent from server to client in single server - multiple client in java消息不会在单个服务器中从服务器发送到客户端 - java 中的多个客户端
【发布时间】:2014-10-17 15:18:43
【问题描述】:

我正在为 JAVA 中的单个服务器和多个客户端进行编码。 This is my code for serverThis is my code for client

更具体地说,我的服务器中有以下代码。

String Name;
    try {
         os.println("Enter Your Name:");
        Name=is.readLine();
        os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or type QUIT to Exit");
         line=is.readLine();
        while(line.compareTo("QUIT")!=0){

            os.println(line);
            os.flush();
            System.out.println("Response to Client  :  "+line);
            line=is.readLine();
        }   
    } catch (IOException e) {

        line=this.getName(); //reused String line for getting thread name
        System.out.println("IO Error/ Client "+line+" terminated abruptly");
    }
    catch(NullPointerException e){
        line=this.getName(); //reused String line for getting thread name
        System.out.println("Client "+line+" Closed");
    }

以及client.java中的以下代码

 try {
        s1=new Socket(address, 4445); // You can use static final constant PORT_NUM
        br= new BufferedReader(new InputStreamReader(System.in));
        is=new BufferedReader(new InputStreamReader(s1.getInputStream()));
        os= new PrintWriter(s1.getOutputStream());
    }
    catch (IOException e){
        e.printStackTrace();
        System.err.print("IO Exception");
    }

    System.out.println("Client Address : "+address);
    System.out.println("Enter Data to echo Server ( Enter QUIT to end):");

    String response=null;

    try{
         response=is.readLine();
         System.out.println(response);
        line=br.readLine(); 
         os.println(line);
         response=is.readLine();
        line=br.readLine();   
        while(line.compareTo("QUIT")!=0)
        {
            os.println(line);
            os.flush();
            response=is.readLine();
            System.out.println("Server Response : "+response);
            line=br.readLine(); 
        }

    }
    catch(IOException e){
        e.printStackTrace();
    System.out.println("Socket read Error");
    }

从这段代码中,我期待以下任务:

  1. 最初服务器和客户端之间已建立连接
  2. 将从服务器向客户端发送一条名为“请输入您的姓名”的消息
  3. 此消息将打印在客户端控制台中。
  4. 从客户端,一个名称将被发送到服务器。
  5. 然后服务器将执行以下行: os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or 输入 QUIT 退出");

  6. 但是在建立连接后,服务器没有向客户端发送类似“输入你的名字”这样的消息

为什么?

【问题讨论】:

    标签: java networking


    【解决方案1】:

    在 Server, LINE 68 之后添加以下行:

    • os.flush()

    客户端将显示预期的“输入您的姓名”。

    服务器和客户端都在等待输入,因此陷入僵局。 您必须调用 os.flush() 才能将数据实际发送到服务器。

    您的服务器代码写入输出但不发送:

    • 服务器,第 68 行:os.println("请输入您的姓名:");

    然后客户端挂了,因为服务器没有通过os.flush()在:

    • 客户端,第 37 行:响应 = is.readLine();

    当您的服务器也在等待客户端的回答时

    • 服务器,第 69 行:名称 = is.readLine();

    os.flush 命令的来源是这个示例代码: https://stackoverflow.com/a/5680427/3738721

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2017-01-12
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      相关资源
      最近更新 更多