Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

get available local port for socket program

Posted on 2002-05-01
6
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 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