Hi
I'm using TIdTelnet client in my Firemonkey project to receive streaming text.
Works fine for iOS and Android, but on Windows I cannot detect when the client was disconnected by the server. If I try to send data on the closed socket I get 10054 error.
I tried OnDisconnect and OnStatus events, but it seems that those are not called when the socket is closed by the server.
How can I detect that the client was disconnected by the server on Windows ?
Thanks
Omar
(08-23-2022, 11:32 AM)omarreis Wrote: [ -> ]Works fine for iOS and Android
It
should be working the same way on all platforms.
(08-23-2022, 11:32 AM)omarreis Wrote: [ -> ]but on Windows I cannot detect when the client was disconnected by the server. If I try to send data on the closed socket I get 10054 error.
I tried OnDisconnect and OnStatus events, but it seems that those are not called when the socket is closed by the server.
The
OnDisconnect is fired only when your app calls
Disconnect() on the client.
If the server closes the connection
gracefully (sends a
FIN), then you
should be getting an
OnStatus(hsDisconnected) event fired after the
FIN is received.
If the connection is lost
abnormally, you are not going to get any event fired for that. The
IOHandler will just raise exceptions instead on any failed socket operations. Which, in the case of reading incoming data, that exception will just be swallowed by the internal thread that
TIdTelnet creates for itself. In which case, you might try assigning an
OnException event handler to the
TIdTelnet.TelnetThread property after
TIdTelnet.Connect() exits successfully.
I'll look into adding some additional internal handling to get an event fired on unexpected disconnects. Oh, wait... there is already an open ticket for that:
#118: TIdTelnet needs to trigger OnDisconnect or OnStatus when disconnected
Thanks rlebeau.
I followed what you said and it seems to work now
What I did was call IdTelnet.Disconnect from IdTelnet.TelnetThread.OnException.
This calls IdTelnet.OnDisconnected event in the case of abnormal socket disconnect,
so the application can respond to all kinds of disconnections (graceful or otherwise)
I also added a form variable fIsTelnetConnected that is updated on IdTelnet events
OnConnected and OnDisconneted, since IdTelnet.IOHandler.Connected can
return a false positive, as in the case of abnormal disconnect.
(08-24-2022, 12:15 PM)omarreis Wrote: [ -> ]What I did was call IdTelnet.Disconnect from IdTelnet.TelnetThread.OnException.
This calls IdTelnet.OnDisconnected event in the case of abnormal socket disconnect,
so the application can respond to all kinds of disconnections (graceful or otherwise)
The only thing to watch out for is the
TelnetThread is not created in a suspended state, so there is a
very small window of opportunity where the thread
could crash before you have have a chance to assign the
OnException handler to it.
(08-24-2022, 12:15 PM)omarreis Wrote: [ -> ]I also added a form variable fIsTelnetConnected that is updated on IdTelnet events
OnConnected and OnDisconneted, since IdTelnet.IOHandler.Connected can
return a false positive, as in the case of abnormal disconnect.
That is a good idea, since you shouldn't be using the
Connected() method anyway, especially across thread boundaries. Internally it performs a read on the socket, and that can corrupt communications if the thread also reads at the same time.