2016-07-14 7 views
0

これは私の最初の投稿stackoverflow、何かのために申し訳ありません。私のスコープにどのように私はCから接続sshを確立することができます#

private SshClient client; 
private ForwardedPortDynamic port; 

私はパテに似たSSHで接続するためのサンプルコードがあります。

private void Connect() 
    { 
     try 
     { 
      client = new SshClient("myserverproxy.net", "user", "password"); 
      client.KeepAliveInterval = new TimeSpan(0, 0, 60); 
      client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20); 
      client.Connect(); 
      port = new ForwardedPortDynamic("127.0.0.1", 20141); 
      client.AddForwardedPort(port); 
      port.Exception += delegate(object sender, ExceptionEventArgs e) 
      { 
       Console.WriteLine(e.Exception.ToString()); 
      }; 
      port.Start(); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

そして切断するには、このコード:

private void Disconnect() 
    { 
     try 
     { 
      port.Stop(); 
      client.Disconnect(); 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

を私は持っていますメソッド "Connect()"を呼び出すボタンですが、しばらくしてから切断され、もう動作しません。何が切断の原因ですか?私は不確定な時間の接続を確立する必要があります。

ありがとうございます!

+0

こんにちはジャック、サンプルコードはありますか? –

答えて

0

最初にSshClientは使い捨てですので、usingキーワードを使用して呼び出す必要があります。このような何か:

using (client = new SshClient("myserverproxy.net", "user", "password")) { 
    client.KeepAliveInterval = new TimeSpan(0, 0, 60); 
    client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20); 
    client.Connect(); 
    port = new ForwardedPortDynamic("127.0.0.1", 20141); 
    ... 
} 

第二に、あなたはthisを読んでいるあなたはForwardedPortLocal代わりのForwardedPortDynamicを使用する必要があります示唆しています。

+0

私はこれを示唆し、テストを読んで行く、ありがとう –

0

私はこれを試すことはできませんが、私はこの方法でそれを行うだろう:

public static void Start() 
{ 
     using (var client = new SshClient("myserverproxy.net", "user", "password")) 
     { 
      client.KeepAliveInterval = new TimeSpan(0, 0, 60); 
      client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20); 
      client.Connect(); 
      ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 20141); 
      client.AddForwardedPort(port); 
      port.Exception += delegate(object sender, ExceptionEventArgs e) 
      { 
       Console.WriteLine(e.Exception.ToString()); 
      }; 
      port.Start(); 
    } 
} 

public static void Main(string[] args) 
{ 
    Thread thread1 = new Thread(Start); 
    thread1.Start(); 
} 

は、多分それは30代にKeepAliveIntervalのを設定するのに役立ちますか?

+0

私はこれを行う場合、私は常に新しい接続を持っているでしょうか? –

関連する問題