Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Server {
- public void run() throws InterruptedException {
- EventLoopGroup accepter = new NioEventLoopGroup();
- EventLoopGroup worker = new NioEventLoopGroup();
- ServerBootstrap b = new ServerBootstrap();
- b.group(accepter, worker)
- .channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- protected void initChannel(SocketChannel s) throws Exception {
- ChannelPipeline p = s.pipeline();
- p.addLast(new Handler());
- }
- });
- ChannelFuture f = b.bind(43594).sync();
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- new Server().run();
- }
- }
- public class Handler extends ChannelInboundHandlerAdapter {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) {
- ((ByteBuf) msg).release();
- }
- @Override
- public void channelReadComplete(ChannelHandlerContext ctx) {
- ctx.flush();
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- // Close the connection when an exception is raised.
- cause.printStackTrace();
- ctx.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement