Jump to content

C# Packet loss/Ping program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
30 replies to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
This code was created by me for my work to ping our Cisco switches. It tells in a percentage how many packets were lost during each ping.

system ("c:\windows\system32\ping.exe");
Ping your server / a server for packet loss. If more than X% then the server/network is down.

Below is a very simple program that I wrote to determine if any of our network switches are down.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;

namespace PacketLoss
{
 public partial class Form1 : Form
    {
        // Variables
        String [] hostArray;
        int amt_ping;

        public Form1()
        {
            InitializeComponent();

            // Declare the array and give it values
            hostArray = new String[14];
            
            // Set our ping amt
            amt_ping = 6;

            // Enter our hosts
            hostArray[0] = "127.0.0.1";

        }

        /// <summary>
        /// This function pings all of the hosts in the hostArray
        /// </summary>
        private void ping_hosts()
        {
            // Disable our button
            btn_ping.Enabled = false;

            // Clear our list view
            lv_results.Items.Clear();

            // Cycle through our host array
            foreach (String host in hostArray)
            {
                // Write our status
                lbl_status.Text = "Pinging (x" + amt_ping + "): " + host;

                // Allow the GUI to update
                Application.DoEvents();

                // Ping the host four times
                double loss = get_loss(host, amt_ping);

                // Determine if there is any loss
                if (loss > 0)
                {
                    // Insert into the List View
                    ListViewItem lv = lv_results.Items.Insert(lv_results.Items.Count, host);
                    lv.SubItems.Add(Convert.ToString(loss) + "%");

                } // End If

            } // End foreach

            // Update our label
            lbl_status.Text = "Complete - press Ping to restart.";

            // Enable our button
            btn_ping.Enabled = true;
        }


        /// <summary>
        /// Pings IP address and shows packet loss
        /// </summary>
        /// 

        /// 

        private void btn_ping_Click(object sender, EventArgs e)
        {
            ping_hosts();
        }

        /// <summary>
        /// Retrieves the packet loss in a percentage
        /// </summary>
        /// 

        /// <returns></returns>
        private double get_loss(String host, int pingAmount)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            int failed = 0;

            // Loop the amount of times to ping
            for (int i = 0; i < pingAmount; i++)
            {
                PingReply reply = pingSender.Send(host, timeout, buffer, options);
                if (reply.Status != IPStatus.Success)
                {
                    failed += 1;
                    /*tb_results.Text = "Address: " + reply.Address.ToString();
                    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                    Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                    Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                     */
                }

                // Allow the GUI to update
                Application.DoEvents();

            } // End For

            // Return the percentage
            double percent = (failed / pingAmount) * 100;
            return percent;
        }

        /// <summary>
        /// When the form loads start the ping
        /// </summary>
        /// 

        /// 

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            ping_hosts();
        }
}
}

Modify this string: hostArray[0] = "127.0.0.1"; to your server IP. This method is extremely fast and to make it faster reduce the amount of loops. I've also attached the complete program. This uses the tcp.PingReply class. Modify as needed.

Attached Files



#2
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts
cool

#3
vodanhkiem

vodanhkiem

    Newbie

  • Members
  • Pip
  • 1 posts
cool :)

#4
Insomnia

Insomnia

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks :)

#5
spamlist

spamlist

    Newbie

  • Members
  • Pip
  • 1 posts
I dont see the definition of your Ping class and i have to post before i can download your source.

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
The Ping class is part of .NET in System.Net.NetworkInformation.

Ping Class (System.Net.NetworkInformation)

#7
bkhhh

bkhhh

    Newbie

  • Members
  • Pip
  • 4 posts
where can i ping my addresses every time i change host array it always pings 10.225..... and it shows that there is an error in lv_result.systemindex changed it says that it should exist after += where it is found there could u help me plz urgent

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Is it a compile error or a runtime error?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
luverboy4u

luverboy4u

    Newbie

  • Members
  • Pip
  • 4 posts
will try it out

#10
Alon2k2

Alon2k2

    Newbie

  • Members
  • Pip
  • 1 posts
thx .. i will look into it.

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Thanks... :D
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
madhan

madhan

    Newbie

  • Members
  • Pip
  • 1 posts
cool