Can the connection timeout be changed?

36 views
Skip to first unread message

Rennie Petersen

unread,
Oct 3, 2013, 8:18:56 PM10/3/13
to naga-...@googlegroups.com
I create a connection with code like this:

            _nioService = new NIOService();

            NIOSocket nioSocket = _nioService.openSocket(_ipAddress, _networkPort);

            _mSocketObserver = new MSocketObserver(_connectionId, _callbackHandler,
                    _runnableConnectionOpened, _runnableMessageReceived, _runnableConnectionBroken);
            nioSocket.listen(_mSocketObserver);

My experience indicates that if no connection is made this times out after 20 seconds.

So my question is, is it possible to change that to 10 seconds, to get faster feedback for the inability to get a connection?

Thanks,
Rennie

Christoffer Lernö

unread,
Oct 4, 2013, 3:08:55 AM10/4/13
to naga-...@googlegroups.com
On Friday, 4 October 2013 02:18:56 UTC+2, Rennie Petersen wrote:
My experience indicates that if no connection is made this times out after 20 seconds.

So my question is, is it possible to change that to 10 seconds, to get faster feedback for the inability to get a connection?

As far as I know it's pretty tricky to change that setting. There's nothing built into the underlying SocketChannel that allows tweaking the connection parameter.

The standard solution is to start a thread or similar which waits the desired delay time and then checks if the connection finished. If it did, then fine, otherwise it forces a close of the connection.

Using the bundled EventMachine is a way to do this:

EventMachine eventMachine = new EventMachine();
eventMachine.start();
final NIOSocket nioSocket = eventMachine.getNIOService().openSocket(ipAddress, networkPort);
final DelayedEvent timeoutEvent = eventMachine.executeLater(10000, new Runnable()
{
public void run()
{
System.out.println("Connection timeout!");
nioSocket.close();
}
}
nioSocket.listen(new SocketObserverAdapter()
{
public void connectionOpened(NIOSocket nioSocket)
{
timeoutEvent.cancel();
System.out.println("Connection opened ok.")
}
});



Rennie Petersen

unread,
Oct 4, 2013, 7:53:13 AM10/4/13
to naga-...@googlegroups.com
Thank you.
Reply all
Reply to author
Forward
0 new messages