关于 Mina BlackListFilter的一个小问题?

DuangDong 发布于 2016/11/25 14:42
阅读 248
收藏 0

最近在研究Mina过滤器,到了黑名单过滤器,发现挺简洁的,于是尝试写一点代码测一测看看,结果和我想的不太一样。先不多说,先贴代妈:

Mina服务端:



IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(1024);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, ConstanceLee.IDELTIMEOUT);

MyBlackListFilter blacklistFilter = new MyBlackListFilter();
InetAddress[] address = new InetAddress[1];
try
{
address[0] = InetAddress.getByName("192.168.1.150");
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
blacklistFilter.setBlacklist(address);
acceptor.getFilterChain().addFirst("black", blacklistFilter);



acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CodeFactory()));


KeepAliveMessageFactory keepAliveMessage = new KeepAliveMessageFactoryImpl_S();
KeepAliveRequestTimeoutHandler keepAliveRequestTimeoutHandler = new KeepAliveRequestTimeoutHandlerImpl_S();
KeepAliveFilter keepAliveFilter = new KeepAliveFilter(keepAliveMessage, keepAliveRequestTimeoutHandler);
keepAliveFilter.setForwardEvent(true);
keepAliveFilter.setRequestInterval(ConstanceLee.RATE);
keepAliveFilter.setRequestTimeout(ConstanceLee.IDELTIMEOUT);


acceptor.getFilterChain().addLast("keepalive", keepAliveFilter);


acceptor.setHandler(new Mina_Server_Handler());
try
{
acceptor.bind(new InetSocketAddress(ConstanceLee.PORT));
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Server started on port: " + ConstanceLee.PORT);



Mina客户端:

NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CodeFactory()));
connector.setConnectTimeoutCheckInterval(30);


KeepAliveMessageFactory keepAliveMessage = new
KeepAliveMessageFactoryImpl_C();
KeepAliveRequestTimeoutHandler keepAliveRequestTimeoutHandler = new
KeepAliveRequestTimeoutHandlerImpl_C();
KeepAliveFilter keepAliveFilter = new
KeepAliveFilter(keepAliveMessage, IdleStatus.BOTH_IDLE,
keepAliveRequestTimeoutHandler);
keepAliveFilter.setForwardEvent(true);
keepAliveFilter.setRequestInterval(ConstanceLee.RATE);
keepAliveFilter.setRequestTimeout(ConstanceLee.IDELTIMEOUT);
connector.getFilterChain().addLast("keepalive", keepAliveFilter);


connector.setHandler(new Mina_Client_Handler());
ConnectFuture cf = connector.connect(new InetSocketAddress("192.168.1.150", ConstanceLee.PORT));
cf.awaitUninterruptibly();
IoSession session = null;
try
{
session = cf.getSession();
}
catch (Exception e)
{
e.printStackTrace();
}

SocketAddress remoteAddress = session.getRemoteAddress();


        if (remoteAddress instanceof InetSocketAddress) {
            InetAddress address = ((InetSocketAddress) remoteAddress).getAddress();
            System.out.println(address);
            
        }



我想阻止本地的连接,可是为什么结果没反应呢?客户端正常打开了。求了解的大神给指正一下错误。其他忽略,只需关注MyBlackListFilter MyBlackListFilter 就是 BlackListFilter 代码没改,我本地ip就是 192.168.1.150



加载中
0
D
DuangDong
本人已经找到问题所在!
返回顶部
顶部