• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sockets, NIO newbie questions

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am working with a ReadableByteChannel implementation like a SocketChannel, the read() method - according to the javadocs - will return a -1 to signal end of stream. What does "end of stream" actually mean ? Does it actually signify that the SocketChannel is closed or disconnected ? Come to think of it, what's the difference b/w closed and disconnected as applied to stream IO channels like SocketChannels ?

Thanks

Pho
 
J Kneeland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Think of the channel like a hose with a limited supply of water (Maybe from a tank). Your buffer would be a plastic bucket. The idea is to use a bucket to gather water instead of getting it in drops (Like streams). The read method will attempt to completely fill the buffer with data, but will also fill it part way. (Your tank may not have an even number of buckets worth of water) So, when ever you come to a partial buffer full of data, the method returns like normal and doesn't return -1. The only time it returns -1 is when there is no data at all. (Your tanks is currently empty, but may be filled later)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!