Lost Password?

  #1 (permalink)  
Old 06-14-2007, 07:47 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,846
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

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 To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!

Last edited by Jordan; 03-13-2008 at 07:16 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-09-2007, 01:57 PM
kresh7 kresh7 is offline
Learning Programmer
 
Join Date: Jun 2007
Posts: 99
Rep Power: 5
kresh7 is on a distinguished road
Default

cool
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-11-2007, 12:34 AM
vodanhkiem vodanhkiem is offline
Newbie
 
Join Date: Nov 2007
Posts: 1
Rep Power: 0
vodanhkiem is on a distinguished road
Default

cool
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-18-2008, 01:35 AM
Insomnia Insomnia is offline
Newbie
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0
Insomnia is on a distinguished road
Default

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 07:32 PM
spamlist spamlist is offline
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0
spamlist is on a distinguished road
Default Thanks

I dont see the definition of your Ping class and i have to post before i can download your source.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-13-2008, 07:21 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,846
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: C# Packet loss/Ping program

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

Ping Class (System.Net.NetworkInformation)
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-01-2008, 11:40 AM
bkhhh bkhhh is offline
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0
bkhhh is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-03-2008, 08:34 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: C# Packet loss/Ping program

Is it a compile error or a runtime error?
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-06-2008, 08:06 AM
luverboy4u luverboy4u is offline
Newbie
 
Join Date: May 2008
Posts: 4
Rep Power: 0
luverboy4u is on a distinguished road
Default Re: C# Packet loss/Ping program

will try it out
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-13-2008, 02:46 PM
Alon2k2 Alon2k2 is offline
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
Alon2k2 is on a distinguished road
Default Re: C# Packet loss/Ping program

thx .. i will look into it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I Program another Program? ! bosco General Programming 1 06-15-2007 11:15 AM
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 10:51 PM
Need help w/ word count program (ASAP) siren C and C++ 1 04-23-2007 08:14 AM
How to modify a program written in .NET 2.0? jackyjack C# Programming 7 03-27-2007 12:26 PM


All times are GMT -5. The time now is 09:19 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads