Sockets, BufferedReader.readline() - why the stream is not ready?

感情迁移 提交于 2019-12-20 03:52:32

问题


i'm learning java and i faced some problems with sockets. I developed a simple client-server app - kind of knock-knock, it performs 4 steps:

  1. client sends some message to server
  2. server recieves them and saves to file
  3. server sends back to client some other messages
  4. client recieves them and also saves to file

Problem appears on step #4: client doesn't recieve messages and never gets out the loop:

while ((inStr = in.readLine()) != null) {
    writer.println(inStr);
}

where in is type of BufferedReader:

    try {
        socket = new Socket(ipAddress, 4444);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

On server side messages are sent:

    try {
        socket = srvSocket.accept();
        out = new PrintWriter(socket.getOutputStream(), true);          
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                
    } catch (IOException e) {
        e.printStackTrace();
    }

...

    out.println("test from server #1");
    out.println("test from server #2");

on client side i watched in.ready() - it returns false. On server side i watch out.checkError() - it returns true;

What am i doing wrong - why is the stream empty ?

Any help ia appreciated! :)


回答1:


You are using public PrintWriter(OutputStream out, boolean autoFlush) which will flush automatically on new line or println. It does not autoflush after every write. You have to flush after every write.

Here is javadoc for the autoFlush param of the constructor: A boolean; if true, the println, printf, or format methods will flush the output buffer




回答2:


This might/might not solve your problem. But try keeping everything within Try Catch block. For eg: your ServerSocket initialization, writer blocks etc. If some error occurs, you might not be able to use writer anyhow, so there is no point in initializing it.
 You might try writing to standard output stream for debugging instead of a file. Below code for Server/ Client is a minor variant of yours and its working.

Server:

    Socket socket;
    ServerSocket srvSocket;
    BufferedReader in;
    PrintWriter out;
    try {
        srvSocket=new ServerSocket(4444);
        socket = srvSocket.accept();
        out = new PrintWriter(socket.getOutputStream(), true);          
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println("test from server #1");
        out.println("test from server #2");
    } catch (IOException e) {
        e.printStackTrace();
    }

Client

    Socket socket;
    BufferedReader in;
    PrintWriter out;
    String inStr;
    try {
        socket = new Socket("127.0.0.1", 4444);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while ((inStr = in.readLine()) != null) {
            System.out.println(inStr);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


来源:https://stackoverflow.com/questions/8563529/sockets-bufferedreader-readline-why-the-stream-is-not-ready

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