Solved

Checking for open ports

Posted on 2003-11-09
14
965 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 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

20 Experts available now in Live!

Get 1:1 Help Now