这是我的代码:
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim socketz As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
Dim bytedata(4096) As Byte
Dim myip As IPAddress
Dim started As Boolean = True
Dim sizediff As Size
Dim formloaded As Boolean = False
Dim FilterIPAddress As New IPAddress(0)
Dim FilterIP As Boolean
Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
'datagridview1 Update stuff
Dim stringz As String = ""
Dim Typez As String = ""
Dim ipfrom As IPAddress
Dim ipto As IPAddress
Dim destinationport As String = ""
Dim sourceport As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sizediff.Height = Me.Height - DataGridView1.Height
sizediff.Width = Me.Width - DataGridView1.Width
formloaded = True
mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
For i = 0 To mycomputerconnections.Length - 1
Combobox1.Items.Add(mycomputerconnections(i).Name)
Next
End Sub
Private Sub OnReceive(ByVal asyncresult As IAsyncResult)
If started = True Then
'Get Length of packet (including header)
Dim readlength As UInteger = BitConverter.ToUInt16(Byteswap(bytedata, 2), 0)
sourceport = BitConverter.ToUInt16(Byteswap(bytedata, 22), 0)
destinationport = BitConverter.ToUInt16(Byteswap(bytedata, 24), 0)
'Get Protocol Type
If bytedata(9) = 6 Then
Typez = "TCP"
ElseIf bytedata(9) = 17 Then
Typez = "UDP"
Else
Typez = "???"
End If
'Get IP from and to
ipfrom = New IPAddress(BitConverter.ToUInt32(bytedata, 12))
ipto = New IPAddress(BitConverter.ToUInt32(bytedata, 16))
'If this is a packet to/from me and not from myself then...
If (ipfrom.Equals(myip) = True Or ipto.Equals(myip) = True) And ipto.Equals(ipfrom) = False Then
If FilterIP = False Or (FilterIP = True And (FilterIPAddress.Equals(ipfrom) Or FilterIPAddress.Equals(ipto))) Then
'Fix data
stringz = ""
For i = 26 To readlength - 1
If Char.IsLetterOrDigit(Chr(bytedata(i))) = True Then
stringz = stringz & Chr(bytedata(i))
Else
stringz = stringz & "."
End If
Next
'Put data to DataGridView, since it's on a different thread now, invoke it
DataGridView1.Invoke(New MethodInvoker(AddressOf datagridview1Update))
End If
End If
End If
'Restart the Receiving
socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
End Sub
Private Sub datagridview1Update()
'Remove rows if there are too many
If DataGridView1.Rows.Count > 9 Then
DataGridView1.Rows.RemoveAt(0)
End If
DataGridView1.Rows.Add()
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value = ipfrom.ToString 'From Column, size at 125
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(1).Value = ipto.ToString 'To Column, size at 125
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(2).Value = destinationport.ToString
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(3).Value = sourceport.ToString
End Sub
Private Function Byteswap(ByVal bytez() As Byte, ByVal index As UInteger)
Dim result(1) As Byte
result(0) = bytez(index + 1)
result(1) = bytez(index)
Return result
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If started = True Then
Button1.Text = "Start"
started = False
Else
Button1.Text = "Stop"
started = True
End If
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If formloaded = True Then
DataGridView1.Size = Me.Size - sizediff
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combobox1.SelectedIndexChanged
For i = 0 To mycomputerconnections(Combobox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
If mycomputerconnections(Combobox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
myip = mycomputerconnections(Combobox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address
BindSocket()
End If
Next
End Sub
Private Sub BindSocket()
Try
socketz.Bind(New IPEndPoint(myip, 0))
socketz.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
Dim bytrue() As Byte = {1, 0, 0, 0}
Dim byout() As Byte = {1, 0, 0, 0}
socketz.IOControl(IOControlCode.ReceiveAll, bytrue, byout)
socketz.Blocking = False
ReDim bytedata(socketz.ReceiveBufferSize)
socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
Combobox1.Enabled = False
Catch ex As Exception
Combobox1.BackColor = Color.Red
End Try
End Sub
End Class
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
If TextBox1.Text <> "" And TextBox1.Text IsNot Nothing Then
FilterIPAddress = IPAddress.Parse(TextBox1.Text)
FilterIP = True
TextBox1.BackColor = Color.LimeGreen
Else
FilterIP = False
TextBox1.BackColor = Color.White
End If
Catch ex As Exception
FilterIP = False
TextBox1.BackColor = Color.White
End Try
End Sub
我试图过滤我的DataGridView只显示源端口列的TextBox1值。