Solved

Monitoring a port

Posted on 2001-09-06
4
323 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

911 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

23 Experts available now in Live!

Get 1:1 Help Now