Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

ASP.NET: System.Net Ping Help!

Hi,

I need help with finishing what i started below. I'm very new to this namespace and found a few tutorials online, but do not know how to handle if an hostname / IP is un pingable.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""
            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)
            txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
            txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
            txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
            txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

        Catch err As Exception

            lblStatus.Text = err.Message
        End Try
    End Sub
Avatar of raterus
raterus
Flag of United States of America image

Avatar of Brian

ASKER

raterus,

Yes, your right. I tried implenting that last night and I kept getting the following error message below. I'm not sure If i have the code in the right place or not. I'm adding the code below withing the Try should I have this somewhere else. Also, it doesnt matter what I add after If pingreply.Status =. I tried them all last night and got the same error.

If pingreply.Status = IPStatus.BadDestination Then
                label4.Text = "Host Unreachable"
End If

Error Message:
Object reference not set to an instance of an object.
Learning with you here, Your pingreply object is empty, you probably can do something like this as a failsafe
Dim pingreply As PingReply = ping.Send(txtHost.Text)
 
if Not pingreply is Nothing Then
  txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
  txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
  txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
  txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
else
  txtPing.Text = "Not working"
end if

Open in new window

Avatar of Brian

ASKER

:(
I keep getting that pesky error.

Object reference not set to an instance of an object.
Post your troublesome code right now, the whole mess of the ping stuff.
Avatar of Brian

ASKER

below is my codebehind:

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text

Partial Class pingutility
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""

            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)
                txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
                txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
                txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
                txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

        Catch err As Exception
            lblStatus.Text = err.Message
        End Try
    End Sub
End Class
Of course you're still getting the error, you didn't implement the last code example I shared :-p
Avatar of Brian

ASKER

raterus,

I did implement what you sent me but it still produced the same error. The code above was my original code. Below is the code that you provided.

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text

Partial Class pingutility
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""

            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)

            If Not pingreply Is Nothing Then
                txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
                txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
                txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
                txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
            Else
                txtPing.Text = "Not working"
            End If

            'Dim pingreply As PingReply = ping.Send(txtHost.Text)

            'If Not pingreply Is Nothing Then
            '    txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
            '    txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
            '    txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
            '    txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
            'Else
            '    txtPing.Text = "Not working"
            'End If
     
        Catch err As Exception

            lblStatus.Text = err.Message
        End Try
    End Sub
End Class
If txtHost.Text is empty, then an ArgumentNullException will be raised.
I meant "thrown", not "raised". Sorry.
Avatar of Brian

ASKER

prairedog,

What happens is that when i enter a host name for example www.microsoft.com and hit my Ping button then that is when I get the error message because they must have ping disabled. If I ping www.asp.net then the info is displayed in txtPing.Text. It only works when the remote computer is pingable if not then I get the error message.
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Prairiedog,

Thank you very much. That worked out just as i needed. Thank you very much for your help.
Glad it worked out. But honestly, I can't take the full credits for the answer...I believe raterus's comments are very helpful.
You finally stalked my answers enough to steal one, <raspberry> :-)
Sorry, raterus. I didn't mean to. I waited for about one hour and didn't see any comments from you, so I thouhgt you might have moved on to another question. I really hope asp_net2 can split the points.
Believe me, no offense taken, points really don't matter anyway when you're in the range I'm in.  This isn't the first time something like this has happened.  You learn to move on with life.
>>You learn to move on with life.
That explains why you are Genius and I am only Master. Still learning. :-)
Thanks for your commnet. It really made me feel a lot better.