Visual Basic ICMP Pinger?

时间:2015-04-28 06:04:49

标签: vb.net visual-studio network-programming ping icmp

我正在构建一个基本的ICMP Pinger。 我希望能够确定目标地址,超时,数据包大小以及要发送的数据包数量。

我只有少量的编程知识,有没有人有任何建议?或者更有效的方式来运行程序?

到目前为止,这是我的工作。

选项明确开启 选项推断关闭

导入System.Net.NetworkInformation Imports System.ComponentModel

Public Class Form1

Private WithEvents bwPing As New BackgroundWorker
Private pingTarget As String
Private pingsize As Integer
Private numOfpings As Integer
Dim timeout As Integer

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    ' start pinger
    bwPing.WorkerReportsProgress = True
    bwPing.WorkerSupportsCancellation = True
    pingTarget = TextBox2.Text
    timeout = ComboBox4.Text

    If Not bwPing.IsBusy Then bwPing.RunWorkerAsync()

    If ComboBox3.Text & ComboBox1.Text = "" Then
        bwPing.CancelAsync()
        ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
        MsgBox(" By failing to prepare, you are preparing to fail. ", MsgBoxStyle.Exclamation)
    ElseIf ComboBox3.Text = "" Then
        bwPing.CancelAsync()
        ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
        MsgBox(" How many troops are you sending in? ", MsgBoxStyle.Exclamation)
    ElseIf ComboBox1.Text = "" Then
        bwPing.CancelAsync()
        ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
        MsgBox(" How strong are your soldiers? ", MsgBoxStyle.Exclamation)
    Else : numOfpings = CInt(ComboBox3.Text)
        pingsize = CInt(ComboBox1.Text)
    End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    ' cancel pinger
    bwPing.CancelAsync()
End Sub

Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
    ' clear
    ListBox1.Items.Clear()
    ComboBox1.Text = ""
    ComboBox3.Text = ""
    ProgressBar1.Value = 0
End Sub

Private Sub bwPing_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bwPing.DoWork
    ' ping worker
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    Dim packet(pingsize) As Byte

    For i As Integer = 0 To numOfpings - 1
        bwPing.ReportProgress(i + 1)
        Dim ping As New Ping
        Dim reply As PingReply = ping.Send(pingTarget, timeout)
        If ComboBox3.Text & ComboBox1.Text = "" Then
            bwPing.CancelAsync()
        Else
            ListBox1.Items.Add("You hit " & pingTarget & " in " & reply.RoundtripTime.ToString() & " ms with " & pingsize & " bytes.")
            System.Threading.Thread.Sleep(500)
        End If

        If worker.CancellationPending Then Exit For
    Next

End Sub

    Private Sub bwPing_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles bwPing.ProgressChanged
    ' update results
    Me.ProgressBar1.Value = e.ProgressPercentage
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = numOfpings
End Sub

Private Sub bwPing_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bwPing.RunWorkerCompleted
    ' finished
    Me.ListBox1.Items.Add("*!* The battle is over, but not the war *!*")
    bwPing.CancelAsync()

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Control.CheckForIllegalCrossThreadCalls = False
End Sub

结束班

0 个答案:

没有答案