简体   繁体   English

消息未从服务器发送到单个服务器中的客户端-Java中的多个客户端

[英]message is not sent from server to client in single server - multiple client in java

I am coding for a single server and multiple client in JAVA . 我在JAVA中为单个服务器和多个客户端编码。 This is my code for server And This is my code for client . 这是我的服务器代码, 这是我的客户端代码

More specifically , I have the following code in my server . 更具体地说,我的服务器中包含以下代码。

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");
    }

ANd the following code in client.java 在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");
    }

From this code I am expecting the following task : 从此代码中,我期望执行以下任务:

  1. Initially A connection has been established between server and client 最初已在服务器和客户端之间建立连接
  2. A message will be sent from server to client named "Enter your name" 一条消息将从服务器发送到客户端,名称为“输入您的名字”
  3. This msg will be printed in clients console . 此消息将在客户端控制台中打印。
  4. From client , a name will be sent to server . 从客户端,名称将被发送到服务器。
  5. Then server will do the following line : os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or type QUIT to Exit"); 然后服务器将执行以下行: os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or键入QUIT退出”);

  6. But after establishment of connection , no message like " Enter your name " is sent from server to client 但是,建立连接后,没有从服务器向客户端发送诸如“输入您的名字”之类的消息

Why ? 为什么呢

Add the following line after Server, LINE 68: 在Server LINE 68之后添加以下行:

  • os.flush() os.flush()

the client will show the expected "Enter your name". 客户将显示预期的“输入您的姓名”。

Both server and client are waiting for input, thus sitting in a deadlock. 服务器和客户端都在等待输入,因此陷入了僵局。 You must call os.flush() to actually sed your data to the server. 您必须调用os.flush()才能将数据实际发送到服务器。

Your server code writes to output but doesn't send it: 您的服务器代码写入输出,但不发送:

  • Server, LINE 68: os.println("Enter Your Name:"); 服务器,第68行:os.println(“输入您的姓名:”);

Then the client hangs because the server didnt send it via os.flush() at: 然后客户端挂起,因为服务器没有通过os.flush()发送它:

  • Client, LINE 37: response = is.readLine(); 客户端,第37行:response = is.readLine();

while your server is also waiting for the clients answer at 而您的服务器也在等待客户端在

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

Source for the os.flush command was this sample code: https://stackoverflow.com/a/5680427/3738721 os.flush命令的源代码是以下示例代码: https ://stackoverflow.com/a/5680427/3738721

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM