Solved

Checking for open ports

Posted on 2003-11-09
14
973 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 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