On 4/5/07, Trustin Lee <[EMAIL PROTECTED]> wrote:
Hi Vinod,
On 4/5/07, Vinod Panicker <[EMAIL PROTECTED]> wrote:
> On 4/5/07, Trustin Lee <[EMAIL PROTECTED]> wrote:
> > On 4/5/07, Coding Horse <[EMAIL PROTECTED]> wrote:
> > >
> > > But why didn't I have this problem before using mina? I used nio directly
> > > and never called setReuseAddres(). The server was in the same linux box
> > > before using mina.
> >
> > Why don't you trace the problem and let us know why if you are having
> > the problem? I can't fix it because it works OK for me.
>
> I believe this problem will show itself only when the disconnected
> client sockets are in a *_WAIT state. To reproduce this, you will
> need to use clients that are not local (since the sockets get recycled
> pretty fast), but rather on the internet or on some simulated low
> bandwidth / high latency connection.
I was able to reproduce this problem in my Linux box today. I tried
three scenarios:
1) Server starts up, and killed -9: OK with rebinding
2) Server starts up, a client connects, the connection closes, and
killed -9: OK with rebinding
3) Server starts up, a client connects, and server gets killed -9.:
Gets BindException on rebinding.
Looking closely the scenario 3, I found a connection with TIME_WAIT
state through netstat. I was able to bind again after the TIME_WAIT
state goes away.
I tried the following solutions:
1) acceptor.setReuseAddress(true); -> doesn't work.
2) acceptor.getSessionConfig().setReuseAddress(true); -> works!
It seems like Linux doesn't clean up connection state and prevents
binding the port which the dangling connection is referencing. It
seems like setting a SO_REUSE_ADDR on an accepted socket disables the
protection. I'm not sure how this differs in Windows.
Cool. It's interesting how its working when you set the option on the
SessionConfig. Technically speaking, an application is not supposed
to bind again on a socket/port that is in a *_WAIT state, since the
state exists so data "in transmission" may be safely expired.
Rebinding a server on a socket in a wait state using the SO_REUSE_ADDR
may be necessary for server systems (to reduce downtime), but could
also potentially generate some interesting issues.
The way it works is exactly the same on both Linux and Windows, and
it's not a Linux issue, but rather the way the JVM is setting the
SO_REUSE_ADDR option on the socket. I've coded servers in C++ on Linux
and successfully managed to use this option for rebinding.
Let me try your workaround - will get back on if it works for me too.
Regards,
Vinod.