+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: C# Packet loss/Ping program

  1. #1
    Jordan Guest
    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.

    Code:
    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.

    Code:
    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 Attached Files
    Last edited by Jordan; 03-13-2008 at 05:16 AM.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jun 2007
    Location
    Kosovo
    Posts
    660
    Rep Power
    23
    cool

  4. #3
    vodanhkiem is offline Newbie
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0
    cool

  5. #4
    Insomnia is offline Newbie
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0
    Thanks

  6. #5
    spamlist is offline Newbie
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Thanks

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

  7. #6
    Jordan Guest

    Re: C# Packet loss/Ping program

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

    Ping Class (System.Net.NetworkInformation)

  8. #7
    bkhhh is offline Newbie
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Re: C# Packet loss/Ping program

    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

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Packet loss/Ping program

    Is it a compile error or a runtime error?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    luverboy4u is offline Newbie
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Re: C# Packet loss/Ping program

    will try it out

  11. #10
    Alon2k2 is offline Newbie
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Re: C# Packet loss/Ping program

    thx .. i will look into it.

+ Reply to Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My Weight Loss Program
    By Nukey in forum Community Projects
    Replies: 0
    Last Post: 10-17-2010, 03:29 AM
  2. Packet loss
    By dancal in forum Perl
    Replies: 1
    Last Post: 09-22-2010, 06:28 PM
  3. Packet Loss Perl Script
    By Jordan in forum Tutorials
    Replies: 1
    Last Post: 04-29-2007, 10:29 AM
  4. ping program
    By Deathcry in forum C and C++
    Replies: 3
    Last Post: 02-27-2007, 01:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts