none
How to ping an IP address every second or more? RRS feed

  • Question

  • I am trying to build a windows form application, where the user enter data, and then these data go to a structured JSON file. Therefore. when the application is open, inside the Form1_Load operation I have tried to ping the IP address and change the label to either "In Range" or "Out of Range". but in order to have a reliable application I need to consistently ping the IP address periodically to ensure the the other device is within the range. The ultimate goal is to save files from remote server to a local server using SFTP.

    Here what I was trying to do: but It only ping it once.

    Ping p = new Ping();
    
                PingReply r1;
                PingReply r2;
                PingReply r3;
                PingReply r4;
    
                string s1 = currentList[0].IPaddress;
                string s2 = currentList[1].IPaddress;
                string s3 = currentList[2].IPaddress;
                string s4 = currentList[3].IPaddress;
    
                //s = textBox2.Text;
                textBox2.Text = s1;
                r1 = p.Send(s1);
    
                textBox3.Text = s2;
                r2 = p.Send(s2);
    
                textBox5.Text = s3;
                r3 = p.Send(s3);
    
                textBox11.Text = s4;
                r4 = p.Send(s4);
                if (r1.Status == IPStatus.Success)
                {
                    label3.Text = "In Range";
                }
                else
                {
                    label3.Text = "Out of Range";
                }
                if (r2.Status == IPStatus.Success)
                {
                    label4.Text = "In Range";
                }
                else
                {
                    label4.Text = "Out of Range";
                }
    
                if (r3.Status == IPStatus.Success)
                {
                    label7.Text = "In Range";
                }
                else
                {
                    label7.Text = "Out of Range";
                }
    
                if (r4.Status == IPStatus.Success)
                {
                    label16.Text = "In Range";
                }
                else
                {
                    label16.Text = "Out of Range";
                }

    Note that the IP addresses are extracted from a JSON file like below:

                filePath = @"C:\Users\Sami\Desktop\Company";
                string text = File.ReadAllText(filePath);
    
                var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
                //first data
                textBox1.Text = currentList[0].Machinename;
                textBox2.Text = currentList[0].IPaddress;
    
                //second data
                textBox4.Text = currentList[1].Machinename;
                textBox3.Text = currentList[1].IPaddress;
    Any thoughts?


    • Edited by samiarja Monday, August 5, 2019 10:20 AM text editing
    Monday, August 5, 2019 5:52 AM

Answers

  • Hi samiarja, 

    Thank you for posting here.

    As Hooyberghs Johnny suggested, you can use a timer to ping an IP address every second.

    I have made a sample and it shows how to use timer in your winform application.

    Mycode:

            static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                myTimer.Tick += new EventHandler(TimerEventProcessor);
                // Sets the timer interval to 1 seconds.
                myTimer.Interval = 1000;
                myTimer.Start();
            }
            /// <summary>
            /// Use your Ping code in this method
            /// </summary>
            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {
                string filePath = @"your path";
                string text = File.ReadAllText(filePath);
    
                List<Datalogger> currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
    
                string s1 = currentList[0].IPaddress;
                textBox1.Text = s1;
            }

    Result of my test:

    Display the IPaddress after 1 second:

    Update IPaddress every 1 second. If I change the value of IPaddress, it will change the value in textbox:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 6, 2019 3:04 AM
    Moderator

All replies

  • Hi,

    If I understand correctly: you want to periodically ping the different IP-addresses?

    In this case, where you are writing your code inside the Form_Load event handler, the code will only run once, when the form loads. If you want to run your code multiple times there are a few options:

    1. You can use a timer with a specific interval and run the code inside the Timer_Tick event handler.
    2. You can run the code when the user changes the IP-address in the TextBox by using the TextBox_TextChanged event handler.

    Also try to create a helper method that does the actual ping and call it four times with different parameters.

    Kind regards,

    Johnny Hooyberghs

    Monday, August 5, 2019 7:10 AM
  • How does a one-per-second ping increase your reliability?  Answer: it doesn't.  It might provide a little bit of feedback, but the critical question is, can you send your SFTP data?  Nothing matters until you actually send the data, and just because it answers to "ping" now doesn't mean it will answer 10 seconds from now.

    The "ping" is a silly idea.  Just do your SFTP, and if it doesn't connect, notify the user at that time.  It is reasonable to do a single "ping" when you get a new address, as Johnny suggested, just to check for silly mistakes, but once it passes once, there's no point in wasting network bandwidth like this.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, August 5, 2019 6:02 PM
  • Hi Tim, basically I am building an application for the mining field where there are lots of data profiler attached to the vehicle. Each data profiler has a unique IP address and data in the memory.

    The idea is when the vehicle get inside the network, the application will detect that there is an IP in range and I will start downloading the files to the local server using SFTP. While I know pinging the IP specially when you have lots of data profiler can waste network bandwidth, but do you suggest any other method or implementation?

    Monday, August 5, 2019 10:41 PM
  • Hi samiarja, 

    Thank you for posting here.

    As Hooyberghs Johnny suggested, you can use a timer to ping an IP address every second.

    I have made a sample and it shows how to use timer in your winform application.

    Mycode:

            static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                myTimer.Tick += new EventHandler(TimerEventProcessor);
                // Sets the timer interval to 1 seconds.
                myTimer.Interval = 1000;
                myTimer.Start();
            }
            /// <summary>
            /// Use your Ping code in this method
            /// </summary>
            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {
                string filePath = @"your path";
                string text = File.ReadAllText(filePath);
    
                List<Datalogger> currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
    
                string s1 = currentList[0].IPaddress;
                textBox1.Text = s1;
            }

    Result of my test:

    Display the IPaddress after 1 second:

    Update IPaddress every 1 second. If I change the value of IPaddress, it will change the value in textbox:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 6, 2019 3:04 AM
    Moderator
  • Just do the SFTP.  Don't waste time on the pings.

    Here's how I'd do it.  I presume you will have a list of all the IP addresses.  Let's say once you have dumped data from a profiler, you don't care about contacting it for another 5 minutes.  So, have a data structure with the profiler's IP address and a "last contact time".  Then, every 15 seconds or so, run through the whole list.  If the profiler hasn't been contacted in the last 5 minutes, try an SFTP.  If it succeeds, update the "last contact time".  If it fails, leave it in the list and move on to the next one.

    Easy, no extra overhead, and every successful contact results in data acquired.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, August 6, 2019 3:25 AM