Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. continuous PING application ?

continuous PING application ?

Scheduled Pinned Locked Moved C#
csharplinqtestingbeta-testingtutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auting82
    wrote on last edited by
    #1

    Hi , I have here an ping application implemented as a console application. I want to turn this into a continuous ping application. Can someone give me a tip how to change this code so it runs continuously.

    ///////////////////////////////////////////////////////////////////////////////////////////
    ///
    /// PingAppConsile: application testing the ping() connection to a node
    ///
    /// Based on code form Microsoft MSDN about the ping() command
    ///
    /// Version: 1.0: 8-JAN-17: NOS
    ///
    ///////////////////////////////////////////////////////////////////////////////////////////
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    using System.Net;
    using System.Net.NetworkInformation;
    //
    namespace PingAppConsole
    {
    class Program
    {
    /// /// //////////////////////////////////////////////////////////////////////////////
    static void Main(string[] args)
    ///
    /// Purpose: the main function in the application handling the ping()communication
    ///
    /// Version: 1.0: 09-FEB-20: Adis Pipic
    ///
    {
    string host, data;
    byte[] buffer;
    int timeout;
    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.
    data = "Rodham street";
    buffer = Encoding.ASCII.GetBytes(data);
    timeout = 120;
    // Name or address of node to access
    host = "www.google.no";
    PingReply reply = pingSender.Send(host, timeout, buffer, options);
    if (reply.Status == IPStatus.Success)
    {
    Console.WriteLine(" Ping communication status for {0}:", host);
    Console.WriteLine(" ------------------------------------------");
    Console.WriteLine(" Address: {0}", reply.Address.ToString());
    Console.WriteLine(" RoundTrip time (mSec): {0}", reply.RoundtripTime);
    Console.WriteLine(" Time to live: {0}", reply.Options.Ttl);
    Console.WriteLine(" Dont fragment: {0}", reply.Options.DontFragment);
    Console.WriteLine(" Buffer size: {0}", reply.Buffer.Length);

    K L 2 Replies Last reply
    0
    • A auting82

      Hi , I have here an ping application implemented as a console application. I want to turn this into a continuous ping application. Can someone give me a tip how to change this code so it runs continuously.

      ///////////////////////////////////////////////////////////////////////////////////////////
      ///
      /// PingAppConsile: application testing the ping() connection to a node
      ///
      /// Based on code form Microsoft MSDN about the ping() command
      ///
      /// Version: 1.0: 8-JAN-17: NOS
      ///
      ///////////////////////////////////////////////////////////////////////////////////////////
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Threading;
      using System.Net;
      using System.Net.NetworkInformation;
      //
      namespace PingAppConsole
      {
      class Program
      {
      /// /// //////////////////////////////////////////////////////////////////////////////
      static void Main(string[] args)
      ///
      /// Purpose: the main function in the application handling the ping()communication
      ///
      /// Version: 1.0: 09-FEB-20: Adis Pipic
      ///
      {
      string host, data;
      byte[] buffer;
      int timeout;
      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.
      data = "Rodham street";
      buffer = Encoding.ASCII.GetBytes(data);
      timeout = 120;
      // Name or address of node to access
      host = "www.google.no";
      PingReply reply = pingSender.Send(host, timeout, buffer, options);
      if (reply.Status == IPStatus.Success)
      {
      Console.WriteLine(" Ping communication status for {0}:", host);
      Console.WriteLine(" ------------------------------------------");
      Console.WriteLine(" Address: {0}", reply.Address.ToString());
      Console.WriteLine(" RoundTrip time (mSec): {0}", reply.RoundtripTime);
      Console.WriteLine(" Time to live: {0}", reply.Options.Ttl);
      Console.WriteLine(" Dont fragment: {0}", reply.Options.DontFragment);
      Console.WriteLine(" Buffer size: {0}", reply.Buffer.Length);

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      put an infinite loop around the ping call

      while ( true ) {
      PingReply reply = pingSender.Send(host, timeout, buffer, options)
      // ...
      else
      {
      Console.WriteLine("Error ... ");
      }
      }

      Not sure if you could ever break out of that, though. You'd probably want to reduce that to one line of output, similar to what you get from ping from the command prompt. Oh, and you'll probably want to add a delay at the bottom of the loop so you don't flood the network with ping requests. Thread.Sleep() seems like a good option.

      L A 2 Replies Last reply
      0
      • K k5054

        put an infinite loop around the ping call

        while ( true ) {
        PingReply reply = pingSender.Send(host, timeout, buffer, options)
        // ...
        else
        {
        Console.WriteLine("Error ... ");
        }
        }

        Not sure if you could ever break out of that, though. You'd probably want to reduce that to one line of output, similar to what you get from ping from the command prompt. Oh, and you'll probably want to add a delay at the bottom of the loop so you don't flood the network with ping requests. Thread.Sleep() seems like a good option.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Use a Timer. Call your routine from the Timer's Tick event. Disable / enable the Timer each time you handle the event to avoid reentrancy with short intervals.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        1 Reply Last reply
        0
        • A auting82

          Hi , I have here an ping application implemented as a console application. I want to turn this into a continuous ping application. Can someone give me a tip how to change this code so it runs continuously.

          ///////////////////////////////////////////////////////////////////////////////////////////
          ///
          /// PingAppConsile: application testing the ping() connection to a node
          ///
          /// Based on code form Microsoft MSDN about the ping() command
          ///
          /// Version: 1.0: 8-JAN-17: NOS
          ///
          ///////////////////////////////////////////////////////////////////////////////////////////
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Threading;
          using System.Net;
          using System.Net.NetworkInformation;
          //
          namespace PingAppConsole
          {
          class Program
          {
          /// /// //////////////////////////////////////////////////////////////////////////////
          static void Main(string[] args)
          ///
          /// Purpose: the main function in the application handling the ping()communication
          ///
          /// Version: 1.0: 09-FEB-20: Adis Pipic
          ///
          {
          string host, data;
          byte[] buffer;
          int timeout;
          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.
          data = "Rodham street";
          buffer = Encoding.ASCII.GetBytes(data);
          timeout = 120;
          // Name or address of node to access
          host = "www.google.no";
          PingReply reply = pingSender.Send(host, timeout, buffer, options);
          if (reply.Status == IPStatus.Success)
          {
          Console.WriteLine(" Ping communication status for {0}:", host);
          Console.WriteLine(" ------------------------------------------");
          Console.WriteLine(" Address: {0}", reply.Address.ToString());
          Console.WriteLine(" RoundTrip time (mSec): {0}", reply.RoundtripTime);
          Console.WriteLine(" Time to live: {0}", reply.Options.Ttl);
          Console.WriteLine(" Dont fragment: {0}", reply.Options.DontFragment);
          Console.WriteLine(" Buffer size: {0}", reply.Buffer.Length);

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Use a Timer. Call your routine from the Timer's Tick event. Disable / enable the Timer each time you handle the event to avoid reentrancy with short intervals. Though with a timer and status reporting, I would probably use a Windows app instead of a Console app.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • K k5054

            put an infinite loop around the ping call

            while ( true ) {
            PingReply reply = pingSender.Send(host, timeout, buffer, options)
            // ...
            else
            {
            Console.WriteLine("Error ... ");
            }
            }

            Not sure if you could ever break out of that, though. You'd probably want to reduce that to one line of output, similar to what you get from ping from the command prompt. Oh, and you'll probably want to add a delay at the bottom of the loop so you don't flood the network with ping requests. Thread.Sleep() seems like a good option.

            A Offline
            A Offline
            auting82
            wrote on last edited by
            #5

            Thanks :) Is there a way I can check all the nodes in the network and also get the number of the nodes in my network segment?

            K 1 Reply Last reply
            0
            • A auting82

              Thanks :) Is there a way I can check all the nodes in the network and also get the number of the nodes in my network segment?

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              It used to be that most nodes on a lan would respond to a broadcast ping, but most systems do not do so any longer. Many hosts also employ firewalls to block pings. And firewalls can be used to block all connections except from certain hosts, or maybe even all incoming connections. If all your nodes are connected through a simple hub, then you might be able to sniff packets on the wire and see what the source and destination nodes are. If you're using smart switches then the switch might only forward packets between the ports the nodes are attached to, so you might not see packets. So there's not any good way to do that. There's only the brute force approach, a la [Windows | Nmap Network Scanning](https://nmap.org/book/inst-windows.html). Be aware that scanning networks where you do not have permission to do so is not advised. Its not unlike someone going around the outside of your hose checking to see if any of your doors or windows are openable. There might not be any intent to create mischief, but it certainly looks like you're trying to break in!

              A 1 Reply Last reply
              0
              • K k5054

                It used to be that most nodes on a lan would respond to a broadcast ping, but most systems do not do so any longer. Many hosts also employ firewalls to block pings. And firewalls can be used to block all connections except from certain hosts, or maybe even all incoming connections. If all your nodes are connected through a simple hub, then you might be able to sniff packets on the wire and see what the source and destination nodes are. If you're using smart switches then the switch might only forward packets between the ports the nodes are attached to, so you might not see packets. So there's not any good way to do that. There's only the brute force approach, a la [Windows | Nmap Network Scanning](https://nmap.org/book/inst-windows.html). Be aware that scanning networks where you do not have permission to do so is not advised. Its not unlike someone going around the outside of your hose checking to see if any of your doors or windows are openable. There might not be any intent to create mischief, but it certainly looks like you're trying to break in!

                A Offline
                A Offline
                auting82
                wrote on last edited by
                #7

                Ok thanks. If you would write pseudocode for it, how would it look like based on my initial code?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups