Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 977
  • Last Modified:

Checking for open ports

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
kwickway
Asked:
kwickway
1 Solution
 
fantasy1001Commented:
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
 
kwickwayAuthor Commented:
can you show me an example?
0
 
MaxPolCommented:
       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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kwickwayAuthor Commented:
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
 
kwickwayAuthor Commented:
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
 
MaxPolCommented:
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
 
kwickwayAuthor Commented:
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
 
kwickwayAuthor Commented:
anyone???  I need help...
0
 
kwickwayAuthor Commented:
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
 
amp072397Commented:
PAQed - no points refunded (of 40)

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now