2

I'm using Java NIO in to accept, read and write in my server.

In the documentation it said that the SocketChannel.read() function will return the number of bytes read from socket, and -1 if it reach end of stream.

Now - i don't really understand what does "End Of Stream" mean? is it as same as borken pipe error ?

Does it mean that the connection is lost for both side and i should close the SocketChannel ?

I would be really great full if someone can explain more about it - and give some example of using and behaving correctly according to it..

Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154
  • 1
    End of stream means that there is nothing more to read. I.e. you have read all the data in the stream (end of file). – svz Dec 26 '12 at 08:09
  • doesn't it what int read = read() ---> read = 0 means ? that it read nothing ? and there is nothing to read ? – Asaf Nevo Dec 26 '12 at 08:14
  • 1
    It means, all data have been read from stream, but stream can be alive . – Sergii Zagriichuk Dec 26 '12 at 08:16
  • 1
    so to sum things up - when the connection got lost during the read() - the indication for it is the Exception and the return from read() – Asaf Nevo Dec 26 '12 at 08:20

1 Answers1

4

End Of Stream means there is nothing more to read because the other end has closed the stream.

The value is 0 is not nothing, it is a zero.

You will get an exception if the connection is lost unexpectedly. I.e. the other end had not closed the connection gracefully.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130