HOW TO:Ping IP

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Console.WriteLine(Me.CheckIP("127.0.0.1"))
        Console.WriteLine(Me.CheckIP("127.0.0.2"))
        Console.WriteLine(Me.CheckIP("lzmtw.cnblogs.com"))
        Console.WriteLine(Me.CheckIP("LocalHost"))
        Console.WriteLine(Me.CheckIP("Localhost1"))
    End Sub

    Public Function CheckIP(ByVal NameOrIP As String) As Boolean
        Dim IsByIP As Boolean
        IsByIP = (NameOrIP.Split("."c).Length = 4)
        Try
            If IsByIP Then
                Console.WriteLine(System.Net.Dns.GetHostByAddress(NameOrIP).HostName)
            Else
                Console.WriteLine(System.Net.Dns.GetHostByName(NameOrIP).HostName)
            End If
            Return True
        Catch ex As System.Net.Sockets.SocketException
            Return False
        End Try
    End Function

你可能感兴趣的:(ping)