Solved

Checking for open ports

Posted on 2003-11-09
14
970 Views
Last Modified: 2013-11-13
Hello, I want an easy and quick way to tell if a port is open on a specfic IP address for a programing im working on.  Can you help?  Maybe it would require the new winsock2???
0
Comment
Question by:kwickway
14 Comments
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9711819
You can try to connect to the ip using different ports number with a loop. If the port is open, you wil probably receive different reply code.

~ fantasy ~
0
 

Author Comment

by:kwickway
ID: 9712348
can you show me an example?
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9716572
       Winsock1.LocalPort = "0"
        Winsock1.RemoteHost = <the ip address you want to check>

        on error resume next
        for l_Port=1 to <max port you wish to check>
               Winsock1.RemotePort = l_Port
               Winsock1.Connect
               if err<>0 then ' you got an error thus the port is probably closed or timed-out or it is already opened
                     'handle error if required                    
               else
              ' you could also try to send some data, but you should know in advance what is required by the service or it will just close the connection.
              Winsock1.SendData "data to send"
              Winsock1.GetData s_Variable, vbString (the fisrt is a variable the second is the type
              Debug.print "Port " & l_Port & " is open. "
              Debug.print "Value read: " & s_Variable
              Winsock1.close
        next l_Port

Bye,

Max
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:kwickway
ID: 9716823
Ok, i tried it, i chagned a little bit so it will work, my form1 looks like this:



Private Sub Form_Load()
CheckPortState "65.144.21.70", "54"
End Sub


Public Function CheckPortState(IPAddress As String, Port As String)
        On Error Resume Next
               Winsock1.RemotePort = Port
               Winsock1.remotehost = IPAddress
               Winsock1.Connect
               If Err <> 0 Then
                     MsgBox "Host Site Down"
               Else
                    MsgBox "Host Site Up"
               End If
              Winsock1.Close
End Function


The problem is the host site showes Host Site Up no matter what I do.  It should error out but it doesnt.
0
 

Author Comment

by:kwickway
ID: 9719751
Private Sub Check1_Click()
    If Check1 = 1 Then Timer1.Enabled = True Else Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
Command1.Enabled = False
On Error Resume Next
With Winsock1
    .Close
    .RemoteHost = Text1.Text
    .RemotePort = Val(Text2.Text)
    .Connect
    While .State = 6
        DoEvents
    Wend
    'MsgBox .State
    If .State = 0 Then Label1.Caption = "Default. Closed"
    If .State = 1 Then Label1.Caption = "Open"
    If .State = 2 Then Label1.Caption = "Listening"
    If .State = 3 Then Label1.Caption = "Connection pending "
    If .State = 4 Then Label1.Caption = "Resolving host"
    If .State = 5 Then Label1.Caption = "Host resolved"
    If .State = 6 Then Label1.Caption = "Connecting"
    If .State = 7 Then Label1.Caption = "Connected"
    If .State = 8 Then Label1.Caption = "Peer is closing the connection"
    If .State = 9 Then Label1.Caption = "Error"
    If .State = 7 Then
        Label2.Caption = "Client is up"
    Else
        Label2.Caption = "Client is down"
    End If
    .Close
End With
Command1.Enabled = True
End Sub

Private Sub Text3_Change()
Timer1.Interval = Val(Text3.Text)
End Sub

Private Sub Timer1_Timer()
Command1_Click
End Sub

OK, well, i figrued this one out mostly on my own.  Sorry guys...
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9721138
I would like just to add that the localport value should be specified to correct coding.

If you do not specify, I do not know which local port will use. It is always better to specify one.

Very good job anyway kwickway


Max
0
 

Author Comment

by:kwickway
ID: 9722751
CC- Please cancle that last request to delete this, i will actually use this again.

Private Sub Check1_Click()
    If Check1 = 1 Then Timer1.Enabled = True Else Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
Command1.Enabled = False
'On Error Resume Next
Dim MyState
Dim MyTimer
With Winsock1
    .Close
    .RemoteHost = Text1.Text
    .RemotePort = Val(Text2.Text)
    .Connect
    While .State = 6
        DoEvents
    Wend
    MyState = .State
    MyTimer = Timer
    Do
    Loop Until Timer = MyTimer + 1
    MsgBox .State
    'If MyState = 0 Then Label1.Caption = "Default. Closed"
    'If MyState = 1 Then Label1.Caption = "Open"
    'If MyState = 2 Then Label1.Caption = "Listening"
    'If MyState = 3 Then Label1.Caption = "Connection pending "
    'If MyState = 4 Then Label1.Caption = "Resolving host"
    'If MyState = 5 Then Label1.Caption = "Host resolved"
    'If MyState = 6 Then Label1.Caption = "Connecting"
    'If MyState = 7 Then Label1.Caption = "Connected"
    'If MyState = 8 Then Label1.Caption = "Peer is closing the connection"
    'If MyState = 9 Then Label1.Caption = "Error"
    If MyState = 7 Then
        Label2.Caption = "Client is up"
    Else
        Label2.Caption = "Client is down"
    End If
    .Close
End With
Command1.Enabled = True
End Sub

Private Sub Form_Load()

End Sub

Private Sub Text3_Change()
Timer1.Interval = Val(Text3.Text)
End Sub

Private Sub Timer1_Timer()
Command1_Click
End Sub


Here is the new code, Now when I run it, but try using port 21, i thought it worked great tell last night, and last night when i took a computer down (turned it off), it still was showing it was ok.

The problem here is, it errors out and the "on error resume next" overides that, i remed it out and it shows now that there is an error in winsock, i don't know how to reset it.  I trid closling it, but it still errors out.
0
 

Author Comment

by:kwickway
ID: 9750010
anyone???  I need help...
0
 

Author Comment

by:kwickway
ID: 9845214
Again, Now when I run it, but try using port 21, i thought it worked great tell last night, and last night when i took a computer down (turned it off), it still was showing it was ok.

The problem here is, it errors out and the "on error resume next" overides that, i remed it out and it shows now that there is an error in winsock, i don't know how to reset it.  I trid closling it, but it still errors out.
0
 

Accepted Solution

by:
amp072397 earned 0 total points
ID: 10528729
PAQed - no points refunded (of 40)

amp
Community Support Moderator amp~at~experts-exchange.com
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust the position 3 65
Programatically extract date from website 8 79
which modules are active in VB6 project? 6 41
adding "ungroup sheets" to existing vbs code 5 30
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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