How do I configure socket timeouts?

There are two options for socket timeout if you are using 1.4.x versions of the API, for 1.6.x only the later option applies as its now the default. 

1. Set a socket timeout on the SshTransport object you create before passing it into the API (SocketTransport extends Socket directly so you can call setSoTimeout as with any other socket).

When you set a socket timeout using setSoTimeout you must be prepared to handle the exceptions that are thrown by the API when a timeout is detected. The current default behaviour of the API does not do anything on timeouts, it simply throws an SshException with SOCKET_TIMEOUT reason.

2. Use the non-default (recently new and is default in 1.6.x) behaviour that manages socket timeouts by setting a few options on the Ssh2Context

((Ssh2Context)con.getContext(2)).setSocketTimeout(30000); 
((Ssh2Context)con.getContext(2)).setIdleConnectionTimeoutSeconds(120);

IdleConnectionTimeout is part of some new behaviour we recently added, but as of yet it is undocumented and only activated when you use the setSocketTimeout method of the Ssh2Context. 

Setting the value with setSocketTimeout in Ssh2Context rather than directly on the Socket using setSoTimeout will activate idle connection management. Set this to a reasonable value, say 30 seconds and set an idle connection of say 2 minutes. The client will be disconnected after 2 minutes.

You can also use the setSendIgnorePacketOnIdle property to force the transport to send a special packet to check the status of the connection. This will be sent every time the socket times out.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.