2015-03-03 152 views
1

我有两个设备具有不同的IP地址,并希望检查是否有任何连接,并且如果连接从设备下载数据库,并且任何给定时间只有一个设备连接。我的查询工作正常罚款一个设备如何检查哪一个连接。我已更新我的代码,但不知道它是如何工作的。检查IP地址并下载数据

private void button7_Click(object sender, EventArgs e)// 1)first download database to local system. 
{  
    this.Process1(); 
} 

public void Process1() 
{ 
    string _ftpURL = @"131.000.00.0"; // fake Host URL or address of the SFTP server 
    /* how to check for another IP adddress if exists */ 

    string _UserName = "root"; //fake User Name of the SFTP server 
    string _Password = "3term"; // fake Password of the SFTP server 
    int _Port = 2222; //Port No of the SFTP server (if any) 
    string _ftpDirectory = "/home/root/systools/WM/WebMobility.db"; //The directory in SFTP server where the files will be uploaded 
    string LocalDirectory = "F:\\Explor\\final test"; //Local directory from where the files will be uploaded 
    try 
    {   
     Sftp Connection = new Sftp(_ftpURL, _UserName, _Password); 
     Connection.Connect(_Port); 
     Connection.Get(_ftpDirectory, LocalDirectory); 
     Connection.Close(); 
    } 
    catch (Exception ex) 
    { 
     if (ex is SshConnectionException || ex is SocketException) 
     { 
      _ifwInstance.Error(string.Format("Ignoring {0} during listing directory", ex.Message)); 
     } 
     else 
     { 
      string _ftpURL = @"131.111.11.11"; // fake Host URL or address of the SFTP server 
      /* how to check for another IP adddress if exists */ 

      string _UserName = "root"; //fake User Name of the SFTP server 
      string _Password = "3term"; // fake Password of the SFTP server 
      int _Port = 2222; //Port No of the SFTP server (if any) 
      string _ftpDirectory = "/home/root/systools/WM/WebMobility.db"; //The directory in SFTP server where the files will be uploaded 
      string LocalDirectory = "F:\\Explor\\final test"; //Local directory from where the files will be uploaded 
      throw new Exception("Login to SFT FAILED", ex); 
     } 
    } 
} 

更新代码:

string[] _ftpURL = { @"100.100.0.0", @"101.0.0.0" }; //Array of address to SFTP servers 
string _UserName = "root"; //fake User Name of the SFTP server 
string _Password = "310rp3"; // fake Password of the SFTP server 
int _Port = 2222; //Port No of the SFTP server (if any) 
string _ftpDirectory = "/home/root/systools/WM/WebMobility.db"; //The directory in SFTP server where the files will be uploaded 
string LocalDirectory = "F:\\Explor\\final test"; //Local directory from where the files will be uploaded 
bool online = false; 
foreach(string furl in _ftpURL) 
{ 
    Sftp Connection = new Sftp(furl, _UserName, _Password); 
    try 
    { 
      Connection.Connect(_Port); 
      online = true; 
    } 
    catch 
    { 
      online = false; 
    } 
    if(online == true) 
    { 
      Connection.Get(_ftpDirectory, LocalDirectory); 
      Connection.Close(); 
      break; 
    } 

}

+1

任何机会,你可以改述,因为我正在努力解决你问的是什么。 – Ceisc 2015-03-03 11:39:51

+0

据我了解,如果SFTP主机不可访问,Connection.Connect方法应该引发异常。您可能会发现此异常并尝试另一个IP。 – 2015-03-03 11:41:11

+0

如果您试图查看是否存在服务器,请尝试使用System.Net.NetworkInformation.Ping – Johan 2015-03-03 11:41:15

回答

0

添加这两种方法的一些类,并从您的桌面代码中调用公共DownloadSftpFile

// returns true if the file had downloaded 
public static bool DownloadSftpFile(string[] hosts, int port, string username, string password, string remotePathAndFile, string localPath) 
{ 
    foreach (var host in hosts) 
    { 
     try 
     { 
      DownloadSftpFile(host, port, username, password, remotePathAndFile, localPath); 
      return true; 
     } 
     catch(SshConnectionException exception) 
     { 
      // log 
     } 
     catch(SocketExcpetion exception) 
     { 
      // log 
     } 
    } 

    return false; 
} 

private static void DownloadSftpFile(string host, int port, string username, string password, string remotePathAndFile, string localPath) 
{ 
    using (var sftp = new Sftp(host, username, password)) 
    { 
     sftp.Connect(port); 
     sftp.Get(remotePathAndFile, localPath); 
    } 
} 
+0

感谢马克我想通过让你知道这就是我所做的。如果可能的话,你可以建议这是正确的方式,因为我知道我的IP地址,我可能会错过一些东西。更新的代码>。 – pretyv5 2015-03-04 08:29:10

+0

据我所知,你的代码是正确的。为了更好地维护,您可以在不同的类/模块中实现下载。 – 2015-03-04 09:43:09