0

The code below is what I'm using to ping 8.8.8.8, but some times when I press start it is just packet loss which this counts, that is strange because I'm sure I don't have that much of packet loss, then I pause it and start again and it works just fine, it doesn't make sense to me, I'm at a loss.

I added Thread.Sleep(); because I guessed that the program is not probably loaded when I start it, but that's strange too ! it is such a small and simple program it shouldn't have such problems

I also have a question what time out time should I put for this , right now the app sends a packet each sec , and drops it every 900ms , should I put it higher than 900 or would that drop the performance?

public partial class Form1 : Form
{
    public int PingTime;
    public int pingor = 0;
    public int pingur = 0;
    public int maxping;
    public int minping;
    public Int64 avgping;
    public string avrage;
    public int ping_no;
    public string fail;
    public string msg1 = " Packet Lost ";

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true )
        {
            timer1.Enabled = false;
            Thread.Sleep(1000);
        }
        else if (timer1.Enabled == false)
        {
            timer1.Enabled = true;
            Thread.Sleep(1000);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {         
        bool pingable = false;

        PingOptions options = new PingOptions();
        options.DontFragment = true;
        string data = "1";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        Ping pinger = new Ping();
        try
        {
            //37.58.117.146
            //8.8.8.8
            PingReply reply = pinger.Send("8.8.8.8", 900, buffer, options);
            pingable = reply.Status == IPStatus.Success;
            string pingtime = reply.RoundtripTime.ToString();
            int newpingtime = Convert.ToInt32(pingtime);

            if (reply.Status == IPStatus.Success)
            {
                label1.Text = reply.RoundtripTime.ToString();
            }
            else
            {
                label1.Text = msg1;
            }

            //_________________________Max Ping

            if (maxping == 0)
            {
                maxping = Convert.ToInt32(newpingtime);
            }
            else if (newpingtime >= Convert.ToInt32(maxping))
            {
                maxping = Convert.ToInt32(newpingtime);
            }
            lblmax.Text = maxping.ToString();

            //_________________________Min Ping

            if (reply.Status == IPStatus.Success && minping == 0 && newpingtime == 0)
            {
                lblmin.Text = minping.ToString();
            }
            else if (reply.Status == IPStatus.Success && lblmin.Text == " - - -" && newpingtime >= 0)
            {
                minping = newpingtime;
                lblmin.Text = minping.ToString();
            }

            else if (reply.Status == IPStatus.Success && newpingtime < minping)
            {
                minping = newpingtime;
                lblmin.Text = minping.ToString();
            }

            //_________________________Ping AVG

            if (reply.Status == IPStatus.Success)
            {
                avgping = avgping + newpingtime;
                ping_no = ping_no + 1;
                lblavg.Text = Convert.ToString(avgping / ping_no);
            }
        }
        catch (PingException error)
        {
            MessageBox.Show(error.Message);
        }

        if (pingable == true)
        {
            pingor++;
        }

        if (pingable == false)
        {
            pingur++;
        }

        lblrecived.Text = Convert.ToString(pingor);
        lbllost.Text = Convert.ToString(pingur);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Thread.Sleep(900);
    }

Thanks in advance, Just letting you know that I'm not having packet loss more than 2% and this shouldn't show 40 packet loss and 0 received ! also it is working just fine 80% of the times!

Once again It happens when I pause and start it again fast or some times the first time I start it.

Sina M.Azad
  • 27
  • 1
  • 9
  • Ping is not the definitive answer to the world - ping has a low network priority, so yes, ping can report packet loss when there isnt any.. however, it is indicative of other issues. – BugFinder Feb 10 '17 at 09:39
  • But using CMD , Powershell and Pingplotter I dont see such a problem and I need to fix it in order to make it a usable app. – Sina M.Azad Feb 10 '17 at 09:40
  • 2
    Are you sure you are not receiving an Exception - you are silently suppressing them. – RB. Feb 10 '17 at 09:43
  • 1
    I would also put a `Debug.Writeline("Exception in Ping")` in your empty catch block, because currently you have no idea when an exception occurs, and that would result in a loss... – Martin Verjans Feb 10 '17 at 09:44
  • Still having problem , added : catch (PingException error) { MessageBox.Show(error.Message); } – Sina M.Azad Feb 10 '17 at 09:52
  • @SinaM.Azad What do you need it for exactly? – FCin Feb 10 '17 at 09:59
  • Just want to write a app with graph and other stuff to monitor the network. this should be the base. @FCin – Sina M.Azad Feb 10 '17 at 10:00

1 Answers1

0

I tested your code and got the same issues. Then I tested ping 8.8.8.8 -t -w 900 from cmd, which usually reports 35ms (same as your code), but also sometimes shows an timeout every now and then.

I think the timeout value set is measured from the time the request is send to the networking service, until an answer is received. If the service is busy, 900ms might not be sufficent, even if the actual ping would be possible in under 900ms.

(not an answer, but to long for a comment)

enter image description here

dognose
  • 20,360
  • 9
  • 61
  • 107
  • Thanks for putting time on this , something I'm not sure if i mentioned is that I don't get 1 or 2 packet loss , it is like constant packet loss till i pause and start it again , did you have the same problem ? @dognose – Sina M.Azad Feb 10 '17 at 11:47
  • @SinaM.Azad Sometimes. Most the time it was like 4-5 losses, then 4-5 good, then losses again. – dognose Feb 10 '17 at 14:32