Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .childHandler(new ServerInitializer());
- ChannelFuture f = b.bind(port).sync();
- f.channel().closeFuture().sync();
- } finally {
- workerGroup.shutdownGracefully();
- bossGroup.shutdownGracefully();
- }
- ///////////////////////////////////
- public class ServerInitializer extends ChannelInitializer<SocketChannel> {
- @Override
- protected void initChannel(SocketChannel ch) throws Exception {
- final ChannelPipeline p = ch.pipeline();
- p.addLast("decoder", new InputDecoder());
- p.addLast("encoder", new OutputEncoder());
- p.addLast("handler", new ServerHandler());
- p.addLast("httpExceptionHandler", new ServerExceptionHandler());
- }
- }
- ///////////////////////////////////
- public class ServerHandler extends SimpleChannelInboundHandler<String> {
- @Override
- protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception{
- ChannelFuture f = ctx.writeAndFlush(msg);
- try {
- f.channel().closeFuture().sync();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- Bootstrap b = new Bootstrap();
- b.group(workerGroup);
- b.channel(NioSocketChannel.class);
- b.handler(new ClientInitializer());
- Channel ch = b.connect(host, port).sync().channel();
- ChannelFuture f = ch.writeAndFlush("Some message");
- } finally {
- workerGroup.shutdownGracefully();
- }
- ///////////////////////////////////
- public class ClientInitializer extends ChannelInitializer {
- @Override
- protected void initChannel(Channel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
- p.addLast("decoder", new InputDecoder());
- p.addLast("encoder", new OutputEncoder());
- p.addLast("handler", new ClientHandler());
- p.addLast("httpExceptionHandler", new ClientExceptionHandler());
- }
- }
- ///////////////////////////////////
- public class ClientHandler extends SimpleChannelInboundHandler<String> {
- @Override
- protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
- System.out.println(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement