Should I use a single or threaded mode client?

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.

Have more questions? Submit a request

3 Comments

  • 0
    Avatar
    Tim Broberg

    Does the use of single-threaded mode imply that all SSH traffic comes to a halt every time one of those big heavy Diffie-Hellman key exchanges happens?

  • 0
    Avatar
    Lee Painter

    Traffic always stops during key exchange because the protocol states that only certain transport level messages can be sent/received during key exchange. Any channel data messages are queued until key exchange is complete, in both thread modes.

    The protocol deals with this in section 7.1

    Once a party has sent a SSH_MSG_KEXINIT message for key exchange or
       re-exchange, until it has sent a SSH_MSG_NEWKEYS message (Section
       7.3), it MUST NOT send any messages other than:
    
       o  Transport layer generic messages (1 to 19) (but
          SSH_MSG_SERVICE_REQUEST and SSH_MSG_SERVICE_ACCEPT MUST NOT be
          sent);
    
       o  Algorithm negotiation messages (20 to 29) (but further
          SSH_MSG_KEXINIT messages MUST NOT be sent);
    
       o  Specific key exchange method messages (30 to 49).
  • 0
    Avatar
    Jim Freeman

    Tim B's question (he's my co-worker) was really asking if when in single-threaded mode, do all other connections block while one connection is doing say a Diffie-Hellman key exchange.

    In Buffered mode, is there a single background thread servicing all connections, or one background thread per connection? Is there documentation on the threading model of the product, i.e. background threads usage, and thread safety rules for classes and methods?  I'm new to this library, and have not read all javadocs in detail yet, not sure if it's discussed in javadocs somewhere.

Please sign in to leave a comment.