C # pingreply IPaddress in win Vista +

Source: Internet
Author: User

Hello,

I'm using the following code to perform a ping:

Ping P = new Ping ();

P. pingcompleted + = new pingcompletedeventhandler (p_pingcompleted );

P. sendasync (New IPaddress (New byte [] {10, 2, 1, 25}), 1000 );

P. Dispose ();

And then I'm catching the reply this way:

Void p_pingcompleted (Object sender, pingcompletedeventargs e ){

Pingreply Pr = E. reply;

IPaddress the_pinged_ip = Pr. address;

}

My question, this works perfectly fine on Windows XP, but: on vista I get a reply from 10.2.1.139 (my own PC's address) if the ping fails. (similar to the 'ping' command in DOS)

Is there a way to find the actual ip that was pinged?

Thanks in advance,


Sam


Roger affirmative, mi amigo.
 



Your solution I believe lies in a parameter of the ping. sendasync method, known as object usertoken.

Change these lines it shocould work fine.

Change your sender:

System. net. IPaddress IP = (New byte [] {10, 2, 1, lowbyte}); // or whatever code works with your for... next loop.

P. sendasync (IP, 1000, ip) // The second "ip" fills the usertoken parameter.

Then in the reply:

System. net. IPaddress callerip = (system. net. IPaddress) E. userstate;

That shoshould work just fine. if you're getting errors, you might have to send it through as a string and convert it back at the other end, but I don't think you'll have to go thru that much trouble. let me know if it doesn' t work.





Oh my f * cking God !!!!

You have no idea how much I love you right now, I 've been searching for something like this for 2 days straight, I 've tried to find ways around und it using self programmed Ping classes with sockets ,.. but nothing worked

This does the trick,

Thank you soooo much.

God bless you,

Kind regards,

Sam

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.