关于 Mina BlackListFilter的一个小问题?

发布于 2021-11-30 23:39:05 字数 3614 浏览 723 评论 1

最近在研究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)



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2021-12-02 04:29:31

本人已经找到问题所在!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文