JAVA NIO  
Author Message
RFleming





PostPosted: 2007-6-16 21:58:00 Top

java-programmer, JAVA NIO 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

 
mrferrier





PostPosted: 2004-6-9 12:48:00 Top

java-programmer >> JAVA NIO Did you ever find a solution to this probelem? I'm getting repeated
occurances of the same exception, using Java's Nio library. I'm not doing
anything fancy, just an online game which is a glorified client/server
chat system. Any ideas? Thanks!

 
Andrew Thompson





PostPosted: 2004-6-9 16:06:00 Top

java-programmer >> JAVA NIO On Wed, 09 Jun 2004 00:48:26 -0400, mrferrier wrote:

> Did you ever find a solution to this probelem? I'm getting repeated
> occurances of the same exception,

That would be an ItMightBeThisItMightBeThatException
exception?

<http://www.physci.org/codes/javafaq.jsp#exact>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
rgrzywinski





PostPosted: 2004-7-15 0:46:00 Top

java-programmer >> JAVA NIO This will happen on windows, typically with a Pipe (which is used in the
Selector implementation). It's cause is shown here:

http://www.smartftp.com/support/kb/index.php/74

Translated into NIO: if you close the source of a Pipe and attempt to
write to it, this will occur.

 
 
rgrzywinski





PostPosted: 2004-7-15 1:10:00 Top

java-programmer >> JAVA NIO I should also mention that this will occur if you have a connection over
the loopback, you close that connection, and then attempt to write to the
connection. Essentially, it's the Pipe case, but without the pipe.

 
 
RFleming





PostPosted: 2007-6-16 22:37:00 Top

java-programmer >> JAVA NIO 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


 
 
smoothop





PostPosted: 2007-6-17 5:01:00 Top

java-programmer >> JAVA NIO On Jun 16, 5:36 pm, email***@***.com wrote:
> 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.

 
 
Esmond Pitt





PostPosted: 2007-6-17 16:18:00 Top

java-programmer >> JAVA NIO email***@***.com wrote:
> 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.
 
 
RFleming





PostPosted: 2007-6-17 19:08:00 Top

java-programmer >> JAVA NIO 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)!

On Jun 17, 4:17 am, Esmond Pitt <email***@***.com>
wrote:

> 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().
>