2016-03-22 19 views
1

erstellen Ich möchte eine große Anzahl Client-Verbindung zum Server für den Test Zweck erstellen. Ich erreiche dies, indem ich einen Thread pro Verbindung erstelle, sodass ich nur 3000 Verbindungen auf meinem Rechner erstellen kann. Unten ist mein Code:wie eine große Anzahl von Verbindungen mit netty 5.0

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"); 
    } 
} 

Kann ich mehr Verbindung bekommen.

Ich hatte eine andere Lösung nach Google-Forschung versuchen, aber der Kanal wird automatisch geschlossen.

+0

BTW Es gibt keine Netty ist 5,0 – mahdix

Antwort

0

hier ist meine andere Lösung

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); 
     } 
    } 

}