From a39d3e89ffb743cfbec3a91df18de83c43d0a9df Mon Sep 17 00:00:00 2001
From: rafaelliu
- 2. Writing a Discard Server
public class DiscardServerHandler extends ChannelInboundByteHandlerAdapter
{
4
@Override
- 6 public void inboundBufferUpdate(
ChannelHandlerContext
ctx, ByteBuf
in) {
+ 6 public void inboundBufferUpdate(
ChannelHandlerContext
ctx, ByteBuf
in) throws Exception {
in.clear();
8 }
@@ -281,13 +281,7 @@
2. Writing a Discard Server
@@ -344,24 +338,24 @@ DiscardServerHandler
extends
- , which is an implementation of
- . provides various event
- handler methods that you can override. For now, it is just enough
- to extend ChannelInboundByteHandlerAdapter
rather than to implement
- the handler interfaces by yourself.
-
+ DiscardServerHandler
extends ChannelInboundByteHandlerAdapter
, which is an implementation of ChannelStateHandler
. ChannelStateHandler
provides various event handler methods that you can override. For now, it is just enough to extend ChannelInboundByteHandlerAdapter
rather than to implement the handler interfaces by yourself.
2. Writing a Discard Server
8 public static void main(String[] args) throws Exception {
ServerBootstrap
bootstrap =
- 10 new ();
+ 10 new
ServerBootstrap
();
try {
- 12 bootstrap.group(new
NioEventLoopGroup
(), new NioEventLoopGroup
();
- .channel(
)
+ 12 bootstrap.group(new
NioEventLoopGroup
(), new NioEventLoopGroup
())
+ .channel(
NioServerSocketChannel
.class)
14 .childHandler(new
ChannelInitializer
<SocketChannel>() {
@Override
16 public void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new DiscardServerHandler());
18 }
})
- 20 .setChildOption(.TCP_NO_DELAY, true)
- .setChildOption(.KEEP_ALIVE, true)
+ 20 .childOption(ChannelOption.TCP_NODELAY, true)
+ .childOption(ChannelOption.SO_KEEPALIVE, true);
22
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)).sync();
24
future.channel().closeFuture().sync();
26 } finally {
- b.shutdown();
+ bootstrap.shutdown();
28 }
}
30 }
@@ -501,8 +495,8 @@
3. Looking into the Received Data
1 @Override - 2 public void inboundBufferUpdated(ChannelHandlerContext
ctx,ByteBuf
in) { - while(in.readable()) { + 2 public void inboundBufferUpdated(ChannelHandlerContext
ctx,ByteBuf
in) throws Exception { + while(in.isReadable()) { 4 System.out.println((char) buf.readByte()); System.out.flush(); 6 } @@ -605,7 +599,7 @@5. Writing a Time Server
1 package io.netty.example.time; 2 - public class TimeServerHandler extends { + public class TimeServerHandler extendsChannelInboundByteHandlerAdapter
{ 4 @Override 6 public void channelActive(ChannelHandlerContext
ctx) {4. How do I pass data between handlers in the same Channel?