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

  • Home
  • SEARCH
  • 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 6193351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:06:16+00:00 2026-05-24T03:06:16+00:00

I have some java code that looks similar to this: private void startServer() throws

  • 0

I have some java code that looks similar to this:

private void startServer() throws IOException {
        URLClassLoader classloader = null;

        System.out.println("Opening server socket for listening on " + PORT_NUMBER);
        try {
            server = new ServerSocket(PORT_NUMBER);
            server.setSoTimeout(10000);
            connected = true;
            System.out.println("Server is now listening on port " + PORT_NUMBER);
        } catch (IOException e) {
            System.err.println("Could not start server on port " + PORT_NUMBER);
            e.printStackTrace();
            connected = false;
        }

        while (connected) {

            // Incoming request handler socket.
            Socket socket = null;
            try {
                System.out.println("Waiting for client connection...");
                // Block waiting for an incoming connection.
                socket = server.accept();
                if (socket == null) continue;

…and so on and so forth. When I call server.close() later on (I don’t get any different behavior if I call socket.close() first), I don’t get any errors, but netstat shows that the port is still being listened on. Should calling ServerSocket.close() be sufficient enough to free up the port on this system?

I am programming for a Java 1.4.2 microedition runtime. It is also worthy to note that I have this method being run in another thread, and I am trying to close the socket from its parent thread.

EDIT Here is the line from netstat, though I can assure you it is still being listened on, since if I start the Xlet again I get an exception with that port number.

tcp        0      0  *.2349                 *.*                    LISTEN
  • 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-24T03:06:16+00:00Added an answer on May 24, 2026 at 3:06 am

    There are several things to consider. One of them is described by the following quotation from JavaDoc of ServerSocket

    public void setReuseAddress(boolean on)
    throws SocketException

    Enable/disable the
    SO_REUSEADDR socket option. When a TCP connection is closed the
    connection may remain in a timeout state for a period of time after
    the connection is closed (typically known as the TIME_WAIT state or
    2MSL wait state). For applications using a well known socket address
    or port it may not be possible to bind a socket to the required
    SocketAddress if there is a connection in the timeout state involving
    the socket address or port.

    So it is kind of OK that the OS can still show that there is something going on after you close() the server socket. But if you going to open/close a server socket on the same port frequently you might hit a problem.

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

Sidebar

Related Questions

I have a some simple Java code that looks similar to this in its
so I have some code that looks like this: import java.awt.BorderLayout; import java.awt.Component; import
I have some Java code that uses curly braces in two ways // Curly
I have a standalone Java app that has some licensing code that I want
I have to take over and improve/finish some code that transforms Java objects from
I have to ship some groovy code to some users that have only java
Let's say I have some Java code: public class SomeClass { static { private
I have some Java code that takes an XML (SOAP) message and returns the
I have ported some LGPL code from Java to C#, which I plan to
Does anyone have a good solution for integrating some C# code into a java

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.