Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:01:48+00:00 2026-05-15T18:01:48+00:00

I understand that the following code can (perhaps not very efficiently) find out a

  • 0

I understand that the following code can (perhaps not very efficiently) find out a free TCP port in Java:

  public static int findFreePort() {
    int port;
    try {
      ServerSocket socket= new ServerSocket(0);
      port = socket.getLocalPort();
      socket.close(); 
    } catch (Exception e) { port = -1; }
    return port;    
  } 

(There are some related questions here in SO – for example).

What I don’t understand is why (or whether) two succesive calls to this method are guaranteed to return two different ports. This is assumed, for example, here (search for the calls to findFreePort method).

Is this correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:01:48+00:00Added an answer on May 15, 2026 at 6:01 pm

    In the Javadoc specification, I don’t see any line saying that two succesive calls is guaranteed to return two different ports…

    Since the ServerSocket is closed, a second call could give the same port. Statistically, it is unprobable but not impossible I think.

    If you open your two ServerSocket, get the ports, and then, close your two ServerSocket, you are guarranted to get two different ports (since the first is not free when you create the second ServerSocket).

    Example method to get n different free ports :

    public int[] getFreePorts(int portNumber) throws IOException {
        int[] result = new int[portNumber];
        List<ServerSocket> servers = new ArrayList<ServerSocket>(portNumber);
        ServerSocket tempServer = null;
    
        for (int i=0; i<portNumber; i++) {
            try {
                tempServer = new ServerSocket(0);
                servers.add(tempServer);
                result[i] = tempServer.getLocalPort();
            } finally {
                for (ServerSocket server : servers) {
                    try {
                        server.close();
                    } catch (IOException e) {
                        // Continue closing servers.
                    }
                }
            }
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that nltk can split sentences and print it out using the following
I understand that using the === compares type, so running the following code results
Can someone explain to me why the following code compiles OK in Java? char
I am trying to understand how the following code ties into two other .java
So, I understand that the following doesn't work, but why doesn't it work? interface
Suppose that one has some lock based code like the following where mutexes are
Read that the following code is an example of unsafe construction as it allows
I have the following code that generates a compiler error: Boolean IConvertible.ToBoolean(IFormatProvider provider) {
Im new to C# programming. Can someone please explain the following code: Console.WriteLine( {0}{1,10},
I have been working with the following code published on msdn: http://msdn.microsoft.com/en-us/library/fx6588te.aspx I understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.