I have a long running TCP connection. A machine (IoT device) establishes a connection with the server, connection is setup (encryption and stuff) and data stored, connection is kept open a while.
Everything kind of works but sometimes the server 'drops' the connection with an error: An established connection was aborted by the software in your host machine (code: 10053 - ConnectionAborted). But the connection isn't dropped, cause server can read data from the device after the error and could start sending again. If the connection drops in real, both the server and client need to reinitialize connection (security and stuff).
There is nothing really that indicates why network stream cannot be written to. And polling the socket says, that it's writetable, and in next point it throws an exception. Seems to happen randomly.
public class ClientIdentifier
{
...
public TcpClient Connection { get; set; }
public BlockDecoder ConnectionDecoder { get; set; }
}
private void ReplyToClient(ClientIdentifier client, byte[] data)
{
byte[] encrypted = client.ConnectionDecoder.Encrypt(data);
var stream = client.Connection.GetStream();
int dataIndex = 0;
while (dataIndex != encrypted.Length)
{
if (CanWriteClient(client))
{
byte[] block = encrypted.GetChunk(dataIndex, Frame.BLOCK_LENGTH);
stream.Write(block, 0, Frame.BLOCK_LENGTH);
dataIndex += Frame.BLOCK_LENGTH;
}
}
}
private bool CanWriteClient(ClientIdentifier client)
{
try
{
return client.Connection.Client.Poll(1000, SelectMode.SelectWrite);
}
catch (Exception ex)
{
Logger.Warn(ex, $"[{client.HexIdentifier}]: Polling client write failed");
return false;
}
}
EDIT (12.11.2017)
Server: 192.168.1.150 Device: 192.168.1.201
I can see that the device sends RST when the server resets Seq and Ack in some weird way.

Managed to find the mistake. Timeout was set to way too low.