0

The following code should detect that there are no more bytes to be read. However it simply hangs until client connection is closed.

Server

        //...

        StringBuilder builder = new StringBuilder(128);
        byte[] buffer = new byte[1024];
        int length = 0;

        while ((length = socket.getInputStream().read(buffer)) != -1) {
            System.out.println(length);
            builder.append(new String(buffer));
            System.out.println(builder.toString());
        }

Client

   //...

    OutputStream stream = clientSocket.getOutputStream();
    stream.write("LIST".getBytes());
    stream.flush();

The API and the previous answer on such question states that -1 should mean that there are no characters to be read, however this loop writes:

  • 4

  • LIST

  • hangs for 20 seconds until client closes socket and picks up -1 to terminate loop.

How could i properly detect that there are no more characters in the stream ?

Connection should remain open after this.

I could send number of bytes in advance or special character, but is it possible to detect that there is nothing to be read any longer even if connection is still active.

John
  • 5,189
  • 2
  • 38
  • 62

1 Answers1

4

The API and the previous answer on such question states that -1 should mean that there are no characters to be read

I can't comment on what some uncited answer might say, but the API documentation says no such thing. It says -1 is returned at end of stream. End of stream on a socket occurs when the peer closes the connection, or shuts it down for output, and not before.

-1 is not an end of message indicator.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • so there is no such thing as to detect there are no characters to be read without blocking or having custom protocol ? – John Oct 22 '15 at 21:07
  • @John That is correct. If you want messages, you have to implement them yourself: lines, length-word prefixes, self-describing protocols like XML, XDR, ... – user207421 Oct 22 '15 at 21:08
  • 1
    What about `available()` (I am just curious)? – vlp Oct 22 '15 at 22:18
  • 1
    @vlp It returns the number of bytes that can be read without blocking, i.e. the number of bytes in the socket receive buffer plus whatever is in any intermediate `BufferedInputStreams.` It doesn't have anything to do with messages. There are no messages in TCP for anything to have anything to do with. It's a byte stream protocol. – user207421 Oct 22 '15 at 22:36
  • I read the question as _how to detect that there are "no more bytes to be read"_ (not _how to detect that connection was closed_). The "Connection should remain open after this." part implies that. I think I basically understand the stream nature of TCP (even have [an answer](http://stackoverflow.com/a/32029415/5128464) here ;) ). Thank you. – vlp Oct 22 '15 at 23:07
  • @vlp The title of the question is 'how to detect end of socket input stream', but I've answered both questions and cleared up the OP's confusion. Your other answer states 'difficult' where it should say 'impossible', and the part about the PSH flag is nonsense. It applies to the current segment only, and in any case it is basically obsolete. – user207421 Oct 22 '15 at 23:16
  • Ad "impossible": If _you are_ the TCP stack (quite usual in embedded systems), than _you_ divide the data into TCP packets (i.e. you are the one who writes the headers). Ad "nonsense": Not PSH flag, but _push function_ (see [here](https://tools.ietf.org/html/rfc793#page-4)). – vlp Oct 22 '15 at 23:23
  • There is no distiction between the 'push function' and the PSH flag. See page 16: 'PSH: Push Function'. But I'm not going to debate your other answer further here. It's pointless. I've commented there. – user207421 Oct 22 '15 at 23:35