Hi, I'm using java 1.6.0+, mina 2.0+, centos 5.5. (I met this problem few
months ago, so i don't remember exactly running environment.) This is a
client program using mina, connectting to several server, and I write a class
implements IoFilter for reconnection when detecting the connection is closed,
rewrite the sessionClosed method, start a thread to reconnect to server. The
code is like this:
public void sessionClosed(NextFilter nextFilter, final IoSession
session) throws Exception { new Thread() { public
void run() { SocketAddress serverAddress =
session.getRemoteAddress(); ... //do reconnect
}; }.start(); }
The problem is sometime the serverAddress is null, and after check out
the NioSocketSession's source code, i'm confused. Because the "ch" is final,
there is no way to set "ch" to null, don't understand the meaning of first
three lines in method getRemoteAddress.
class NioSocketSession extends NioSession { private final
SocketChannel ch;
public NioSocketSession(IoService service,
IoProcessor<NioSession> processor, SocketChannel ch) {
this.service = service; this.processor = processor;
this.ch = ch; this.handler = service.getHandler();
this.config.setAll(service.getSessionConfig()); }
public InetSocketAddress getRemoteAddress() {
if (ch == null) { return null;
} Socket socket = ch.socket(); if
(socket == null) { return null; }
return (InetSocketAddress)
socket.getRemoteSocketAddress(); } }
So, in which situation, invoke NioSocketSession.getRemoteAddress() will
returns null and how to get the server address after the connection is close?