Results 1 to 2 of 2

Thread: checking PORT availability

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    82

    Exclamation

    Is there any way for me to check for a port setting before assigning it?

    Of course when another application is using the port, and I want to use the same port, of course there would be conflict.

    Is there anyway for me to be able to check the port avaiability before the system decides to use it?

    *thank you*

  2. #2
    Lively Member harsoni's Avatar
    Join Date
    Oct 2000
    Posts
    118
    SUMMARY
    This article describes how to monitor whether a TCI/IP port is in use.



    MORE INFORMATION
    There are times when it is necessary to know or monitor whether a port is in use. The following sample demonstrates how this is done.



    Create a new Standard EXE project. Form1 is created by default.


    Add 1 Timer, 2 Labels, 1 Listbox, 1 TextBox and 2 CommandButton controls to Form1. Position Label1 above Text1 and Label2 above List1.


    Set the Sorted property of List1 to True.


    Add the following code to the General Declarations section of Form1:

    Option Explicit

    Dim MySock As Object
    Const PortsChecked = 200

    Private Sub Command1_Click()
    Timer1.Enabled = True
    Timer1.Interval = 1000
    End Sub

    Private Sub Command2_Click()
    Timer1.Interval = 0
    Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Timer()
    Dim X As Integer

    List1.Clear
    For X = 1 To PortsChecked
    DoEvents
    Text1.Text = X
    Set MySock = CreateObject("MSWinsock.Winsock.1")
    MySock.LocalPort = X
    On Error Resume Next
    MySock.Listen ' If we get an error, the port is busy.
    If Err.Number = 10048 Then
    List1.AddItem X ' Log Active port # to list box.
    Err.Number = 0
    End If

    MySock.Close
    Set MySock = Nothing
    Next X
    End Sub

    Private Sub Form_Load()
    Label1.Caption = "Checking Port #"
    Label2.Caption = "Ports In Use"
    Command1.Caption = "Start"
    Command2.Caption = "End"
    Text1.Locked = True
    End Sub



    Run the program. If, for example, you have an active DCOM connection, you should see 135 displayed in the ListBox.

    To change the number of ports checked, modify the constant PortsChecked. To change the amount of time between samples modify the constant TimerInt.

    CODE FROM MSDN...

    Sonia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width