You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Michał Gałuszka <mg...@zonith.com> on 2016/10/19 09:27:22 UTC

Address already in use when restarting server

Dear MINA users.

I’m using MINA server acceptor in my Java application that needs to be restarted from time to time.
When I stop and start that application „too fast” I always get an exception when starting and trying to bind:

java.io.IOException: Error while binding on 0.0.0.0/0.0.0.0:8245
original message : Address already in use

It must mean that I’m not closing all connections correctly upon application termination.
I have tried many things but nothing helped.

When terminating application I’m closing all sessions (most often there is only one session open), wait for the session to close gracefully and then I fire 

acceptor.dispose(true);

I’m not calling acceptor.unbind() because it is already called inside dispose(). Right?

After few minutes I can always start my application and then
acceptor.bind(new InetSocketAddress(port));
will work correctly not throwing any Exception.

OS: macOS 10.12 and Ubuntu
Java: 1.8.0_60 x64
MINA: 2.0.14

Best regards
Michał Gałuszka

Re: Address already in use when restarting server

Posted by Jonathan Valliere <sy...@gmail.com>.
If a remote tcp connection is open to that port then TCP will always say Address already in use. Set the Reuse Address flag and it will work fine. 

Sent from my iPhone

> On Oct 19, 2016, at 5:58 AM, Emmanuel Lécharny <el...@gmail.com> wrote:
> 
> 
> 
>> Le 19/10/16 à 11:27, Michał Gałuszka a écrit :
>> Dear MINA users.
>> 
>> I’m using MINA server acceptor in my Java application that needs to be restarted from time to time.
>> When I stop and start that application „too fast” I always get an exception when starting and trying to bind:
>> 
>> java.io.IOException: Error while binding on 0.0.0.0/0.0.0.0:8245
>> original message : Address already in use
>> 
>> It must mean that I’m not closing all connections correctly upon application termination.
>> I have tried many things but nothing helped.
>> 
>> When terminating application I’m closing all sessions (most often there is only one session open), wait for the session to close gracefully and then I fire 
>> 
>> acceptor.dispose(true);
>> 
>> I’m not calling acceptor.unbind() because it is already called inside dispose(). Right?
>> 
>> After few minutes I can always start my application and then
>> acceptor.bind(new InetSocketAddress(port));
>> will work correctly not throwing any Exception.
> This is inherent to the way TCP works. You can have a look at
> http://hea-www.harvard.edu/~fine/Tech/addrinuse.html.
> 
> You can try to setup the SO_REUSEADRESS flag to mitigate the problem,
> using the setReuseAddress( true ) method on your service.
> 

Re: Address already in use when restarting server

Posted by Emmanuel Lécharny <el...@gmail.com>.

Le 19/10/16 à 11:27, Michał Gałuszka a écrit :
> Dear MINA users.
>
> I’m using MINA server acceptor in my Java application that needs to be restarted from time to time.
> When I stop and start that application „too fast” I always get an exception when starting and trying to bind:
>
> java.io.IOException: Error while binding on 0.0.0.0/0.0.0.0:8245
> original message : Address already in use
>
> It must mean that I’m not closing all connections correctly upon application termination.
> I have tried many things but nothing helped.
>
> When terminating application I’m closing all sessions (most often there is only one session open), wait for the session to close gracefully and then I fire 
>
> acceptor.dispose(true);
>
> I’m not calling acceptor.unbind() because it is already called inside dispose(). Right?
>
> After few minutes I can always start my application and then
> acceptor.bind(new InetSocketAddress(port));
> will work correctly not throwing any Exception.
This is inherent to the way TCP works. You can have a look at
http://hea-www.harvard.edu/~fine/Tech/addrinuse.html.

You can try to setup the SO_REUSEADRESS flag to mitigate the problem,
using the setReuseAddress( true ) method on your service.