简体   繁体   中英

SFTP Connections not closing (Renci & C#)

I'm testing SFTP connections to a 3rd party, we're using C# and Renci to do this.

But the connections don't seem to be closing. This is my simple test code:

public bool TestConnection()
{
    using (var client = new SftpClient(_connectionInfo))
    {
        try
        {
            client.Connect();

            if (!client.IsConnected)
                return false;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
        finally
        {                                        
            client.Disconnect();              
            client.Dispose();                    
        }

        return true;
    }
}

Strictly speaking the disconnect and dispose are redundant.

Wireshark shows the last command of every test from us to the host is

59916 → 22 [RST, ACK] Seq=2319 Ack=1941 Win=0 Len=0

Which to me indicates that the connection should close. Yet after about 19 consecutive tests the host starts forcibly closing connection attempts until the original connections start timing out. Is the host ignoring the RST, is something like a firewall preventing the disconnect somehow? Whats going on?

Finally we found the cause. It had nothing to do with our code.

One of the internal firewalls has an intrusion prevention rule, basically to prevent DoS attacks.

We discovered this by testing against our own SFTP server. The server logs said the client was closing the connection. The client said the server was closing the connection, so we assumed that something in between was the culprit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM