2013-07-16 87 views
0

所以,我正在製作一個端口掃描器,並有一個最小和最大端口,但不能讓端口掃描器在到達最大端口時停止掃描? 我已經嘗試做一個退出當端口達到portmax。vb For循環重複一定次數

下面是代碼:

Public Class Form1 

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Dim counter As Integer 


    Button2.Enabled = False 
    'set counter explained before to 0 
    counter = 0 
End Sub 
Private Sub Timer1_Tick(ByVal sender As System.Object, _ 
     ByVal e As System.EventArgs) Handles Timer1.Tick 
    Dim host As String 

    Dim counter As Integer 
    Dim portmin As Integer = TextBox3.Text 
    Dim portmax As Integer = TextBox2.Text 
    'Set the host and port and counter 
    counter = counter + 1 'counter is for the timer 
    host = TextBox1.Text 

     For port As Integer = portmin To portmax 




      ' Next part creates a socket to try and connect 
      ' on with the given user information. 

      Dim hostadd As System.Net.IPAddress = _ 
       System.Net.Dns.GetHostEntry(host).AddressList(0) 
      Dim EPhost As New System.Net.IPEndPoint(hostadd, port) 
      Dim s As New System.Net.Sockets.Socket(_ 
      System.Net.Sockets.AddressFamily.InterNetwork, _ 
     System.Net.Sockets.SocketType.Stream, _ 
      System.Net.Sockets.ProtocolType.Tcp) 

      Try 
       s.Connect(EPhost) 
      Catch 
      End Try 


      If Not s.Connected Then 
       ListBox1.Items.Add("Port " + port.ToString + " is not open") 


      Else 
       ListBox1.Items.Add("Port " + port.ToString + " is open") 
       ListBox2.Items.Add(port.ToString) 



      End If 
      Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString 


     Next 






End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, _ 
      ByVal e As System.EventArgs) Handles Button2.Click 
    'stop button 
    Timer1.Stop() 
    Timer1.Enabled = False 
    Button1.Enabled = True 
    Button2.Enabled = False 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles Button1.Click 
    ListBox1.Items.Add("Scanning: " + TextBox1.Text) 
    ListBox1.Items.Add("-------------------") 
    Button2.Enabled = True 
    Button1.Enabled = False 
    Timer1.Enabled = True 
    Timer1.Start() 
End Sub 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 

End Sub 

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged 

End Sub 

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged 

End Sub 

End Class 

我真的很感激任何幫助 感謝,

回答

1

嘗試在掃描過程中停止定時器。

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

    'Your code 

    Timer1.Enabled = True 
End Sub 

如果那是問題,而且看起來它,你應該考慮使用try/catch塊:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    Try 
     Timer1.Enabled = False 

     'Your code 

    Catch ex As Exception 
     'Manage the error 
    Finally 
     Timer1.Enabled = True 
    End Try 
End Sub 
+0

你能告訴我的全部代碼,因爲我 – Gabe

+0

你有什麼問題?你不需要修改你的代碼,只需在循環之前添加這兩行('Timer1.Enabled = False'),而循環之後的另一行('Timer1 .Enabled = True')。 – SysDragon

+0

輸出爲 端口1未打開 端口1未打開 端口1未打開 端口1未打開 端口2未打開 端口2未打開 端口2未打開 等 – Gabe

0

試試這個

對於端口作爲整數= portmin要portmax - 1

+0

它不工作:( – Gabe