Why SEND() method of PING class throws an error and not return the correct code defined in IPStatus

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
hi,
this code: (a modified form of the code found at http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx" target="_blank
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx )
<pre><span style="color:Blue using
System;<br/>
<span style="color:Blue using
System.Net;<br/>
<span style="color:Blue using
System.Net.NetworkInformation;<br/>
<span style="color:Blue using
System.Text;<br/>
<br/>
<span style="color:Blue namespace
Examples.System.Net.NetworkInformation.PingTest<br/>
{<br/>
<span style="color:Blue public
<span style="color:Blue class
PingExample<br/>
{<br/>
<span style="color:Green // args[0] can be an IPaddress or host name.
<br/>
<span style="color:Blue public
<span style="color:Blue static
<span style="color:Blue void
Main (<span style="color:Blue string
[] args)<br/>
{<br/>
Ping pingSender = <span style="color:Blue new
Ping ();<br/>
PingOptions options = <span style="color:Blue new
PingOptions ();<br/>
<br/>
<span style="color:Green // Use the default Ttl value which is 128,
<br/>
<span style="color:Green // but change the fragmentation behavior.
<br/>
options.DontFragment = <span style="color:Blue true
;<br/>
<br/>
<span style="color:Green // Create a buffer of 32 bytes of data to be transmitted.
<br/>
<span style="color:Blue string
data = <span style="color:#a31515 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
;<br/>
<span style="color:Blue byte
[] buffer = Encoding.ASCII.GetBytes (data);<br/>
<span style="color:Blue int
timeout = 120;<br/>
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);<br/>
<span style="color:Blue if
(reply.Status == IPStatus.Success)<br/>
{<br/>
Console.WriteLine (<span style="color:#a31515 "Address: {0}"
, reply.Address.ToString ());<br/>
Console.WriteLine (<span style="color:#a31515 "RoundTrip time: {0}"
, reply.RoundtripTime);<br/>
Console.WriteLine (<span style="color:#a31515 "Time to live: {0}"
, reply.Options.Ttl);<br/>
Console.WriteLine (<span style="color:#a31515 "Dont fragment: {0}"
, reply.Options.DontFragment);<br/>
Console.WriteLine (<span style="color:#a31515 "Buffer size: {0}"
, reply.Buffer.Length);<br/>
}<br/>
else if (reply.Status == IPStatus.HardwareError) Console.WriteLine("hw error"0);// added by me<br/>
else if (reply.Status == IPStatus.Unknown) Console.WriteLine("unknown");// added by me<br/>
else if (reply.Status == IPStatus.TimedOut) Console.WriteLine("timeout");// added by me<br/>
}<br/>
}<br/>
}<br/>
[/code]
handles only 2 ip statuses SUCCESS and TIMEOUT. for the other statuses it throws an exception.<br/>
for example: i run this code when my ADSL modem is on, and it works for the 2 statues, but when i turn off my ADSL MODEM it throws an error and the program is closed.
what can i do to handle all the statues defined in IPStatus enum ?
<br/>

View the full article
 
Back
Top