[英]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 : 从此代码中,我期望执行以下任务:
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退出”);
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之后添加以下行:
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: 您的服务器代码写入输出,但不发送:
Then the client hangs because the server didnt send it via os.flush() at: 然后客户端挂起,因为服务器没有通过os.flush()发送它:
while your server is also waiting for the clients answer at 而您的服务器也在等待客户端在
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.