EldoS | Feel safer!

Software components for data protection, secure storage and transfer

How can I know the socket is disconnected on TElSimpleSSHClient

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#36864
Posted: 06/02/2016 07:50:45
by Hejian Lin (Standard support level)
Joined: 04/21/2016
Posts: 5

Currently I am using the "Active" property to tell the connection state of TElSimpleSSHClient object. But from the documentation, it mentions that "However, if the connection is lost on the socket level, there is no way to find out that the connection has been closed, and Active property can still be True.". So the "Active" property shouldn't be a reliable way to tell the connection state.

Then I change my logic to handle the OnError event. I would say the socket is disconnected and need to be reconnect when the ErrorCode is :
ERROR_SSH_HOST_NOT_ALLOWED_TO_CONNECT
ERROR_SSH_CONNECTION_LOST
ERROR_SSH_APPLICATION_CLOSED
ERROR_SSH_NOT_CONNECTED
ERROR_SSH_CONNECTION_CANCELLED_BY_USER
ERROR_SSH_TCP_CONNECTION_FAILED

Am I doing the right thing? I need a very reliable way to tell me the connection is broken and do reconnect to the server.

Thanks
#36865
Posted: 06/02/2016 07:57:18
by Eugene Mayevski (EldoS Corp.)

The only proper way to discover socket disconnection is to attempt to send something to a socket. Without this you can't know that the socket has been disconnected. This is how TCP was designed to work.

Almost all SSH errors are fatal, so there's no need to check for ERROR_SSH_* constants. It makes sense to close the connection if you get any error, and then reconnect.

Another thing to care about is exceptions. If there's an exception thrown, your code needs to catch it, and also possibly reconnect.


Sincerely yours
Eugene Mayevski
#36866
Posted: 06/02/2016 08:03:37
by Hejian Lin (Standard support level)
Joined: 04/21/2016
Posts: 5

Thank you very much for your quick response.

Do you mean that I do need to handle "OnError" event as all the errors in this event are fatal and I should disconnect and then reconnect when this event is fired?

Furthermore, the exception you mentioned is the exception in my application or in "TElSimpleSSHClient" component?

Thanks
#36867
Posted: 06/02/2016 08:17:58
by Eugene Mayevski (EldoS Corp.)

Quote
jnlhj wrote:
Do you mean that I do need to handle "OnError" event as all the errors in this event are fatal and I should disconnect and then reconnect when this event is fired?


Yes, with the exception of authentication error, which means that just the particular chosen algorithm has failed to authenticate, all other errors (maybe with minor exceptions) are fatal and you should re-connect.

Quote
jnlhj wrote:
Furthermore, the exception you mentioned is the exception in my application or in "TElSimpleSSHClient" component?


I was talking about exceptions thrown by SecureBlackbox when you use SSH client's methods like Open, SendData, ReceiveData etc.


Sincerely yours
Eugene Mayevski
#36868
Posted: 06/02/2016 21:34:51
by Hejian Lin (Standard support level)
Joined: 04/21/2016
Posts: 5

I thinke I found the problem. But one more thing I am still not sure.

I found there are four error related with authentication:
ERROR_SSH_UNSUPPORTED_AUTH_TYPE
ERROR_SSH_INVALID_RSA_CHALLENGE
ERROR_SSH_AUTHENTICATION_FAILED
ERROR_SSH_NO_MORE_AUTH_METHODS_AVAILABLE

which one I should ignore the reconnection?

Thanks

Lin
#36871
Posted: 06/03/2016 02:58:47
by Ken Ivanov (EldoS Corp.)

Hi Lin,

All the above errors are fatal and lead to connection closure. In fact, if you use TElSimpleSSHClient, the component's Open() call will always throw an exception if the connection was unsuccessful, so you can easily rely on a try/catch construction around it to identify connection issues.

Ken
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

Topic viewed 1600 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!