none
TCP Socket failing issue RRS feed

  • Question

  • 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;
        }
    }

    Wireshark sniffing:

    Device error

    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.



    Sunday, November 12, 2017 9:31 PM

Answers

  • Hello RassK,

    the error message may occurs by the following circumstance.

    1.This was possibly due to a data transmission timeout or protocol error.

    2.antivirus programs and firewalls blocks access because it believes the connection is malicious, please checked your firewall/antivirus/network configuration.

    3.In my knowledge, the issues also occurs by lose packages when transmit data while client can't response the resend request. it also caused the timeout problem.

    If you are using widows server, the 'software in your host machine' that is referred to is actually 'Winsock' - the TCP/IP component of Windows.

    You could get more details from there.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RassK Tuesday, November 14, 2017 4:59 PM
    Monday, November 13, 2017 10:22 AM
    Moderator

All replies

  • Hello RassK,

    the error message may occurs by the following circumstance.

    1.This was possibly due to a data transmission timeout or protocol error.

    2.antivirus programs and firewalls blocks access because it believes the connection is malicious, please checked your firewall/antivirus/network configuration.

    3.In my knowledge, the issues also occurs by lose packages when transmit data while client can't response the resend request. it also caused the timeout problem.

    If you are using widows server, the 'software in your host machine' that is referred to is actually 'Winsock' - the TCP/IP component of Windows.

    You could get more details from there.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RassK Tuesday, November 14, 2017 4:59 PM
    Monday, November 13, 2017 10:22 AM
    Moderator
  • Hi Neil,

    Thanks, I was able to focus to the right point and find the mistake.

    Timeout was set to way too low...

    ...
    TcpClient client = server.EndAcceptTcpClient(ar);
    
    int timeout = (int)TimeSpan.FromSeconds(3).TotalMilliseconds;
    
    client.ReceiveTimeout = timeout;
    client.SendTimeout = timeout;
    ...

    Tuesday, November 14, 2017 5:03 PM