Netty 4 creating multiple connections from client

I am trying to create multiple client connection to a java based socket server from another machine. Both server and client use Netty 4 for NIO. On server side, I used boss and worker group and its able to receive and server 100000 concurrent connection on a single linux box (after setting kernel parameters and ulimit).

However, I end up creating a new thread per connection on client side and that caused JVM thread limit exception.

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
​
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
​
public class TelnetClient
{
    private Bootstrap b;
    private NioEventLoopGroup loopGroup;
    private TelnetConnectionInitializer tci;
​
​
    public static void main(String[] args) throws Exception
    {
        System.out.println("TelnetClient:main:enter " + args[0]);
​
        TelnetClient tc = new TelnetClient();
​
        String countStr = args[0];  //number of connections to make
        int count = Integer.valueOf(countStr);
​
        for (int i=0; i < count; i++)
        {
            params.add(String.valueOf(i));
            Runnable r = new ClientThread(tc);
            new Thread(r).start();
        }
​
        System.out.println("TelnetClient:main:exit");
    }
​
    public TelnetClient()
    {
        System.out.println("TelnetClient:TelnetClient");
        b = new Bootstrap();
        loopGroup = new NioEventLoopGroup();
        b = b.group(loopGroup);
        b = b.channel(NioSocketChannel.class);
        tci = new TelnetConnectionInitializer();
    }
​
    public void connect(String host, int port) throws Exception {
        System.out.println("TelnetClient:connect:enter");
​
        try {
            b.handler(tci).connect(host, port).sync().channel().closeFuture().sync();
        } finally {
            b.group().shutdownGracefully();
        }
        System.out.println("TelnetClient:connect:exit");
    }
}
​
/// Creating a new thread per connection, 
/// Which seems the culprit of JVM exception, but couldn't found a way to implement boss / worker like solution on client side. 
class ClientThread implements Runnable
{
    TelnetClient myTc;
​
    public ClientThread(TelnetClient tc)
    {
        myTc = tc;
    }
​
    public void run()
    {
        System.out.println("ClientThread:run");    ​
        try
        {
            myTc.connect("192.168.1.65", 4598);  //Server running on different machine in local network
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Can someone point me, how I can create multiple connections from client side using Netty, without spawning new thread per client. I tried one and only snippet found for similar condition in another post on stack overflow but in that, for me execution paused (entered into an infinite wait state) after first successful connection itself.

Thanks


The code looks to be correct apart from two important things - you have to share netty context by all the clients and work asynchronously.

Ie initialize EvenetLoopGroup at the beginning and pass this single instance to every call to Bootstrap.group() for each client.

For asynchronous aproach avoid sync() on connect() future (not that much important) and mainly on close() future. The latter the code being suspended until the connection is closed.

链接地址: http://www.djcxy.com/p/95222.html

上一篇: 是否可以在全双工TCP通信中使用Netty?

下一篇: Netty 4从客户端创建多个连接