Should I use a single or threaded mode client?

Lee Painter

The Maverick API provides 2 modes of operation. This article discusses the merits of each.

Single Threaded Mode


When running an SSH client in single threaded mode you should be aware of some of the caveats involved. You create a client in single threaded mode when you pass a false value into the SshConnector.connect method for its third parameter 'buffered', or you use one of the method signatures that does not include the buffered parameter.

// Creates a single threaded mode client
SshClient ssh = con.connect(new SocketTransport("localhost", 22), "root");

SshClient ssh = con.connect(new SocketTransport("localhost", 22), "root", false);

 

In single threaded mode there is no background thread that is reading data from the socket. So the API relies on your calls to its methods to act as the reading thread. The main consequences of this are:

  • Channel InputStream available method will not work as expected.
  • ChannelEventListener will not work if all work is performed on your main thread.

Buffered/Threaded Mode


Buffered, or threaded mode clients have a background thread that is permanently reading from the connections socket. This allows data to be received external to your applications main thread. In addition, messages are also sent on a separate thread and this mode generally tends to improve performance of any intensive operations you may be performing. 

To create a buffered mode client you pass a value of true through to the SshConnector.connect method for its third parameters 'buffered'.

// Creates a buffered / threaded mode client
SshClient ssh = con.connect(new SocketTransport("localhost", 22), "root", true);


We generally recommend using the buffered mode client where possible.