Renci ssh.net No connection could be made because the target machine actively refused it

Nathan picture Nathan · Oct 19, 2015 · Viewed 8k times · Source

We recently deployed a project into production and are now receiving this error message when we attempt to connect to the external sftp machine, "No connection could be made because the target machine actively refused it". When I was developing the application and testing it, we had no issues connecting to this server.

What would be different? I have administrative privileges and the app pool on the production server does not. I'm not sure if this could be what's causing the issue or if it may be something on the external client's server or their firewall blocking us.

Answer

Mike de Klerk picture Mike de Klerk · Nov 21, 2016

If you are using localhost, specify 127.0.0.1 instead.

When I was using SSH.Net and the host was specified as localhost it threw the exception that the host actively refused the connection WSAECONNREFUSED - 10061. When I specified 127.0.0.1 instead, the connection could be made. I think this is due to the implementation in SSH.Net:

    internal static IPAddress GetIPAddress(this string host)
    {
        IPAddress ipAddress;
        if (!IPAddress.TryParse(host, out ipAddress))
            ipAddress = Dns.GetHostAddresses(host).First();

        return ipAddress;
    }

Which does not resolve a valid hostname for localhost, apparently. C# Interactive yields the following:

> Console.WriteLine(Dns.GetHostAddresses("localhost").First());
::1

The value ::1 does not seem to be a valid IP address for sockets to use.