如何检测一个主机能不能连接得上?
我的本意是想要用检测login.minecraft.net连不连得上,如果连得上,就发送POST数据,如果超过一段时间仍然连不上就不发送并且弹一个消息框,
测试的时候,我连着网络测试时正常的,
但是我断开网络测试时,它提示w.Connect("login.minecraft.net", 80)这一句错误,说是:“找不到这样的主机”。我是希望检测这个主机的状态,请问各位我应该怎么做才行?在此感激不尽。
下面是我现在的代码
网络 vb.net TcpClient
Dim w As New Net.Sockets.TcpClient
w.Connect("login.minecraft.net", 80)
Dim ot As Integer = 1000
Dim nw As Boolean = False
Do Until w.Connected
ot -= 1
Threading.Thread.Sleep(16)
If ot <= 0 Then
MsgBox("连接超时")
nw = True
Else
End If
Loop
If nw = False Then
Dim wc As New WebClient()
Dim postData As New Text.StringBuilder()
postData.Append("user=" + IDInput.Text)
postData.Append("&password=" + PWInput.Text)
postData.Append("&version=" + "13")
Dim sendData As Byte() = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString())
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
wc.Headers.Add("ContentLength", sendData.Length.ToString())
Dim recData As Byte() = wc.UploadData("http://login.minecraft.net/", "POST", sendData)
MsgBox(System.Text.Encoding.GetEncoding("UTF-8").GetString(recData))
Else
MsgBox("无法连接到登录服务器,请确认网络是否正常。")
End If
Using cn As New SqlConnection(CnStr)
cn.Open()
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function