List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.github.nettybook.ch7.junit.TelnetServerV3.java
License:Apache License
public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .ja v a 2s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new TelnetServerInitializerV3()); ChannelFuture future = b.bind(address).sync(); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.nettybook.ch8.HttpSnoopServer.java
License:Apache License
@SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { SslContext sslCtx = null;/*from www .java 2 s.c o m*/ try { File certChainFile = new File("netty.crt"); File keyFile = new File("privatekey.pem"); keyFile.exists(); sslCtx = SslContext.newServerContext(certChainFile, keyFile, "1234"); } catch (SSLException e) { e.printStackTrace(); System.out.println("Can not create SSL context! \n Server will be stop!"); } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new HttpSnoopServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.nettybook.ch8.TelnetServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w . j a v a 2s. c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new TelnetServerInitializer()); b.bind(listenPort).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.sinsinpub.pero.frontend.NettySocksServer.java
License:Apache License
public void run() throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadFactoryRepository.BOSS_GORUP); EventLoopGroup workerGroup = new NioEventLoopGroup(getMaxWorkerThreads(), ThreadFactoryRepository.WORKER_GROUP); try {//from w ww .ja va 2s.com ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(getSocksServerInitializer()); ChannelFuture cf = b.bind(getPort()).sync(); logger.info( String.format("Proxy server %s %s started.", ApplicationVersion.DEFAULT.getApplicationName(), ApplicationVersion.DEFAULT.getApplicationVersion())); cf.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.unafraid.signer.server.ServerManager.java
License:Apache License
private void init() { SslContext sslCtx = null;//from w ww . j a v a2 s . com if (SSL) { try { final SelfSignedCertificate ssc = new SelfSignedCertificate("localhost"); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (Exception e) { LOGGER.warn(e.getMessage(), e); } } InetAddress listenAddress; try { listenAddress = Inet4Address.getByName(HOSTNAME); } catch (Exception e) { LOGGER.warn("Incorrect listen ip specified: {} using localhost instead!", HOSTNAME); listenAddress = Inet4Address.getLoopbackAddress(); } final EventLoopGroup bossGroup = new NioEventLoopGroup(1); final EventLoopGroup workerGroup = new NioEventLoopGroup(); try { final ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ServerInitializer(sslCtx)); // Start listening final Channel ch = b.bind(listenAddress, PORT).sync().channel(); LOGGER.info("Open your web browser and navigate to {}://{}{}/", (SSL ? "https" : "http"), listenAddress.getHostAddress(), (PORT != 443 && PORT != 80 ? ":" + PORT : "")); // Block til closed ch.closeFuture().sync(); } catch (Exception e) { LOGGER.warn("Failed to initialize server: ", e); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.vitrifiedcode.javautilities.netty.DiscardServer.java
License:Open Source License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . ja va 2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // (7) // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.github.wangshuwei5.server.NettyServer.java
License:Apache License
public void bind() throws Exception { // ??NIO// w w w.ja va 2 s. c o m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new NettyServerInitializer(SSLMODE.CSA.toString())); // ??? ChannelFuture f = b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync(); // ??? f.channel().closeFuture().sync(); System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT)); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.github.wolf480pl.ircd.netty.NettyServer.java
License:Open Source License
public ChannelFuture start() { if (!started.compareAndSet(false, true)) { return null; }//from w ww. jav a2 s . com ServerBootstrap bootstrap = new ServerBootstrap(); bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); IRCChannelInitializer initializer = new IRCChannelInitializer(handler); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(initializer) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture future = bootstrap.bind(bindAddress); this.channel = future.channel(); return future; }
From source file:com.github.zk1931.jzab.NettyTransport.java
License:Apache License
/** * Constructs a NettyTransport object./* w ww . j a v a2 s . c o m*/ * * @param hostPort "hostname:port" string. The netty transport binds to the * port specified in the string. * @param receiver receiver callback. * @param sslParam Ssl parameters. * @param dir the directory used to store the received file. */ public NettyTransport(String hostPort, final Receiver receiver, ZabConfig.SslParameters sslParam, final File dir) throws InterruptedException, GeneralSecurityException, IOException { super(receiver); this.keyStore = sslParam.getKeyStore(); this.trustStore = sslParam.getTrustStore(); this.keyStorePassword = sslParam.getKeyStorePassword() != null ? sslParam.getKeyStorePassword().toCharArray() : null; this.trustStorePassword = sslParam.getTrustStorePassword() != null ? sslParam.getTrustStorePassword().toCharArray() : null; this.dir = dir; if (isSslEnabled()) { initSsl(); } this.hostPort = hostPort; String[] address = hostPort.split(":", 2); int port = Integer.parseInt(address[1]); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { if (isSslEnabled()) { SSLEngine engine = serverContext.createSSLEngine(); engine.setUseClientMode(false); engine.setNeedClientAuth(true); ch.pipeline().addLast(new SslHandler(engine)); } // Incoming handlers ch.pipeline().addLast(new MainHandler()); ch.pipeline().addLast(new ServerHandshakeHandler()); ch.pipeline().addLast(new NotifyHandler()); ch.pipeline().addLast(new ServerErrorHandler()); } }); // Travis build fails once in a while because it fails to bind to a port. // This is most likely a transient failure. Retry binding for 5 times with // 1 second sleep in between before giving up. int bindRetryCount = 5; for (int i = 0;; i++) { try { channel = b.bind(port).sync().channel(); LOG.info("Server started: {}", hostPort); return; } catch (Exception ex) { if (i >= bindRetryCount) { throw ex; } LOG.debug("Failed to bind to {}. Retrying after 1 second.", hostPort); Thread.sleep(1000); } } }
From source file:com.github.zk1931.jzab.transport.NettyTransport.java
License:Apache License
/** * Constructs a NettyTransport object.// www . j ava 2 s . co m * * @param hostPort "hostname:port" string. The netty transport binds to the * port specified in the string. * @param receiver receiver callback. * @param sslParam Ssl parameters. * @param dir the directory used to store the received file. */ public NettyTransport(String hostPort, final Receiver receiver, SslParameters sslParam, final File dir) throws InterruptedException, GeneralSecurityException, IOException { super(receiver); this.keyStore = sslParam.getKeyStore(); this.trustStore = sslParam.getTrustStore(); this.keyStorePassword = sslParam.getKeyStorePassword() != null ? sslParam.getKeyStorePassword().toCharArray() : null; this.trustStorePassword = sslParam.getTrustStorePassword() != null ? sslParam.getTrustStorePassword().toCharArray() : null; this.dir = dir; if (isSslEnabled()) { initSsl(); } this.hostPort = hostPort; String[] address = hostPort.split(":", 2); int port = Integer.parseInt(address[1]); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { if (isSslEnabled()) { SSLEngine engine = serverContext.createSSLEngine(); engine.setUseClientMode(false); engine.setNeedClientAuth(true); ch.pipeline().addLast(new SslHandler(engine)); } // Incoming handlers ch.pipeline().addLast(new MainHandler()); ch.pipeline().addLast(new ServerHandshakeHandler()); ch.pipeline().addLast(new NotifyHandler()); ch.pipeline().addLast(new ErrorHandler()); // Outgoing handlers. ch.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4)); } }); // Travis build fails once in a while because it fails to bind to a port. // This is most likely a transient failure. Retry binding for 5 times with // 1 second sleep in between before giving up. int bindRetryCount = 5; for (int i = 0;; i++) { try { channel = b.bind(port).sync().channel(); LOG.info("Server started: {}", hostPort); return; } catch (Exception ex) { if (i >= bindRetryCount) { throw ex; } LOG.debug("Failed to bind to {}. Retrying after 1 second.", hostPort); Thread.sleep(1000); } } }