Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package com.beatburger.echoserver;
  2.  
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.*;
  5. import io.netty.channel.nio.NioEventLoopGroup;
  6. import io.netty.channel.socket.SocketChannel;
  7. import io.netty.channel.socket.nio.NioServerSocketChannel;
  8. import io.netty.handler.logging.LogLevel;
  9. import io.netty.handler.logging.LoggingHandler;
  10.  
  11. public class EchoServer {
  12.  
  13.     private final int port;
  14.  
  15.     // server constructor
  16.     public EchoServer(int port){
  17.         this.port = port;
  18.     }
  19.  
  20.     public void run() throws Exception{
  21.         EventLoopGroup bossGroup = new NioEventLoopGroup();
  22.         EventLoopGroup workGroup = new NioEventLoopGroup();
  23.  
  24.         try {
  25.             ServerBootstrap bootstrap = new ServerBootstrap();
  26.  
  27.             bootstrap.group(bossGroup, workGroup)
  28.                     .channel(NioServerSocketChannel.class)
  29.                     .option(ChannelOption.SO_BACKLOG, 100)
  30.                     .handler(new LoggingHandler(LogLevel.INFO))
  31.                     .childHandler(new ChannelInitializer<SocketChannel>() {
  32.  
  33.                         @Override
  34.                         protected void initChannel(SocketChannel socketChannel) throws Exception {
  35.                             ChannelPipeline pipeline = socketChannel.pipeline();
  36.                             pipeline.addLast(new EchoServerHandler());
  37.                         }
  38.                     });
  39.  
  40.             ChannelFuture cf = bootstrap.bind(port).sync();
  41.  
  42.             cf.channel().closeFuture().sync();
  43.         }
  44.         finally {
  45.             bossGroup.shutdownGracefully();
  46.             workGroup.shutdownGracefully();
  47.         }
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement