C# and Ping.SendAsync

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samentu
    New Member
    • Apr 2007
    • 7

    C# and Ping.SendAsync

    Hi everybody.
    I'm trying to develop e network scanner. I've run into trouble when got to the part of scanning the IP's :) ... meaning that I need to make the scanning method non-blocking. To do so I thought the Ping.SendAsync would come in handy. I've followed this example: http://msdn2.microsoft .com/en-us/library/system.net.netw orkinformation. ping.aspx but when I try to use it in a Winforms application the WaitOne() method blocks the PingCompletedCa llback method that handles the pinging. I would really appreciate some help.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What error are you receiving?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Your link points to .net3.0 (be sure you have .net3.0 or follow over to link relative to your own version of .net)

      Why are you using the WaitOne() call? It's a blocking call. Seems like that would defeat the purpose of your async call?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I just did this, it will have run conditions, but works,
        Code:
        Ping pi = new Ping();
        private string allofit = "";
        byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
        
        void Form1_Load(object sender, EventArgs e)
        {
           pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
           int i = 1;
           pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
        }
        void pi_PingCompleted(object sender, PingCompletedEventArgs e)
        {
           PingReply pr = e.Reply;
           allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
           int i = (int)e.UserState;
           i=i+1;
           if (i < 10)
           {
              pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
           }
           else
           {
              MessageBox.Show(allofit);
           }
           //throw new Exception("The method or operation is not implemented.");
        }
        and a few secs later a window popping up telling me:
        192.168.1 {success?]
        192.168.2 {success?]
        192.168.3 {success?]
        192.168.4 {success?]
        192.168.5 {success?]
        192.168.6 {success?]
        192.168.7 {success?]
        192.168.8 {success?]
        192.168.9 {success?]

        Comment

        • samentu
          New Member
          • Apr 2007
          • 7

          #5
          Originally posted by Plater
          I just did this, it will have run conditions, but works,
          Code:
          Ping pi = new Ping();
          private string allofit = "";
          byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
          
          void Form1_Load(object sender, EventArgs e)
          {
             pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
             int i = 1;
             pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
          }
          void pi_PingCompleted(object sender, PingCompletedEventArgs e)
          {
             PingReply pr = e.Reply;
             allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
             int i = (int)e.UserState;
             i=i+1;
             if (i < 10)
             {
                pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
             }
             else
             {
                MessageBox.Show(allofit);
             }
             //throw new Exception("The method or operation is not implemented.");
          }
          and a few secs later a window popping up telling me:
          192.168.1 {success?]
          192.168.2 {success?]
          192.168.3 {success?]
          192.168.4 {success?]
          192.168.5 {success?]
          192.168.6 {success?]
          192.168.7 {success?]
          192.168.8 {success?]
          192.168.9 {success?]
          Your code works but the behaviour I'm looking for is kinda different.
          It's something like this: say I'm pinging a range of IP's starting with192.168.1.1 in the main thread using a for loop. At first the PingCompletedCa llback returns the ping statistics from 192.168.1.1; then the main thread pings 192.168.1.2. At this point the PingCompletedCa llback returns the ping statistics from another host, something like 192.168.1.32 or some other IP from the given range instead of the statistics for 192.168.1.2 and so on when the main thread gets to the next host. The point is that PingCompletedCa llback pings all the IP's but not in the order that the main thread tells it to. And I figured that WaitOne does just that - it forces PingCompletedCa llback to return the ping statistics from the current IP in the main thread. Or am I missing something? Or maybe I need another approach? I need the asynchronous ping so that the UI does not freeze while pinging the hosts.
          Thanks for all your posts, guys.

          Comment

          • mwalts
            New Member
            • May 2007
            • 38

            #6
            Originally posted by samentu
            Your code works but the behaviour I'm looking for is kinda different.
            It's something like this: say I'm pinging a range of IP's starting with192.168.1.1 in the main thread using a for loop. At first the PingCompletedCa llback returns the ping statistics from 192.168.1.1; then the main thread pings 192.168.1.2. At this point the PingCompletedCa llback returns the ping statistics from another host, something like 192.168.1.32 or some other IP from the given range instead of the statistics for 192.168.1.2 and so on when the main thread gets to the next host. The point is that PingCompletedCa llback pings all the IP's but not in the order that the main thread tells it to. And I figured that WaitOne does just that - it forces PingCompletedCa llback to return the ping statistics from the current IP in the main thread. Or am I missing something? Or maybe I need another approach? I need the asynchronous ping so that the UI does not freeze while pinging the hosts.
            Thanks for all your posts, guys.
            You're basically forcing the SendAsync to act like the normal Send. If you need them all to come back in the specific order, then you'll have to block and use Send. So do it in another thread to avoid freezing the UI thread.

            Something like

            //in a button press event or something

            new Thread (new ThreadStart(Pin gAll)).Start();

            //now to create the PingAll method

            private void PingAll()
            {
            //Do your Ping.Sends one at a time here
            }

            If you need to interact with the UI thread once the pinging is done, then you might want to look at the tutorial here
            http://www.yoda.arachs ys.com/csharp/threads/index.shtml

            You can jump ahead to the section on Forms if you want, but it might be a bit confusing.

            Have fun,

            -mwalts

            Comment

            • samentu
              New Member
              • Apr 2007
              • 7

              #7
              Originally posted by mwalts
              You're basically forcing the SendAsync to act like the normal Send. If you need them all to come back in the specific order, then you'll have to block and use Send. So do it in another thread to avoid freezing the UI thread.

              Something like

              //in a button press event or something

              new Thread (new ThreadStart(Pin gAll)).Start();

              //now to create the PingAll method

              private void PingAll()
              {
              //Do your Ping.Sends one at a time here
              }

              If you need to interact with the UI thread once the pinging is done, then you might want to look at the tutorial here
              http://www.yoda.arachs ys.com/csharp/threads/index.shtml

              You can jump ahead to the section on Forms if you want, but it might be a bit confusing.

              Have fun,

              -mwalts
              Thank you very much, mwalts. Your link proved to be very useful. Thank you again.

              Comment

              Working...