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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:58:59+00:00 2026-05-25T23:58:59+00:00

I got a BindException when i restart my application. It acts as a server

  • 0

I got a BindException when i restart my application. It acts as a server waiting for remote control messages. The ServerSocket is running in a background thread (AsyncTask). After restart of my application i always get the exception mentioned above. I have to wait for like 10 minutes until it can bind again to the port in order to listen.

I tried different ports (all >50000) so I’m sure there is no other application blocking my port. I tried to be careful about closing the socket and i tried to use the SO_REUSEADDR option. Also I am sure that there is only one connection open at run time, since i logged every socketbind.

So what i think is, that the connection is not closed properly. I’ve read about the habit of sockets not closing instantly. But i cannot wait like 10 minutes on every restart of the application and i did not find a way to shorten or kill this time.

Do you have any ideas?

The exception:

10-04 16:39:22.526: WARN/System.err(4974): java.net.BindException: Address already in use
10-04 16:39:22.526: WARN/System.err(4974):     at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
10-04 16:39:22.526: WARN/System.err(4974):     at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:275)
10-04 16:39:22.526: WARN/System.err(4974):     at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:165)
10-04 16:39:22.526: WARN/System.err(4974):     at java.net.ServerSocket.<init>(ServerSocket.java:123)
10-04 16:39:22.526: WARN/System.err(4974):     at java.net.ServerSocket.<init>(ServerSocket.java:74)
10-04 16:39:22.526: WARN/System.err(4974):     at com.*******.remote.RemoteHandlerListener$1.doInBackground(RemoteHandlerListener.java:114)

The code:

ServerSocket server;
try {
    server = new ServerSocket();
    server.setReuseAddress(true);
    server.bind(new InetSocketAddress(serverport));
} catch (IOException e) {
    e.printStackTrace();
    return null;
}

while (true) {
    BufferedReader inStream = null;
    Socket client = null;
    try {
        client = server.accept();
        inStream = new BufferedReader(new InputStreamReader( client.getInputStream()));
        // read from stream
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException e) { }
        }
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) { }
        }
    }
}
try {
    server.close();
} catch (IOException e) { }

The exception is thrown on the server.bind-statement.

EDIT:
Cause of the problem: The thread will not terminate itself since the accept-call is blocking. The program did not terminate completely and the socket was not unbound.

Solution: Set SO_TIMEOUT to the socket and check for isCancelled in the while()-loop. This way the thread will finish if you call cancel() on it.

  • 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-25T23:59:00+00:00Added an answer on May 25, 2026 at 11:59 pm

    Try to print the stacktrace of all the caught exceptions. That will probably help a lot. Maybe there is something wrong in your code you posted. You are creating try-catch blocks without handeling the Exception. You even don’t know if there are any.


    Update: Next step is to make sure your application is really stopped. At first sight of your code you never quit your infinite loop of accepting clients. Try to fix that and add a print statement after you close the serversocket:

    try {
        server.close();
        System.out.println("Server successfully stopped.");
    } catch (IOException e) { }
    

    Make sure you find this in the output of your application.

    And if you want to be sloppy, you can enforce the application (and the serversocket) to shutdown using:

    System.exit(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm trying to set up a socket server, I've got an error message:
Got the following error when I tried to compile a C application in 64-bit
Got an application wich read some infos in a specific XML file. I would
Got the guestbook working alright. New messages are building up nicely. Now, only the
Got a partial answer from searching in this thread: Viewing raw RSS feed? But
Got a problem with jquery. I cant get a .xml file from the server,
Got an exception during migration Web application from WAS 6.1 to WAS 7. This
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
got a new blog at wordpress few days ago ( http://ghads.wordpress.com ) and I
Got a class that serializes into xml with XMLEncoder nicely with all the variables

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.