Skip to content

Instantly share code, notes, and snippets.

@adamrofer
Created June 18, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrofer/5809753 to your computer and use it in GitHub Desktop.
Save adamrofer/5809753 to your computer and use it in GitHub Desktop.
Netty 4.0.0.CR5 UDP pipeline example
public void run() throws Exception {
EventLoopGroup udpGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(udpGroup)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("udpDecoder", new MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, MessageList<Object> out) throws Exception {
out.add(msg.content());
msg.retain();
}
});
p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65535, 2, 2));
}
});
b.bind(port).sync().channel().closeFuture().sync();
} finally {
udpGroup.shutdownGracefully();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment