The article directories


Constants defined

public class Constant {
    static final int BEGIN_PORT = 8000; 
    static final int N_PORT = 100;
}
Copy the code

ConnectionCountHandler

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Sharable
public class ConnectionCountHandler extends ChannelInboundHandlerAdapter {

    private AtomicInteger nConnection = new AtomicInteger();

    public ConnectionCountHandler() {
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            System.out.println("connections: " + nConnection.get());
        }, 0, 2, TimeUnit.SECONDS);

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        nConnection.incrementAndGet();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        nConnection.decrementAndGet();
    }

}
Copy the code

Client

import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; Public class Client {private static final String SERVER_HOST = "192.168.1.121"; public static void main(String[] args) { new Client().start(BEGIN_PORT, N_PORT); } public void start(final int beginPort, int nPort) { System.out.println("client starting...." ); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup); bootstrap.channel(NioSocketChannel.class); bootstrap.option(ChannelOption.SO_REUSEADDR, true); bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { } }); int index = 0; int port; while (! Thread.interrupted()) { port = beginPort + index; try { ChannelFuture channelFuture = bootstrap.connect(SERVER_HOST, port); channelFuture.addListener((ChannelFutureListener) future -> { if (! future.isSuccess()) { System.out.println("connect failed, exit!" ); System.exit(0); }}); channelFuture.get(); } catch (Exception e) { } if (++index == nPort) { index = 0; }}}}Copy the code

server

import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public final class Server { public static void main(String[] args) { new Server().start(BEGIN_PORT, N_PORT); } public void start(int beginPort, int nPort) { System.out.println("server starting...." ); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childOption(ChannelOption.SO_REUSEADDR, true); bootstrap.childHandler(new ConnectionCountHandler()); for (int i = 0; i < nPort; i++) { int port = beginPort + i; bootstrap.bind(port).addListener((ChannelFutureListener) future -> { System.out.println("bind success in port: " + port); }); } System.out.println("server started!" ); }}Copy the code

Breach of local file handle limit

1. Check the current limit on local file handles

ulimit -n
Copy the code

2. Modify the file

sudo vim /etc/security/limits.conf
Copy the code

3. Add content:

*		hard		nofile		1000000
*		soft		nofile		1000000
Copy the code

Note: * is valid only for the current user. After the modification, the machine needs to be restarted


Breach of global file handle limit

1. Check the global file handle limit

cat /proc/sys/fs/file-max
Copy the code

2. Temporary modification

echo 1000000 > /proc/sys/fs/file-max
Copy the code

Note: The Echo mode can be modified immediately, but it will be invalid after the machine restarts

3. Permanently modify

sudo vim /etc/sysctl.conf
Copy the code

4. At the end of the file, add content

fs.file-max = 1000000
Copy the code

5. Make the changes take effect

sudo sysctl -p
Copy the code

Note: The machine needs to be restarted after completing the above commands