it's not my first time trying to understand this issue but i hope it will be the last one:

**some background:**

i have a `Java SocketChannel NIO` server working in non-blocking mode.

this server has multiple clients which send and receive messages from it.

each client maintain its connection to the server with `"keepalive"` messages every once in a while.
The main idea with the server is that the clients will remain connect "all the time" and receive messages from it in "push" mode.

**now to my question:**

in Java NIO `read()` function - when the read() return -1 - it means that its EOS.

in the question i've asked [here](https://stackoverflow.com/questions/14037721/java-nio-read-end-of-stream) i realized that it means that the socket has finished its current stream and doesn't need to be closed..

when searching in google a bit more about this i found out that it does mean that the connection is closed on the other side..

1. what does the word "stream" exactly means? is it the current message being sent from the client? is it the ability of the client side connection to send anymore messages ?
2. why would a `SocketChannel` be closed on the client side if the client never told him to be closed ?
3. what is the difference between `read()` return -1 and connection reset by peer I/O error ?

this is how i read from `SocketChannel`:

```
private JSONObject readIncomingData(SocketChannel socketChannel)
        throws JSONException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {
    JSONObject returnObject = null;
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    Charset charset = Charset.forName("UTF-8");
    String endOfMesesage = "\"}";
    String message = "";
    StringBuilder input = new StringBuilder();      
    boolean continueReading = true;
    while (continueReading && socketChannel.isOpen()) 
    {
        buffer.clear();         
        int bytesRead = socketChannel.read(buffer);             
        if (bytesRead == -1)
        {
            continueReading = false;                    
            continue;
        }
        buffer.flip();
        input.append(charset.decode(buffer));
        message = input.toString();
        if (message.contains(endOfMesesage))
            continueReading = false;
    }       
    if (input.length() > 0 && message.contains(endOfMesesage))
    {
        JSONObject messageJson = new JSONObject(input.toString());
        returnObject = new JSONObject(encrypter.decrypt(messageJson.getString("m")));
    }           
    return returnObject;
}

```