Solved

get available local port for socket program

Posted on 2002-05-01
6
166 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now