Visual Basic .NET UDP / TCP Scanner
Benötigte Komponenten:
1 x ListView, 2 x Button, 2 x BackgroundWorker, 3 x TextBox, 3 x Label
Eigenschaften der Komponenten:
Listview: View auf Details, Columns 3 Spalten adden
Code:
Public Class Form1
Public startPort As Integer
Public endPort As Integer
Public IPAddress As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
IPAddress = TextBox1.Text
startPort = TextBox2.Text
endPort = TextBox3.Text
For startPort = startPort To endPort
Me.BackgroundWorker1.RunWorkerAsync()
While Me.BackgroundWorker1.IsBusy = True
Application.DoEvents()
End While
Next
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim lvi As New ListViewItem(startPort)
Dim tryToConnect As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient(IPAddress, startPort)
lvi.SubItems.Add("is offen")
lvi.ForeColor = Color.Red
lvi.EnsureVisible()
BackgroundWorker1.ReportProgress(0, lvi)
Catch ex As Exception
Dim lvi As New ListViewItem(startPort)
lvi.SubItems.Add("ist zu")
lvi.SubItems.Add(ex.Message)
lvi.ForeColor = Color.Green
BackgroundWorker1.ReportProgress(0, lvi)
End Try
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim lvi As New ListViewItem
lvi = e.UserState
Me.ListView1.Items.Add(lvi)
lvi.EnsureVisible()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
BackgroundWorker2.WorkerReportsProgress = True
BackgroundWorker2.WorkerSupportsCancellation = True
IPAddress = TextBox1.Text
startPort = TextBox2.Text
endPort = TextBox3.Text
For startPort = startPort To endPort
Me.BackgroundWorker2.RunWorkerAsync()
While Me.BackgroundWorker2.IsBusy = True
Application.DoEvents()
End While
Next
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Try
Dim lvi As New ListViewItem(startPort)
Dim tryToConnect As System.Net.Sockets.UdpClient = New System.Net.Sockets.UdpClient(IPAddress, startPort)
lvi.SubItems.Add("ist offen")
lvi.ForeColor = Color.Red
BackgroundWorker2.ReportProgress(0, lvi)
Catch ex As Exception
Dim lvi As New ListViewItem(startPort)
lvi.SubItems.Add("is zu")
lvi.SubItems.Add(ex.Message)
lvi.ForeColor = Color.Green
BackgroundWorker2.ReportProgress(0, lvi)
End Try
End Sub
Private Sub BackgroundWorker2_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Dim lvi As New ListViewItem
lvi = e.UserState
Me.ListView1.Items.Add(lvi)
lvi.EnsureVisible()
End Sub
End Class
Das Programm ist natürlich kein fertiges Programm, es zeigt nur was möglich wäre. Es treten folgende Fehler auf. Wenn das Programm ohne Eingabe von IP-Adresse, Start-Port und End-Port gestartet wird, so stürzt das Programm ab. Man könnte optional noch eine ProgressBar einbauen.