Check online status, for example by pinging

只谈情不闲聊 提交于 2019-12-10 20:15:17

问题


I am developing an universal windows app for Windows 10 IoT using VB.NET. I am checking for two things - first is, if there is any network at all. I am using

Imports System.Net
NetworkInformation.NetworkInterface.GetIsNetworkAvailable

for this. But what this does not tell me is if I really do have internet access, it only indicates if I am connected to a network.

Is there any way to ping an address (like 8.8.8.8)? I cannot find a solution. The device will only be used for private purposes, the app will not be public, if that information is necessary.


回答1:


You can use:
My.Computer.Network.Ping("192.168.1.1") to ping an ip
or
My.Computer.Network.Ping("www.google.com") to ping a url

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If My.Computer.Network.Ping("192.168.1.1") Then
        MsgBox("Connection ok")
    Else
        MsgBox("No Connection")
    End If
End Sub



回答2:


This proposal text shows you how to definetly connected to the internet. For example it returns true if google.com is reachable. So you don't need any ping.

''' <summary>
''' check for a existing internet connection to www.google.com
''' </summary>
''' <returns>True if it's exist</returns>
Public Shared Function isConnected() As Boolean
    Try
        Dim addresslist As IPAddress() = Dns.GetHostAddresses("www.google.com")
        ' | ' addresslist holds a list of ipadresses to google
        ' | ' e.g  173.194.40.112                                       |
        ' | '                .116                                       |
        If addresslist(0).ToString().Length > 6 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Sockets.SocketException
        ' | ' You are offline                   |
        ' | ' the host is unkonwn               |
        Return False
    Catch ex As Exception
        Return False
    End Try
End Function


来源:https://stackoverflow.com/questions/32936840/check-online-status-for-example-by-pinging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!