Monitoring a port

Does anyone have any sample code on how to monitor a port?
I would like to write a small vb app that will monitor ports 21 and a few others.  If someone connects to this port the app should be able to capture information about this connection.

Any help would be great.
LVL 1
jtjcompAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
VincentLawlorConnect With a Mentor Commented:
Heres a fairly simple example
 
Creat a new project
Add a winsock control
Make it a control array

You then need an array to store the port numbers you wish to monitor

So add the following code as an example:


Dim lPorts(1 To 4) As Long
Private Sub Form_Load()

   
    lPorts(1) = 10
    lPorts(2) = 20
    lPorts(3) = 30
    lPorts(4) = 40
   
    For i = 1 To UBound(lPorts)
   
        Load Winsock1(i)
        With Winsock1(i)
            .LocalPort = lPorts(i)
            .Listen
        End With
       
    Next i

End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)

    If Winsock1(Index).State <> sckClosed Then Winsock1(Index).Close
    Winsock1(Index).Accept requestID

End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)

    Dim strData As String
    Winsock1(Index).GetData strData, vbString
    Debug.Print Str(Winsock1(Index).LocalPort) + ": Data " + strData


End Sub

Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

    Debug.Print Str(Index) + ": Error " + Str(Number) + ": " + Description

End Sub


To test that it works.
Open a new project and add another winsock control. Paste in the following code

Private Sub Command1_Click()

    Winsock1.SendData "Testing"

End Sub

Private Sub Form_Load()

    If Winsock1.State = sckOpen Then Winsock1.Close
   
    Winsock1.Connect "localhost", 20

End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

    Debug.Print DescriptionPrivate Sub Command1_Click()

    Winsock1.SendData "Testing"

End Sub

Private Sub Form_Load()

    If Winsock1.State = sckOpen Then Winsock1.Close
   
    Winsock1.Connect "localhost", 20

End Sub


Hit F5 on the first example and then F5 on the other and look at the debug window.

Change the port number to another one in the array.

Private Sub Form_Load()

    If Winsock1.State = sckOpen Then Winsock1.Close
   
    Winsock1.Connect "localhost", 10

End Sub

You will see that the second time the message comes to port 20.

Or use something like telnet.

Is that what you are looking for ?

Vin
0
 
charlescopeCommented:
Check out Planet Source Code and do a search on ports. Their are lots of programs to chose from.
http://www.planetsourcecode.com
0
 
PaulHewsCommented:
If you are monitoring port 80, you will want a .close followed by a .listen in the DataRecieved event.  Reason being that an http request will wait for the port to be closed or it will hang onto the connection waiting for more data to come back.  Don't know how the other protocols manage this.  This is just to back up VincentLawlor's excellent example (I think he should get the points) but this is a little program I wrote to trap http requests and log them to a file.  It is basically to catch code red and sadmind attacks from my ISP so I can forward the log to the abuse address at my ISP.  The GetHostFromIP function is just a reverse DNS lookup I found in some library somewhere on the net.

Option Explicit

Private mlngCount As Long

Private Sub Form_Load()
    With Winsock1
    .LocalPort = 80
'    Debug.Print .LocalIP
    .Listen
    End With
End Sub

Private Sub Form_Resize()
    picPrint.Move 300, 300, Me.ScaleWidth - 600, Me.ScaleHeight - 600
End Sub

Private Sub Winsock1_Close()
    Debug.Print "Closed"
End Sub

Private Sub Winsock1_Connect()
    Debug.Print "Connect"
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    If Winsock1.State <> sckClosed Then Winsock1.Close
        Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strFile As String
    Dim strHost As String
    Dim strIP As String
    Dim hFile As Integer
    Dim vData As Variant
   
    picPrint.Cls
   
   
    strFile = "C:\Windows\Desktop\"
   
    'increment count
    mlngCount = mlngCount + 1
   
    strIP = Winsock1.RemoteHostIP
    strHost = GetHostFromIP(strIP)
    If InStr(1, strHost, "myisp", vbTextCompare) Then
        strFile = strFile & "P80_MyISP.txt"
    Else
        strFile = strFile & "P80_Other.txt"
    End If
    hFile = FreeFile
    Open strFile For Append As #hFile
   
    sPrint String(65, 42), hFile
    sPrint "Record: " & mlngCount, hFile
    sPrint "Date: " & Format(Now, "dd-mmm-yyyy hh:nn"), hFile
    sPrint "From IP: " & Winsock1.RemoteHostIP, hFile
    sPrint "HostName: " & strHost, hFile
   
   
   Winsock1.GetData vData, vbString, bytesTotal
   sPrint "Data Received:", hFile
   sPrint vData, hFile
   
   Close hFile
   DoEvents
   
   Winsock1.Close
   Winsock1.Listen
End Sub


Public Sub sPrint(ByVal strText As String, hFile As Integer)
    Debug.Print strText
    Print #hFile, strText
   
    picPrint.Print strText
End Sub
0
 
jtjcompAuthor Commented:
Great sample code, sorry it took so long to reply...


J
0
All Courses

From novice to tech pro — start learning today.