问题
thank u for all ur previous responses which are very helpful. I have another Q for server/client app. I gt the connection between server/client. But now when I type something, nothing happens. The run() that I implement listens to input and display them. It seems that this method is not working (thats why there no output I guess)
This is the run() (listen and display message received) and the send() (send message)
Thank you
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);
}
Some information that could be helpful: Those method is in class I call them by
someClass.start() (someClass extends Thread class)
someClass.send()
回答1:
It looks like you need to flush the output stream.
public void send(String msg){//send outgoing message
System.out.println("in the send()");
serverOut.println(msg);
serverOut.flush();
}
That's assuming whatever serverOut
is a PrintWriter
which it seems to be?
来源:https://stackoverflow.com/questions/13205530/no-output-for-java-client-server-app