开发者

"BindException: Address already in use" after restart of app

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.RemoteHandl开发者_运维知识库erListener$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.


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);


I Agree - Usually this indicates that the restart failed to terminate the server process properly. Somehow it's still running and therefore the port is still blocked.

Waiting for 10 minutes is somewhat long. Just check (if possible) if the previously running java process has terminated.


If you haven't already, you should study the Android dev stuff on Activity Lifecycles. The tl;dr of that article is that the Android operating system makes absolutely no guarantee about when an application will be killed except that it can happen any time, and that's why they provide the onPause, onResume, onRestart, etc. You can't ever guarantee that your app will always die completely, but you can perform your own cleanup, and I'd recommend killing your thread and closing your socket (or some other management) inside of onPause.

In addition, when using Java sockets (whether it's Android or not), it's never good practice to let program termination be the action that closes your sockets. You should almost always close them yourself in some sort of clean-up area. Especially when dealing with threads.

Update: You said that you are hitting back and then restarting. This doesn't call onDestroy. onDestroy is only called if the OS needs the memory and kills your app completely (the equivalent of killing it in the Task Manager)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜