List of usage examples for io.netty.channel ChannelOption SO_REUSEADDR
ChannelOption SO_REUSEADDR
To view the source code for io.netty.channel ChannelOption SO_REUSEADDR.
Click Source Link
From source file:org.wenxueliu.netty.client.ClientTest.java
License:Apache License
private void connectRetry(String ip, int port, ChannelFutureListener clientConnectionListener) { try {/*from w ww . ja v a 2 s . c o m*/ bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE) .option(ChannelOption.SO_RCVBUF, SEND_BUFFER_SIZE) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT); pipelineFactory = new DefaultChannelInitializer(timer, this); bootstrap.handler(pipelineFactory); //.handler(new ChannelInitializer<SocketChannel>() { // @Override // protected void initChannel(SocketChannel channel) throws Exception { // ChannelPipeline p = channel.pipeline(); // p.addLast(new MessageDecoder(), // new StringEncoder(CharsetUtil.UTF_8), // new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0), // new BootstrapTimeoutHandler(timer, 10), // new ConnectionHandler()); // } //}); bootstrap.remoteAddress(ip, port); ChannelFuture future = bootstrap.connect(); future.awaitUninterruptibly(); future.addListener(clientConnectionListener); } catch (Exception e) { log.warn("Connection to the server {}:{} failed", ip, port); } }
From source file:org.wso2.carbon.gateway.internal.transport.sender.channel.ChannelUtils.java
License:Open Source License
/** * Provides incomplete Netty channel future. * * @param targetChannel Target channel which has channel specific parameters such as handler * @param eventLoopGroup Event loop group of inbound IO workers * @param eventLoopClass Event loop class if Inbound IO Workers * @param httpRoute Http Route which represents BE connections * @return ChannelFuture/*w ww.j a va 2s .c o m*/ */ @SuppressWarnings("unchecked") public static ChannelFuture getNewChannelFuture(TargetChannel targetChannel, EventLoopGroup eventLoopGroup, Class eventLoopClass, HttpRoute httpRoute) { BootstrapConfiguration bootstrapConfiguration = BootstrapConfiguration.getInstance(); Bootstrap clientBootstrap = new Bootstrap(); clientBootstrap.channel(eventLoopClass); clientBootstrap.group(eventLoopGroup); clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive()); clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay()); clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse()); clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut()); // set the pipeline factory, which creates the pipeline for each newly created channels TargetInitializer targetInitializer = new TargetInitializer(); targetChannel.setTargetInitializer(targetInitializer); clientBootstrap.handler(targetInitializer); if (log.isDebugEnabled()) { log.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", httpRoute.getHost(), httpRoute.getPort(), clientBootstrap); } return clientBootstrap.connect(new InetSocketAddress(httpRoute.getHost(), httpRoute.getPort())); }
From source file:org.wso2.carbon.transport.http.netty.sender.channel.ChannelUtils.java
License:Open Source License
/** * Provides incomplete Netty channel future. * * @param targetChannel Target channel which has channel specific parameters such as handler * @param eventLoopGroup Event loop group of inbound IO workers * @param eventLoopClass Event loop class if Inbound IO Workers * @param httpRoute Http Route which represents BE connections * @param senderConfiguration sender configuration * @return ChannelFuture//from www .j a va2s .c o m */ @SuppressWarnings("unchecked") public static ChannelFuture getNewChannelFuture(TargetChannel targetChannel, EventLoopGroup eventLoopGroup, Class eventLoopClass, HttpRoute httpRoute, SenderConfiguration senderConfiguration) { BootstrapConfiguration bootstrapConfiguration = BootstrapConfiguration.getInstance(); Bootstrap clientBootstrap = new Bootstrap(); clientBootstrap.channel(eventLoopClass); clientBootstrap.group(eventLoopGroup); clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive()); clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay()); clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse()); clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut()); // set the pipeline factory, which creates the pipeline for each newly created channels NettyClientInitializer nettyClientInitializer = new NettyClientInitializer(senderConfiguration); targetChannel.setNettyClientInitializer(nettyClientInitializer); clientBootstrap.handler(nettyClientInitializer); if (log.isDebugEnabled()) { log.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", httpRoute.getHost(), httpRoute.getPort(), clientBootstrap); } return clientBootstrap.connect(new InetSocketAddress(httpRoute.getHost(), httpRoute.getPort())); }
From source file:org.wso2.carbon.transport.http.netty.sender.channel.pool.PoolableTargetChannelFactory.java
License:Open Source License
private Bootstrap instantiateAndConfigBootStrap(EventLoopGroup eventLoopGroup, Class eventLoopClass, BootstrapConfiguration bootstrapConfiguration) { Bootstrap clientBootstrap = new Bootstrap(); clientBootstrap.channel(eventLoopClass); clientBootstrap.group(eventLoopGroup); clientBootstrap.option(ChannelOption.SO_KEEPALIVE, bootstrapConfiguration.isKeepAlive()); clientBootstrap.option(ChannelOption.TCP_NODELAY, bootstrapConfiguration.isTcpNoDelay()); clientBootstrap.option(ChannelOption.SO_REUSEADDR, bootstrapConfiguration.isSocketReuse()); clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, bootstrapConfiguration.getConnectTimeOut()); return clientBootstrap; }
From source file:p2p_server.P2p_server.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); List<ChannelFuture> futures = new ArrayList<>(); SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); try {// w ww . j a v a2s . c o m ServerBootstrap appboot = new ServerBootstrap(); appboot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 8192).childHandler(new AppChildChannelHandler(sslCtx)); appboot.option(ChannelOption.SO_REUSEADDR, true); appboot.option(ChannelOption.TCP_NODELAY, true); appboot.childOption(ChannelOption.SO_KEEPALIVE, true); appboot.childOption(ChannelOption.SO_RCVBUF, 512); appboot.childOption(ChannelOption.SO_SNDBUF, 512); ServerBootstrap devboot = new ServerBootstrap(); devboot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 8192).childHandler(new DevChildChannelHandler(sslCtx)); devboot.option(ChannelOption.SO_REUSEADDR, true); devboot.option(ChannelOption.TCP_NODELAY, true); devboot.childOption(ChannelOption.SO_KEEPALIVE, true); devboot.childOption(ChannelOption.SO_RCVBUF, 512); devboot.childOption(ChannelOption.SO_SNDBUF, 512); //ChannelFuture f = boostrap.bind(port).sync(); futures.add(devboot.bind(5560)); futures.add(appboot.bind(5561)); for (ChannelFuture f : futures) { f.sync(); } for (ChannelFuture f : futures) { f.channel().closeFuture().sync(); } // ??? // f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:picoview.collectd.CollectClient.java
License:Apache License
@Override public void initialize() throws RuntimeException { NetworkInterface nic;/* w w w . ja v a 2s. co m*/ try { if (StringUtils.isBlank(nicName)) { nic = NetworkInterface.getByIndex(0); } else { nic = NetworkInterface.getByName(nicName); } } catch (SocketException exep) { throw new RuntimeException("unable to determine network interface to use", exep); } Bootstrap bs = new Bootstrap(); bs.option(ChannelOption.SO_BROADCAST, true); bs.option(ChannelOption.SO_REUSEADDR, true); bs.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, false); bs.option(ChannelOption.SO_RCVBUF, 2048); bs.option(ChannelOption.IP_MULTICAST_TTL, 255); bs.group(new NioEventLoopGroup()); bs.channelFactory(new ChannelFactory<Channel>() { public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } }); bs.handler(new ChannelInitializer<DatagramChannel>() { @Override public void initChannel(DatagramChannel channel) throws Exception { channel.pipeline().addLast(new CollectChannelHandler()); } }); if (StringUtils.isBlank(multicastHost)) { multicastHost = "239.192.74.66"; } if (multicastPort <= 0) { multicastPort = 25826; } try { DatagramChannel dch = (DatagramChannel) bs.bind(multicastPort).sync().channel(); ChannelFuture cf = dch.joinGroup(new InetSocketAddress(multicastHost, multicastPort), nic).sync(); if (!cf.isSuccess()) { throw new RuntimeException("unable to join multicast group"); } } catch (InterruptedException exep) { throw new RuntimeException("unable to setup network for collect client", exep); } }
From source file:preformance_test.client.WebSocketClient.java
License:Apache License
public static void main(String[] args) throws Exception { //final String host = System.getProperty("netease.pushserver.host", "192.168.100.241"); final String host = "127.0.0.1"; int port = 9001; EventLoopGroup group = new NioEventLoopGroup(); try {/*w ww . jav a 2 s . c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class); b.option(ChannelOption.SO_REUSEADDR, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpClientCodec()); p.addLast(new HttpObjectAggregator(8192)); p.addLast(WebSocketClientCompressionHandler.INSTANCE); p.addLast("webSocketClientHandler", new WebSocketClientHandler()); } }); // for (int i = 0; i < 100; i++) { for (int j = 0; j < 60000; j++) { b.connect(host, port).sync().get(); } port++; } System.in.read(); } finally { group.shutdownGracefully(); } }
From source file:preformance_test.server.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww . ja va 2 s.c o m ServerBootstrap b = new ServerBootstrap(); //????channel?serverchannel??socket??channel b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true).childHandler(new WebSocketServerInitializer()) .childOption(ChannelOption.SO_REUSEADDR, true); for (int i = 0; i < 100; i++) { b.bind(++PORT).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if ("true".equals(System.getProperty("netease.debug"))) System.out.println("??" + future.channel().localAddress()); } }); } // ??????() TestCenter.startTest(); System.in.read(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:qunar.tc.qmq.netty.NettyServer.java
License:Apache License
public void start() { bootstrap.option(ChannelOption.SO_REUSEADDR, true); bootstrap.childOption(ChannelOption.TCP_NODELAY, true); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w ww . j av a 2s. c o m*/ protected void initChannel(SocketChannel ch) { ch.pipeline().addLast("connectionHandler", connectionHandler); ch.pipeline().addLast("encoder", new EncodeHandler()); ch.pipeline().addLast("decoder", new DecodeHandler(true)); ch.pipeline().addLast("dispatcher", serverHandler); } }); try { channel = bootstrap.bind(port).await().channel(); } catch (InterruptedException e) { LOG.error("server start fail", e); } LOG.info("listen on port {}", port); }
From source file:ratpack.server.internal.NettyRatpackService.java
License:Apache License
@Override protected void startUp() throws Exception { Stopper stopper = new Stopper() { @Override//from ww w . j a va 2 s . co m public void stop() { try { NettyRatpackService.this.stop(); } catch (Exception e) { throw uncheck(e); } } }; ServerBootstrap bootstrap = new ServerBootstrap(); group = new NioEventLoopGroup(launchConfig.getMainThreads(), new DefaultThreadFactory("ratpack-group", Thread.MAX_PRIORITY)); ChannelInitializer<SocketChannel> channelInitializer = channelInitializerTransformer.transform(stopper); bootstrap.group(group).channel(NioServerSocketChannel.class).childHandler(channelInitializer) .childOption(ChannelOption.ALLOCATOR, launchConfig.getBufferAllocator()) .childOption(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_BACKLOG, 1024) .option(ChannelOption.ALLOCATOR, launchConfig.getBufferAllocator()); try { channel = bootstrap.bind(buildSocketAddress()).sync().channel(); } catch (Exception e) { partialShutdown(); throw e; } boundAddress = (InetSocketAddress) channel.localAddress(); if (logger.isLoggable(Level.INFO)) { logger.info(String.format("Ratpack started for http://%s:%s", getBindHost(), getBindPort())); } }