Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Netty服务器在本地运行时未正确关闭_Netty - Fatal编程技术网

Netty服务器在本地运行时未正确关闭

Netty服务器在本地运行时未正确关闭,netty,Netty,我在同一个jvm中运行Netty客户端和服务器并试图停止服务器时遇到问题。以下是我的代码来举例说明: @BeforeMethod public void setUp() { serverBootstrap = new ServerBootstrap(new DefaultLocalServerChannelFactory()); serverBootstrap.getPipeline().addLast("my-server-handler", new ServerHandler

我在同一个jvm中运行Netty客户端和服务器并试图停止服务器时遇到问题。以下是我的代码来举例说明:

@BeforeMethod
public void setUp() {
    serverBootstrap = new ServerBootstrap(new DefaultLocalServerChannelFactory());
    serverBootstrap.getPipeline().addLast("my-server-handler", new ServerHandler());
    LocalAddress local = new LocalAddress("1");
    serverChannel = serverBootstrap.bind(local);

    clientBootstrap = new ClientBootstrap(new DefaultLocalClientChannelFactory());

    clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("my-client-handler", new ClientHandler());
            return pipeline;
        }
    });

    ChannelFuture future = clientBootstrap.connect(local);
    if (future.isSuccess())
        System.out.println("Client connected");
    clientChannel = future.getChannel();
}

@AfterMethod
public void tearDown() {
    closeServer();
    clientChannel.close().awaitUninterruptibly();
    clientBootstrap.releaseExternalResources();
}

@Test
public void shoulClose() {        
    sendData();
    closeServer();
    sendData();       
}

private void closeServer() {
    serverChannel.close().awaitUninterruptibly();
    serverBootstrap.releaseExternalResources();
}

private void sendData() {
    clientChannel.write(new byte[] { 1, 2, 3 });
}
我已经用Netty 3.4.0.Final和3.4.4.Final测试过了,我不知道我做错了什么


为什么在服务器被关闭后,客户端仍然可以向服务器发送数据?

客户端可以发送数据,但不能发送clientChannel.write。。应该失败

您可以通过以下方式进行检查:

boolean success = clientChannel.write(new byte[] { 1, 2, 3 }).awaitUninteruptable().isSucess();
或者使用ChannelFutureListener以异步方式获取通知