Solved

Checking for open ports

Posted on 2003-11-09
14
969 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

832 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