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 3454496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:30:01+00:00 2026-05-18T09:30:01+00:00

Who knows how the port is chosen when I’m using accept method of ServerSocket

  • 0

Who knows how the port is chosen when I’m using accept method of ServerSocket class? Is it possible to define a range for the ports the method can choose from? Can I ‘take’ ports one by one just in order?

ServerSocket sSocket = new ServerSocket(5050);
Socket socket = sSocket.accept();

From the book

  • 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-18T09:30:02+00:00Added an answer on May 18, 2026 at 9:30 am

    The diagram is incorrect (and is listed in the unconfirmed errata on the O’Reilly site).

    The client chooses its port at random (you don’t need to do anything special in Java) and connects to the server on whichever port you specified. Using the netstat commandline tool you can see this.

    First, just the listening server socket with no clients:

    simon@lucifer:~$ netstat -n -a
    Active Internet connections (including servers)
    Proto Recv-Q Send-Q  Local Address          Foreign Address     (state)
    ...
    tcp46      0      0  *.5050                 *.*                 LISTEN
    ...
    

    (there are lots of other entries, I’ve just removed the unrelated ones)

    Now with one client connecting from localhost (127.0.0.1):

    simon@lucifer:~$ netstat -n -a
    Active Internet connections (including servers)
    Proto Recv-Q Send-Q  Local Address          Foreign Address     (state)
    ...
    tcp4       0      0  127.0.0.1.64895        127.0.0.1.5050      ESTABLISHED <- 1
    tcp4       0      0  127.0.0.1.5050         127.0.0.1.64895     ESTABLISHED <- 2
    tcp46      0      0  *.5050                 *.*                 LISTEN      <- 3
    ...
    

    Since the client is connecting from the same machine, we see two established connections – one from client to server (1), the other from server to client (2). They have opposite local and foreign addresses (since they’re talking to each other) and you can see the server is still using port 5050 while the original server socket (3) continues to listen on the same port.

    (this output is from a Mac, but Windows/Linux also have netstat giving similar output)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyone who knows how to destroy a javascript (jquery) function? I'm using
Many bad things happened and continue to happen (or not, who knows, anything can
Who knows what archetype should be chosen to get such directory layout http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Who knows a good component for a calendar control (NOT date/time picker)? Calendar control
Who knows good graph layout engines with clear customization and managment? Flare is an
Anyone who knows a good way to generate/create a barcode from a String in
Anyone out there who knows what are the differences between BasicRenderEngine and LazyRenderEngine?
Is there anyone who knows this? I have been trying this for the last
i hired a designer who knows flash but not flex, is there a way
I'm a PHP developer who knows a little bit of Ruby. I want to

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.