Solved

Monitoring a port

Posted on 2001-09-06
4
320 Views
Last Modified: 2013-11-13
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.
0
Comment
Question by:jtjcomp
4 Comments
 
LVL 2

Expert Comment

by:charlescope
ID: 6462446
Check out Planet Source Code and do a search on ports. Their are lots of programs to chose from.
http://www.planetsourcecode.com
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 50 total points
ID: 6462448
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6462733
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
 
LVL 1

Author Comment

by:jtjcomp
ID: 6470849
Great sample code, sorry it took so long to reply...


J
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now