2016-08-05 60 views
0

我为时钟系统公司工作,有时无法访问路由器和其他工具来查找网络中的某些东西。扫描在端口9922上运行的东西的内部IP范围VBNet

所以我做了这个,但得到的错误。我需要能够扫描端口9922,TCP上的内部网络。

Imports System.Net.Sockets 
Imports System.Net 
Imports System.Threading 
Imports System.Text 
Module Module1 
    Dim portStart As Integer 
    Dim portEnd As Integer 
    Dim lngPort As Long 
    Dim lngRange As Long 
    Dim txtHost1 As String 
    Dim txtHost2 As String 
    Dim txtHost As String 
    Dim openPorts As Integer 
    Dim closedPorts As Integer 
    Dim range As String 
    Dim lgnRange2 As Long 
    Sub Main() 
     Console.Write("Range Start: (For example 192.168.1.1 = 192.168.1. ") 
     range = Console.ReadLine() 

     Console.Write("Host to: Example 1 ") 
     txtHost1 = Console.ReadLine() 

     Console.Write("Host from: Example 254 ") 
     txtHost2 = Console.ReadLine() 

     Console.Write("Port: (9922 for Face Rec By default)") 
     portEnd = Console.ReadLine() 
     For lngRange = txtHost1 To txtHost2 

     Next 
     For lgnRange2 = range + lngRange 
      Dim myTcpClient As New TcpClient() 
      Try 
       myTcpClient.Connect(lngPort, portEnd) 
       Console.WriteLine("Host: " + txtHost + " : ") 
       Console.WriteLine(" Port " + lngPort.ToString() + " : Open :") 
       openPorts += 1 
       myTcpClient.Close() 
      Catch ex As SocketException 
       Console.WriteLine("Host: " + txtHost + " : ") 
       Console.WriteLine(" Port " + lngPort.ToString() + " : Closed :") 
       ' Console.WriteLine(ex.Message) 
       closedPorts += 1 
       Console.Write(" " & openPorts.ToString & " open port(s) : ") 
       Console.Write("  " & closedPorts.ToString & " closed port(s) : ") 

       Console.Beep() 
       Console.Write(txtHost + " : " + portStart.ToString + " - " + portEnd.ToString + " : Scanned Sucessfully") 

      End Try 
     Next 
    End Sub 
    Public Class psAPP 
     Public Shared Sub Main() 

     End Sub 
    End Class 
End Module 
+0

你应该提供比“所以我做了这个,但得到的错误已经”更多的细节 - 什么错误是什么线?运行时或编译错误?等等。回顾[问]获取更多提示。 – Mark

+0

这行看起来不正确。对于lgnRange2 = range + lngRange'。 – Mark

+0

对不起,迟到的回应, –

回答

0

一个For循环需要一个数字开始和结束,所以你需要将txtHost1txtHost2变量转换为整数。 TcpClient.Connect调用将需要一个IPAddress或从您的硬编码前缀和当前主机号码构建的主机名。所以,你的循环可能看起来像(未经测试):

Option Strict On 
Option Infer On 
'... 
For i = Integer.Parse(txtHost1) To Integer.Parse(txtHost2) 
    Dim remoteAddr = IPAddress.Parse("192.168.1." & i) 
    Try 
     Using myTcpClient = New TcpClient() 
      myTcpClient.Connect(remoteAddr, Integer.Parse(portEnd)) 
     End Using 
     Console.WriteLine("Host: " + txtHost + " : ") 
     Console.WriteLine(" Port " + lngPort.ToString() + " : Open :") 
     openPorts += 1 
    Catch ex As SocketException 
     Console.WriteLine("Host: " + txtHost + " : ") 
     Console.WriteLine(" Port " + lngPort.ToString() + " : Closed :") 
     closedPorts += 1 
    End Try 
Next 
Console.Write(" " & openPorts.ToString & " open port(s) : ") 
Console.Write("  " & closedPorts.ToString & " closed port(s) : ")