在同一个 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 对此进行了测试,但我不知道我做错了什么。
为什么服务器宕机后客户端还能向服务器发送数据?