0

How disconnect TidTcpClient from TIdTcpServer properly?

i mean if i call Disconnect on client side i receive 10054 connection reset by peer on server onexception event.

if i call disconnect on AContext (using queue / OnExecute event) server return connection closed gracefully on onexception event.

My currently protocol don't use OnConnect and OnDisconnect events, only OnExecute with a IdTCPServer1.ContextClass := TClientContext;

I mean it because when i return number of connected clients using Contexts list always return a wrong number, so now i use:

InterlockedIncrement(FClientCount);
TThread.Synchronize(nil, UpdateUI);

Increase on OnConnect event and decrease on OnDisconnect event. Since on OnException call OnDisconnect, decrease number too.

Maybe if TIdTcpClient disconnect correctly count will work perfect using Contexts List no?

  • "*i mean if i call Disconnect on client side i receive `10054 connection reset by peer` on server onexception event*" - it shouldn't be causing a connection reset, that implies an ungraceful closure, but `Disconnect()` actually does use a graceful closure. Might be a firewall or networking issue instead. But even so, it is still a disconnect, so just let it happen on the serve side, the server will still handle it and close the socket. You can simply ignore that error code in your `OnException` code if needed. – Remy Lebeau Nov 05 '18 at 19:57
  • "*when i return number of connected clients using Contexts list always return a wrong number*" - then you are doing something wrong in your code to block the server from terminating its internal client threads correctly. Please show your actual server code. Whether the connections are being closed cleanly or not, once the server detects the connection is gone, the next I/O operation on that socket will raise an exception, and if you let the server handle that exception, it will terminate the calling thread. Let it. – Remy Lebeau Nov 05 '18 at 19:59
  • "*Increase on OnConnect event and decrease on OnDisconnect event. Since on OnException call OnDisconnect, decrease number too."* - that just goes to prove that you are doing something in your code that blocks the sever from terminating its threads correctly, as the `OnDisconnect` event is triggered during thread cleanup after it is signaled for termination. – Remy Lebeau Nov 05 '18 at 20:01
  • 1
    "*Maybe if TIdTcpClient disconnect correctly count will work perfect using Contexts List no?*" - No, the issue is that you clearly have a flaw somewhere in your server code and you needs to fix it. But you didn't show your actual code. – Remy Lebeau Nov 05 '18 at 20:01
  • We only use OnExecute event, who it's the same as posted here https://stackoverflow.com/questions/53035334/prevent-tidtcpserver-stuck-connections?noredirect=1#comment93043415_53035334, i call AContext.Connection.Disconnect; on OnException event are that correct? or TIdTcpServer already drop client after an exception. – Pâmella Douglas Nov 07 '18 at 01:30
  • You can also download the source code for example used here https://sites.google.com/site/delphigeist/downloads/IndyProtoSrc.zip?attredirects=0 – Pâmella Douglas Nov 07 '18 at 01:41
  • No, you do not need to call `Disconnect()` in the `OnException` event. The thread is already in a state of cleanup by that time, so the socket will be closed automatically after the event handler exits. – Remy Lebeau Nov 07 '18 at 01:59
  • 1
    Please do not post your code on an external site expecting people to download it, [edit] your question instead to show the code. But what you have provided is nowhere near a [mcve], so please slim it down to the **absolute minimum code** needed to reproduce your issue. – Remy Lebeau Nov 07 '18 at 02:05
  • it's so much things to add here, stackoverflow detect it as only code and don't permit save. the link who i send it's the source who i use. – Pâmella Douglas Nov 12 '18 at 20:59
  • It is still not permitted. Please post your code here instead, as a [mcve] (emphasis on MINIMAL and COMPLETE) that people can copy/paste into a project and run it. – Remy Lebeau Nov 12 '18 at 21:06
  • Later i try it again, i already tried others times but stackoverflow block. – Pâmella Douglas Nov 15 '18 at 20:11

0 Answers0