感谢您之前的所有回复,这些回复非常有帮助。 我还有另一个关于服务器/客户端应用程序的问题。我知道服务器/客户端之间的连接。 但现在当我输入一些东西时,什么也没有发生。我实现的 run() 监听输入并显示它们。看来这个方法不起作用(这就是为什么我猜没有输出)
这是 run() (监听并显示收到的消息)和 send() (发送消息)
谢谢
public void run(){//watch for incoming communication
String msg;
try{//loop reading lines from the client and display msg
while ((msg = serverIn.readLine()) != null) {
System.out.println("msg received"+msg);
}
}catch (IOException e) {
System.err.println(e);
}
}
public void send(String msg){//send outgoing message
System.out.println("in the send()");
serverOut.println(msg);
}
一些可能有用的信息: 这些方法在类中我通过
调用它们someClass.start() (someClass extends Thread class)
someClass.send()
最佳答案
看来您需要刷新输出流。
public void send(String msg){//send outgoing message
System.out.println("in the send()");
serverOut.println(msg);
serverOut.flush();
}
假设 serverOut
是一个 PrintWriter
,它看起来就是这样?
关于java - java客户端/服务器应用程序没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205530/