1

I want to create large number client connection to the server for the test purpose. I accomplish this by creating thread per connection, so I can only create 3000 connection on my machine. below is my code:

package com.stepnetwork.iot.apsclient.application;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Created by sam on 3/22/16.
 */
public class DtuClient extends Thread {

    private static final String HOST = "192.168.54.36";
    private static final int PORT = 30080;
    private EventLoopGroup workerGroup;

    private String dtuCode;

    public DtuClient(String dtuCode, EventLoopGroup workerGroup) {
        this.dtuCode = dtuCode;
        this.workerGroup = workerGroup;
    }

    public void run() {

        Bootstrap bootstrap = new Bootstrap(); // (1)
        try {
            bootstrap.group(workerGroup); // (2)
            bootstrap.channel(NioSocketChannel.class); // (3)
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true); // (4)
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new MyClientHandler());
                }
            });


            ChannelFuture feature = bootstrap.connect(HOST, PORT).sync();
            feature.addListener((future) -> {
                System.out.println(dtuCode + " connected to server");
                Channel channel = feature.channel();
                ByteBuf buffer = Unpooled.buffer(256);
                buffer.writeBytes(dtuCode.getBytes());
                channel.writeAndFlush(buffer);
            });

            feature.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("completed");
    }
}

Can I get more connection.

I had try another solution after doing google research, but the channel will close automatically.

Sam Zeng
  • 11
  • 1

1 Answers1

0

here is my another solution

package com.stepnetwork.iot.apsclient.application;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sam on 3/22/16.
 */
public class Test {

    private static final String HOST = "192.168.54.36";
    private static final int PORT = 30080;

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap(); // (1)
        try {
            bootstrap.group(workerGroup); // (2)
            bootstrap.channel(NioSocketChannel.class); // (3)
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true); // (4)
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new MyClientHandler());
                }
            });


            List<Channel> channels = new ArrayList<>();

            // create many connection here, but the channel will be closed atomically
            for (int i = 0; i < 10000; i++) {
                channels.add(bootstrap.connect(HOST, PORT).sync().channel());
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (true) {
            Thread.sleep(Integer.MAX_VALUE);
        }
    }

}
Sam Zeng
  • 11
  • 1