Solved

get available local port for socket program

Posted on 2002-05-01
6
171 Views
Last Modified: 2013-11-13
I'm getting into sockets programming using winsock and my question is this:

I need to be able to assign a local available port on my machine so that I can use to communicate with the other end.

Does anyone know of a way to scan for which ports are currently in use so that I don't try using that one when trying to assign a port number.

Example - let's say that ports 1000 and 1001 are currently being used by other applications. I want a program to say the next available port to use would be 1002.

A short snippet of code would be very helpful.

Thanks in advance.
0
Comment
Question by:samic400
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 6983303
If using winsock


winsock1.bind 0 'this binds to an available port
debug.print winsock1.localport
0
 
LVL 13

Author Comment

by:samic400
ID: 6983375
I need to find which ports are currently being used on my PC before I try and sort of binding. Do you know how to do that? Maybe it is some sort of Windows API?
0
 
LVL 13

Expert Comment

by:crazyman
ID: 6983413
Well you could try this, bit slow but it works.


Option Explicit

Public Function GetFreePorts(Socket As Winsock)
    Dim arrPorts() As Long
    Dim lPort As Long
    Dim lTemp As Long
    On Error Resume Next
    lTemp = -1
    For lPort = 1 To 32767
        Socket.Bind lPort
        'Debug.Print Err.Number
        If Err.Number = 10048 Then
            ReDim Preserve arrPorts(lTemp + 1)
            arrPorts(lTemp + 1) = lPort
            lTemp = UBound(arrPorts)
            DoEvents
        End If
        Socket.Close
    Next lPort
    GetFreePorts = arrPorts
End Function

Private Sub Command1_Click()
    Dim arr() As Long
    Dim l As Long
    arr = GetFreePorts(Winsock1)
    For l = LBound(arr) To UBound(arr)
        Debug.Print "Free Port " & l & " = " & arr(l)
    Next l
End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:crazyman
ID: 6983426
Oooops, not quite correct.


Option Explicit

Public Function GetFreePorts(Socket As Winsock)
    Dim arrPorts() As Long
    Dim lPort As Long
    Dim lTemp As Long
    On Error Resume Next
    lTemp = -1
    Socket.Protocol = sckUDPProtocol
    For lPort = 1 To 32767
        Socket.Bind lPort
        'Debug.Print Err.Number
        If Err.Number <> 10048 Then
            ReDim Preserve arrPorts(lTemp + 1)
            arrPorts(lTemp + 1) = lPort
            lTemp = UBound(arrPorts)
            'Debug.Print "FREE PORT = " & lPort
        Else
            'port in use!
            'Debug.Print "PORT IN USE = " & lPort
            Err.Clear
        End If
        DoEvents
        Socket.Close
    Next lPort
    GetFreePorts = arrPorts
End Function

Private Sub Command1_Click()
    Dim arr() As Long
    Dim l As Long
    arr = GetFreePorts(Winsock1)
    For l = LBound(arr) To UBound(arr)
        Debug.Print "Free Port " & l & " = " & arr(l)
    Next l
End Sub
0
 
LVL 13

Author Comment

by:samic400
ID: 6983470
Your example uses UDP. I'd be doing it with TCP. Same concept? If so, How could I do it?

Thanks!
0
 
LVL 13

Accepted Solution

by:
crazyman earned 75 total points
ID: 6983591
It only uses UDP to get the free ports, once finished you can switch back to TCP, in fact i think you could try it with TCP and it would work just the same.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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…

911 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

25 Experts available now in Live!

Get 1:1 Help Now