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.
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:
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: