Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: PortForwading Bug

$
0
0
Hi,

I could reproduce your issue with your code and credentials.
The problem in fact isn't SSH.Net. Its the SqlConnection pool.

To make it clear, the problem is your connection/query/result seems to get cached.
So same connection/query, result is returned from the cache not the actual server.

If you clear the pool, in C# it's "SqlConnection.ClearAllPools()" or "SqlConnection.ClearPool(sql)", everything is fine.

My Code:
using System;
using System.Data.SqlClient;
using Renci.SshNet;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string ip1 = "77.73.166.66";
            string ip2 = "77.73.161.57";
            try
            {
                GetData(ip1);
                GetData(ip2);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }

            Console.WriteLine("Press Enter to exit");
            Console.ReadLine();
        }

        private static void GetData(string ip)
        {
            Console.WriteLine("Getting Data from: " + ip);
            var client = new SshClient(ip, 1443, "tempuser", "tempuser");
            client.Connect();

            var fw = new ForwardedPortLocal("127.0.0.1", 14444, "192.168.1.3", 1433);
            client.AddForwardedPort(fw);
            fw.Start();

            var query = "select * from temp";
            var foo = "data source =127.0.0.1,14444; initial catalog = TEMP; user id = tempuser; password = tempuser; MultipleActiveResultSets=True";

            var sql = new SqlConnection(foo);
            var cmd = new SqlCommand();

            sql.Open();
            cmd.Connection = sql;
            cmd.CommandText = query;

            var reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Console.WriteLine(reader[0] + " " + reader[1]);
            }

            reader.Close();
            sql.Close();

            // this fixes the issue
            //SqlConnection.ClearAllPools();
            SqlConnection.ClearPool(sql);


            fw.Stop();
            client.RemoveForwardedPort(fw);
            client.Disconnect();
        }
    }
}
Also if you don't clear the pool, you can send sql queries to localhost:14443 without having any connection to it and
get the cached result.

Caching is so awesome... ;)

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>