Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. public class Server {
  2.  
  3.     public void run() throws InterruptedException {
  4.        
  5.         EventLoopGroup accepter = new NioEventLoopGroup();
  6.         EventLoopGroup worker = new NioEventLoopGroup();
  7.        
  8.             ServerBootstrap b = new ServerBootstrap();
  9.             b.group(accepter, worker)
  10.             .channel(NioServerSocketChannel.class)
  11.             .childHandler(new ChannelInitializer<SocketChannel>() {
  12.  
  13.                 @Override
  14.                 protected void initChannel(SocketChannel s) throws Exception {
  15.                     ChannelPipeline p = s.pipeline();
  16.                     p.addLast(new Handler());
  17.                 }
  18.                
  19.             });
  20.            
  21.             ChannelFuture f = b.bind(43594).sync();
  22.            
  23.     }
  24.    
  25.     /**
  26.      * @param args
  27.      * @throws Exception
  28.      */
  29.     public static void main(String[] args) throws Exception {
  30.         new Server().run();
  31.     }
  32. }
  33.  
  34. public class Handler extends ChannelInboundHandlerAdapter {
  35.  
  36.     @Override
  37.     public void channelRead(ChannelHandlerContext ctx, Object msg) {
  38.         ((ByteBuf) msg).release();
  39.     }
  40.  
  41.     @Override
  42.     public void channelReadComplete(ChannelHandlerContext ctx) {
  43.         ctx.flush();
  44.     }
  45.  
  46.     @Override
  47.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  48.         // Close the connection when an exception is raised.
  49.         cause.printStackTrace();
  50.         ctx.close();
  51.     }
  52.    
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement