JAVA NIO

R

RFleming

Hello,

Somewhat new to JAVA, and I swear I have looked all over on this
problem, and found an answer, but it does not seem to work. I have a
non blocking NIO socket client connection I can send data to the
socket check the input buffer for data and handle it okay. If no
activity occurs in say 10 seconds, I do an input read. From reading
documentation, I would expect a 0 return value for a proper connection
with no data, a -1 return value or IOexception for a bad socket
connection. However, what is happening is that the socket connection
is still open on both sides, the read times out and generates an
IOexception error described as Read timed out. Does the JAVA examples
only work when the server is also a NIO object? I say this because
the server I am using is not written in JAVA. Any help or suggestions
would be greatly appreciated!

Thanks

Ryan
 
R

RFleming

For others who might be having this problem, I have found a
workaround, I am performing an outputbuffer write and catching the
IOexception when the server side connection is gone, however, I would
still like to know if I could do the same with a read.

Thanks

Ryan
 
S

smoothop

For others who might be having this problem, I have found a
workaround, I am performing an outputbuffer write and catching the
IOexception when the server side connection is gone, however, I would
still like to know if I could do the same with a read.

Thanks

Ryan

ServerSocket server = new ServerSocket(port);
// timeout after 60 seconds
server.setSoTimeout(60000);
try {
Socket socket=server.accept();
}
catch ( java.io.InterruptedIOException e ) {
System.err.println( "Timed Out (60 sec)!" );
}

This is true for READ operation too. Since READ operation blocks as
long necessary it may be wise to use the setSoTimeout() method. Note
that when the TIMEOUT expires, an InterruptException is thrown.
However, the socket is still connected even though the Exception was
raised.

quote from http://www.rgagnon.com/javadetails/java-0086.html Maybe you
also make a try with timeOut parameters.
 
E

Esmond Pitt

From reading documentation, I would expect a 0 return value for a proper connection
with no data, a -1 return value or IOexception for a bad socket
connection.

Nope. You will only get the zero in non-blocking mode, and you will
never get -1 for a bad socket connection, only for one that has been
cleanly closed by the other end, and you're not very likely to get an
IOExeption either. You mostly get those when *writing*.

However, what is happening is that the socket connection
is still open on both sides, the read times out and generates an
IOexception error described as Read timed out.

That's correct behaviour if you set a timeout, which also implies that
you are in blocking mode. Usually NIO is used in non-blocking mode in
conjunction with Selector.select().
Does the JAVA examples only work when the server is also a NIO object?

No, it neither knows nor cares.
 
R

RFleming

Thanks for the reply. As you stated below, apparently I am still
using blocking mode. I thought that by checking the inputstrea
available method that I was doing the Java equivalent to non
blocking. Apparently I still have more learning to do (like it ever
stops)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Upload using java nio 10
NIO not so hot 15
NIO 12
Question about non-blocking NIO and Selection Keys 12
NIO UDP and TCP 5
Error with server 3
Java NIO server and Python asyncore client 2
NIO multiplexing + thread pooling 16

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top