Two Laptop Bag
The moose likes Sockets and Internet Protocols and the fly likes Sockets, NIO newbie questions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Sockets, NIO newbie questions" Watch "Sockets, NIO newbie questions" New topic
Author

Sockets, NIO newbie questions

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

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


Regards,

Pho
J Kneeland
Ranch Hand

Joined: Aug 15, 2004
Posts: 31


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)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Sockets, NIO newbie questions
 
Similar Threads
ServerSocketChannel.accept() seems to create a random port to send data to?
About ObjectOutputStream
does a nio socket buffer data internally too?
Creating a nonblocking stream
SocketChannel and OutputStream