Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to increase connection timeout when using SocketChannel

843790Jun 22 2006 — edited Jun 24 2006
Hi guys,

I am writing an application that will have upwards of 500 simultaneous connections. Because of this I would like to use the nonblocking NIO rather than the plain IO with hundreds of threads.

My problem is that I need to be able to adjust the connection timeout for the SocketChannel connections I am making in order to determine weather a server exists at a particular address. I have no problems limiting the timeout to a certain value (using TimerTasks) however I would like to know how to actually increase the timeout. It seems that when I try to connect to an ip address that doesn't have a server, the connection will time itself out after 21 seconds. This behaviour has been verified on both windows and linux. Below is some sample code that will demonstrate this timeout.
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class NIOTest {


	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		try {
			Selector selector = Selector.open();

			SocketChannel s = SocketChannel.open();
			s.configureBlocking(false);
			s.register(selector, SelectionKey.OP_CONNECT);
			s.connect(new InetSocketAddress("xxx.xxx.xxx.xxx", 80)); //If the address given doesn't have a server then the finishConnect method below with throw the timeout exception

			while (true) {
				selector.select();
				Iterator it = selector.selectedKeys().iterator();
				while (it.hasNext()) {
					SelectionKey key = (SelectionKey) it.next();
					if ((key.readyOps() & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) {
						SocketChannel sc = (SocketChannel) key.channel();
						boolean retVal = sc.finishConnect(); //This throws an exception if there was no server at the given address
						System.out.println(retVal);
					}

					it.remove();
				}

			}

		} catch (Exception e) {
			System.err.println("Timeout occured after " + (System.currentTimeMillis() - startTime) + " ms.");
			e.printStackTrace();
		}
	}

}
And here is the exception that gets thrown on windows
Timeout occured after 21203 ms.
java.net.ConnectException: Connection timed out: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:521)
	at NIOTest.main(NIOTest.java:32)
If anyone has any ideas on how to increase the timeout, I would be very grateful.

Thanks in advance
Nick
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 22 2006
Added on Jun 22 2006
3 comments
2,212 views