Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

timeout not working by the time of reading data from channel (urgent help please ..)

 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If I set timeout by the time of reading data from socket channel, but it is not working ...
I set 30 seconds but it is not coming out after after 30 seconds ... it is taking more than 2 minutes ..
Why is timeout not working .....
Here is my code ...
SocketChannel channel = null;
ByteBuffer requestByteBuffer = null;
ByteBuffer buffer = null;
ByteBuffer responseByteBuffer = ByteBuffer.allocate(100);
InetSocketAddress isa = new InetSocketAddress("xx.xxx.xxx.xxx",yyyyy);
try {
channel = SocketChannel.open(isa);
connected = channel.isConnected();
}catch (java.io.IOException ioe) { }
try {
requestByteBuffer = ByteBuffer.wrap(request, 0, request.length);
channel.write(requestByteBuffer);

// set timeout
channel.setSoTimeout(30000);
responseByteBuffer.rewind();
// Read bytes from the channel
numRead = channel.read(responseByteBuffer);
}catch(Exception e) { System.out.println("unable to read data from channel"); }
finally{
if (channel != null) {
try {
channel.close(); // close the channel
} catch (Exception e) {throw new RuntimeException(e);}
}
}
Thanks.
 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can any one help me why setSoTimeout() is not working?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one problem is that the code as written wouldn't compile. SocketChannel doesn't have a setSoTimeout() method. There's one in Socket, however, so you could call
channel.socket().setSoTimeout(30000)
instead. Give that a try.
 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am sorry. I misstyped by the time of posting ..
My code in class is :
//set 30 seconds time out
channel.socket().setSoTimeout(30000)
This not working ...
If I use channel.setSoTimeOut(30000) it doesn't compile first ...
If I set timeout -1 then I am getting exception where as if I set 30 seconds it is not working ..it is taking more than 2 minutes. It is not coming out after 30 seconds ...
Any ideas please ...
Thanks.
 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
does any one know why setSoTimeout() not working in blocking mode of SocketChannel?
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic