Solved

get available local port for socket program

Posted on 2002-05-01
6
182 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

820 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