Solved

get available local port for socket program

Posted on 2002-05-01
6
200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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