• Post Reply Bookmark Topic Watch Topic
  • New Topic

SocketChannel question  RSS feed

 
Ahmed Basheer
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JavaNIO for my socket application. Earlier I was using input output streams. Inputstream has a method called "available" that, without blocking, returns the number os bytes available for reading from the socket. Is there any similar method which i can use with SocketChannel for reading the data from socket. Basically I wanted to know how much the tcpip buffer is getting full before read on the channel returns or simply finding it out how many bytes are available for reading.

I apprecaite your response.

Thanks,
Basheer
 
Manuel Palacio
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using non-blocking sockets, you can't rely on available() anymore. You have to call the read method in the SocketChannel with a byte buffer and check that you have read enough data: "read() cannot read any more bytes than are immediately available from the socket's input buffer".

As usual I'd recommend using a framework. And for writing high-performance network apps I'd suggest: http://directory.apache.org/subprojects/mina/getting_started.html

I've used it succesfully in a project that required non-blocking sockets.

/M
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!