AsyncTcpClient icon indicating copy to clipboard operation
AsyncTcpClient copied to clipboard

Any thoughts on handling network disconnection such as cable unplugged?

Open LiamKarlMitchell opened this issue 3 years ago • 1 comments

The close callbacks does not seem to execute for example when unplugging the network cable. Would be nice if it can.

ReceiveTimeout, SendTimeout do not help (Only for sync methods) Its like the ReadAsync is stuck waiting for data?

Reducing Keep Alive to 5

// Reduce Keep Alive check time to 5 seconds. // https://social.technet.microsoft.com/wiki/contents/articles/39650.c-the-use-of-keep-alive-to-deal-socket-abnormal-disconnection-method.aspx uint dummy = 0; byte[] inOptionValues = new byte[System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 3]; // Set keepalive on BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); // Interval time between last operation on socket and first checking. example:5000ms=5s BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy)); // After first checking, socket will check serval times by 1000ms. BitConverter.GetBytes((uint)500).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 2); tcpClient.Client.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);

Or how can I check for socket error after SendAsync then kill the ReadAsync so that it attempts reconnection or exits runAsync?

LiamKarlMitchell avatar Oct 30 '20 08:10 LiamKarlMitchell

Cable unplug can only be detected with a heartbeat/ping communication at a defined interval. That's up to the application level. I've done that in other communication systems. But it cannot be added to this sample code because it doesn't define a protocol. You have the byte stream all for yourself here.

ygoe avatar Nov 01 '20 13:11 ygoe

@LiamKarlMitchell Add

catch (IOException ex) when ((ex.InnerException as SocketException)?.ErrorCode == (int)SocketError.TimedOut)
{
    Message?.Invoke(this, new AsyncTcpEventArgs("Connection timeout", ex));
    readLength = -3;
}

behind:

https://github.com/ygoe/AsyncTcpClient/blob/58acf5f03be9691a171d31df0c7ebc87521311dc/AsyncTcpClient/AsyncTcpClient.cs#L231-L249

lathoub avatar Feb 11 '24 12:02 lathoub

Thanks, I was using this library to communicate to an eftpos terminal, I no longer have easy access to the code/project as have moved onto another project/team but as you can imagine disconnect of cable can happen somewhat frequently if it wears out and people move the machine.

The protocol did not have a way to ping during the operations transfer/print receipt etc if I recall as sending any unexpected message would terminate the connection but I had wanted to wait async as to not block UI thread of application. If I recall ended up having a reasonable timeout cancellationtoken and sending another message on reconnection to reset state.

Was a bit janky but worked around it.

LiamKarlMitchell avatar Feb 12 '24 03:02 LiamKarlMitchell