This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Sockets and Internet Protocols and the fly likes timeout not working by the time of reading data from channel (urgent help please ..) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "timeout not working by the time of reading data from channel (urgent help please ..)" Watch "timeout not working by the time of reading data from channel (urgent help please ..)" New topic
Author

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

kishoret tata
Greenhorn

Joined: Feb 15, 2004
Posts: 12
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

Joined: Feb 15, 2004
Posts: 12
Please can any one help me why setSoTimeout() is not working?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
kishoret tata
Greenhorn

Joined: Feb 15, 2004
Posts: 12
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

Joined: Feb 15, 2004
Posts: 12
Hi,
does any one know why setSoTimeout() not working in blocking mode of SocketChannel?
Thanks.
 
 
subject: timeout not working by the time of reading data from channel (urgent help please ..)