Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > nio read timeout ?

Reply
Thread Tools

nio read timeout ?

 
 
dchev
Guest
Posts: n/a
 
      11-24-2004
>From the nio API docs, it appears to me that I should be able to set a
socket read timeout, but in practice, the read blocks without timing
out.
The old java.net APIs seem to work just fine.

I understand that I could use non-blocking mode and a Selector, but
that seemed overkill for my one little datagram exchange which runs in
its own thread.

Can anyone tell me why the following nio-based code does not timeout?
ie. Why does DatagramChannel object's socket().setSoTimeout(x) not
work?

Thanks!
-Dave

NIO-based snippet:
DatagramChannel channel = DatagramChannel.open();
ByteBuffer bbuf;
InetSocketAddress sa;
// skipping the part where sa and bbuf are initialized & prepared

channel.send(buffer, sa); // send a request
bbuf.clear();
channel.socket().setSoTimeout(1000); // set timeout to one second

sa = (InetSocketAddress) channel.receive(bbuf); // does NOT timeout


The following plain old java.net based code works fine
(so, yes, that's what I'm using. But I want to understand why the nio
code did not work)

DatagramSocket socket = new DatagramSocket();
// init/prepare byte[] cmd and InetSocketAddress sa
socket.send( new DatagramPacket(cmd, cmd.length, sa));
byte[] buffer = new byte[MAX_RX_SIZE];
DatagramPacket rxPkt = new DatagramPacket(buffer, buffer.length);
socket.setSoTimeout(1000);
socket.receive(rxPkt); // times out in one second as expected (if no
data)

 
Reply With Quote
 
 
 
 
dchev
Guest
Posts: n/a
 
      01-27-2005
hehe... well, I did find a better answer to this problem eventually (
Selector) but it was pretty funny to me for this reason:

1) my background was sockets interface with C, where I was accustomed
to using select() to receive responses.

2) learning java circa 1.1/1.2, it was a frustrating realization that
there was no select() and that I should put all receive handling in
separate threads (grrr)

3) circa 1.4, trying to move over to the nio channels (so that my
server socket accept() thread could be interrupted - grrr), I tried to
use the same model as in point 2, only to find that doesn't work and
the model in point 1 does again....

full circle for me!

someone could have at least told me to rtfm
I spent the last two months thinking very negatively of nio.
cheers,
-Dave

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between 'Read timeout' & 'Response timeout' & 'Connecttimeout' Eddieturbo@googlemail.com Java 7 04-24-2009 02:35 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM
NIO read() SocketChannel EOF End of Stream Fritz Bayer Java 16 07-27-2004 03:56 AM
NIO with timeouts != NIO? iksrazal Java 1 06-18-2004 02:28 PM
NIO Timeout Kam Java 0 04-01-2004 02:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57