简体   繁体   English

在 Netty 中关闭客户端连接

[英]Close client connection in Netty

I have simple Netty client/server app.我有简单的 Netty 客户端/服务器应用程序。 in server side I check if client connect from right host or not and if it's not correct, close the client's connection.在服务器端,我检查客户端是否从正确的主机连接,如果不正确,请关闭客户端的连接。 in server side I use this code:在服务器端我使用这个代码:

@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        String remoteAddress = ctx.channel().remoteAddress().toString();
        if(!"/127.0.0.1:42477".equals(remoteAddress)) {
            ctx.writeAndFlush("Not correct remote address! Connection closed");
            ctx.close();
        }
        System.out.println(remoteAddress);
    }

and in client side this one:在客户端这个:

@Override
    protected void channelRead0(ChannelHandlerContext ctx, String data) throws Exception {
        try {
            System.err.println(data);
        } finally {
            ReferenceCountUtil.retain(data);
        }       
    }

but I can't get the message from server side in ctx.writeAndFlush() and client side shutdown with exception:但我无法在 ctx.writeAndFlush() 和客户端关闭中从服务器端获取消息,但异常:

java.io.IOException: Соединение сброшено другой стороной(TRANSLATION: Connection reset from the other side)
    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
    at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:447)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:242)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
    at java.lang.Thread.run(Thread.java:745)

how I can close client connection in rigth way?如何以正确的方式关闭客户端连接? I'm newbe in netty我是 netty 的新手

ctx.writeAndFlush is asynchronous and therefore may return before the data is actually written to the channel. ctx.writeAndFlush是异步的,因此可能会数据实际写入通道之前返回。 However, it returns a ChannelFuture , which lets you add a listener which will be notified when the operation is complete.但是,它返回一个ChannelFuture ,它允许您添加一个侦听器,该侦听器将在操作完成时收到通知。 To make sure the channel is closed only after the data has been written, you can do the following:要确保仅在写入数据后关闭通道,您可以执行以下操作:

ctx.writeAndFlush("Not correct remote address! Connection closed")
        .addListener(ChannelFutureListener.CLOSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM