Java 无法获取消息
解决Java 无法获取消息,java,Java,解决 现在可以从客户端和用户获取输出了客户端和服务器似乎都在等待一方向另一方发送消息。您的代码结构表明,建立连接后,服务器将等待来自客户端的消息 DataInputStream dis = new DataInputStream(s.getInputStream()); String Message = dis.readUTF(); History.setText(History.getText()+ '\n' + "Client:-"
现在可以从客户端和用户获取输出了客户端和服务器似乎都在等待一方向另一方发送消息。您的代码结构表明,建立连接后,服务器将等待来自客户端的消息
DataInputStream dis = new DataInputStream(s.getInputStream());
String Message = dis.readUTF();
History.setText(History.getText()+ '\n' + "Client:-" + Message);
System.out.println("Message from client :- "+ Message);
}
catch(IOException e) {
History.setText(History.getText()+ "\n" + "Error in Connection");
History.setText(History.getText()+ "\n" + "Please Try Again or Exit");
你的客户端也在等待服务器的消息
DataInputStream dis = new DataInputStream(s.getInputStream());
String Message = dis.readUTF();
History.setText(History.getText()+ '\n' + "Client:-" + Message);
System.out.println("Message from client :- "+ Message);
在等待传入消息之前,请删除这些代码行中的任何一行,或者让客户端或服务器向另一行发送消息。您的代码正在工作
在操作处理程序中,您将在发送消息之前清除消息
DataInputStream dis = new DataInputStream(s.getInputStream());
String Message = dis.readUTF();
History.setText(History.getText()+ '\n' + "Server:- " + Message);
删除客户端和服务器中的该行
您仍然有问题,因为只能接收第一条消息。对于初学者,我将添加登录IOException捕获块。可能某个地方抛出了异常。另一个观察-DataInputStreams和DataOutputStreams未关闭。您可以使用try with resources来实现这一点。什么是“他们无法接收和发送来自彼此的消息”呢?引发异常或服务器和客户端开始等待,但什么也没有发生?服务器和客户端成功连接,但当我单击“发送”按钮时,客户端和服务器都无法接收彼此发送的消息。
Msg.setText("");