vb.net 检查在线状态,例如通过 ping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32936840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check online status, for example by pinging
提问by user2694534
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
我正在使用 VB.NET 为 Windows 10 IoT 开发一个通用的 Windows 应用程序。我正在检查两件事 - 首先是,是否有任何网络。我在用
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.
有什么办法可以 ping 一个地址(比如 8.8.8.8)?我找不到解决办法。该设备将仅用于私人目的,如果需要该信息,该应用程序将不会公开。
回答by Creator
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
您可以使用:
My.Computer.Network.Ping("192.168.1.1") ping ip
或
My.Computer.Network.Ping("www.google.com") ping 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
回答by Renate Neumann
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.
该提案文本向您展示了如何明确连接到互联网。例如,如果 google.com 可访问,则返回 true。所以你不需要任何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

