no output for java client/server app [closed]

a 夏天 提交于 2019-12-02 09:19:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!