Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two devices having separate IP addresses and want to check if any one is connected and if connected download the database from the device and any given time only one device is connected.My query works fine for one device how to check if ahve another IP address.

C#
string _ftpURL = @"100.100.00.0";  //Host URL or address of the SFTP server
 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
            Sftp Connection = new Sftp(_ftpURL, _UserName, _Password);
            Connection.Connect(_Port);
            Connection.Get(_ftpDirectory, LocalDirectory);
            Connection.Close();


I am struck here as I have to check which Ipaddress is connected .:101.00.00.0.(2nd Ip address)
Posted
Comments
Sergey Alexandrovich Kryukov 3-Mar-15 12:13pm    
If-else is one thing, IP is something different. Embrace separation of concerns and take one concern at a time. Don't hard-code path names as you do, there are no cases when it can be useful.
—SA
Kiran Vontela 3-Mar-15 12:21pm    
Thanks Sergey yes i agree that if else and Ip are different but my situation how to find out which device is connected and then download the data.I have no idea how to do that.
Sergey Alexandrovich Kryukov 3-Mar-15 12:27pm    
Usually the devices have some API with some method which does nothing or provides some basic info. You can call it and see if it fails or not. Also, it's more typical that you connect to the device programmatically, not the device connects to your software. If by "connected" you mean physically connected, you can try to connect to the device programmatically and see if it fails. This is what I can tell you without reading the documentation you should have, but you can read it.

And stop hard-coding the path names... :-)

—SA
PIEBALDconsult 3-Mar-15 12:29pm    
I recommend trying a different tactic altogether. Something I did with syncing files to some USB devices was to have a configuration file (e.g. DeviceName.MyApp) on the device. When I attached a device, I could use Explorer to access the device and then double-click the configuration file, and I used a file association so the system would then run MyApp and sync that device.
HKHerron 3-Mar-15 12:55pm    
Are the servers on your Local LAN, or internet?
Do you have any better idea of what the IP Addresses are?

Your starting at 100.100.0.0, but how many IP addresses do you need to go through to find the next one? or Do you know the IP Addresses of the server?

Since FTP servers use a particular Port, I would suggest possibly using a C# Port scanner on the IP address range and FTP port you are using (2222). You really don't even have to login or any thing, just see if it responds. Any response from one would mean that the server is ONLINE.

However, depending on the number of IP addresses it would need to go through, this could be a long process....

100.100.0.0 - 100.100.0.255 is a total of 256 IP address to search through.
100.100.0.0 - 100.100.255.255 is a total of 65,025 IP address to search through.

1 solution

I think I may have miss read your post. :(
It appears you only have 2 servers to check, and you know what their IP Address are.

You could then try something like this:
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;
     }
}
 
Share this answer
 
Comments
Kiran Vontela 9-Mar-15 7:12am    
Thanks HKHerron that's what I need Just to make sure if both are connected and different ports.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900