posted 21 years ago
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.