计算ping尝试

时间:2013-09-19 14:24:34

标签: vb.net ping

Imports System.Net.NetworkInformation
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Dim pingtarget As String = "88.250.204.138"
        Dim pingre As PingReply = Ping.Send(pingtarget)

        If My.Computer.Network.Ping("pingtarget", 9002) Then

           address.ForeColor = Color.Green

           Do While My.Computer.Network.Ping("pingtarget", 9002)

                Me.ListBox1.Items.Add("Response from " & pingtarget & " in " & pingre.RoundtripTime.ToString() & " ms")
                My.Computer.Network.Ping("pingtarget", 9002)

            Loop


        Else
            Timer2.Enabled = True
            address.ForeColor = Color.Red
            MsgBox("Connection Failed")
        End If

    End Sub

End Class

嘿伙计们,我刚刚开始尝试开发一个程序,它将继续ping服务器,如果连接丢失,客户端将自行重启。我有一些问题:

1-如何统计重启?否则,只要没有连接,它就会继续重启。重新启动将重新尝试重新连接互联网,但它失败太多,必须有一种方法来阻止它。例如,在3次重新启动后,即使没有连接,客户端也会运行。

2-在这些代码中,当没有连接时,我收到一条错误消息,指出“Microsoft.VisualBasic.dll中发生了'System.InvalidOperationException'类型的未处理异常”。

1 个答案:

答案 0 :(得分:0)

一个明显的问题:

更改代码的这一部分:

My.Computer.Network.Ping("pingtarget", 9002)

到此:

My.Computer.Network.Ping(pingtarget, 9002)

您将pingtarget作为字符串而不是变量传递。