Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
- - - - -

C# Packet loss/Ping program


  • Please log in to reply
30 replies to this topic

#1 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 14 June 2007 - 04:47 AM

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


  • 0

#2 kresh7

kresh7

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 326 posts

Posted 09 November 2007 - 11:57 AM

cool
  • 0

#3 vodanhkiem

vodanhkiem

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 November 2007 - 10:34 PM

cool :)
  • 0

#4 Insomnia

Insomnia

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 17 February 2008 - 11:35 PM

Thanks :)
  • 0

#5 spamlist

spamlist

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 01 March 2008 - 05:32 PM

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

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 13 March 2008 - 04:21 AM

The Ping class is part of .NET in System.Net.NetworkInformation.

Ping Class (System.Net.NetworkInformation)
  • 0

#7 bkhhh

bkhhh

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 01 May 2008 - 08:40 AM

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
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 03 May 2008 - 05:34 AM

Is it a compile error or a runtime error?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 luverboy4u

luverboy4u

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 06 May 2008 - 05:06 AM

will try it out
  • 0

#10 Alon2k2

Alon2k2

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 May 2008 - 11:46 AM

thx .. i will look into it.
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 13 May 2008 - 11:54 AM

Thanks... :D
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 madhan

madhan

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 July 2008 - 08:33 PM

cool
  • 0