在.NET中查找数据包丢失和跟踪路由

时间:2009-03-15 19:30:17

标签: .net vb.net winforms network-programming network-protocols

我正在尝试编写代码以捕获计算机上的数据包丢失,但我能够这样做的唯一方法是运行NetStat.exe和TraceRt.exe进程并将它们捕获到列表框中(使用代码如下):

Private Sub myProcess()
    Dim p As System.Diagnostics.Process
    Dim theFile, sTemp, sLineOut, mySent, myRetrans As String
    Dim intSentStart As Double
    Dim isEnabled As Boolean

    p = New System.Diagnostics.Process
    theFile = t.Name

    If File.Exists("c:\windows\system32\" & theFile) Then
        theFile = "c:\windows\system32\" & theFile
    Else
        MessageBox.Show("Unable to find the " & theFile & " file on your computer.", "File Find Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

    Select Case t.Name
        Case "TraceRt.exe"
            p.StartInfo.Arguments = "-h 30 www.stackoverflow.com"

        Case "NetStat.exe"
            p.StartInfo.Arguments = "-s -p tcp"
    End Select

    Try
        p.StartInfo.FileName = theFile
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.Start()
        p.PriorityClass = System.Diagnostics.ProcessPriorityClass.Normal

        sLineOut = ""

        Do While Not p.HasExited
            sTemp = p.StandardOutput.ReadLine()
            If sTemp <> "" Then
                sLineOut = sLineOut & sTemp & vbNewLine
                'Send to listbox
                setTrace(sTemp)
            End If
        Loop
        If sLineOut = "" Then
            'Clear Listbox
            clearTrace()
            myProcess()
            Exit Sub
        End If
        If t.Name = "NetStat.exe" Then
            Try
                If lboxTrace.Items.Count < 9 Then
                    'Clear Listbox
                    clearTrace()
                    myProcess()
                    Exit Sub
                End If
                mySent = ""
                mySent = lboxTrace.Items.Item(8) '("Segments Sent")
                mySent = Trim(mySent.Replace("Segments Sent", ""))
                mySent = Trim(mySent.Replace("=", ""))

                myRetrans = ""
                myRetrans = lboxTrace.Items.Item(9)
                myRetrans = Trim(myRetrans.Replace("Segments Retransmitted", ""))
                myRetrans = Trim(myRetrans.Replace("=", ""))

                intSentStart = Math.Round((myRetrans / mySent) * 100, 2)

                'setTrace sends data to the listbox
                If intSentStart < 2 Then
                    setTrace("Your Current Packet Loss is: " & intSentStart & "%.")
                    setTrace("Your packet loss is within acceptable ranges.")
                Else
                    setTrace("Your Current Packet Loss is: " & intSentStart & "%.")
                    setTrace("Your packet loss is below acceptable ranges!")
                    setTrace("Please contact your Internet Provider about your Internet Connection.")
                End If

            Catch ex As Exception
                'Clear Listbox
                clearTrace()
                myProcess()
                Exit Sub
            End Try
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

有没有人有更好的想法来获取这些信息?我宁愿使用托管代码而不是通过这种方式进行黑客攻击。

提前致谢! -JFV

1 个答案:

答案 0 :(得分:1)

不确定这是否是您要找的,但是您是否看过Ping class

编辑:实际上对于TraceRoute,请看一下这里的答案:TraceRoute and Ping in C#

对于Netstat,请查看http://towardsnext.wordpress.com/2009/02/09/netstat-in-c/