gpt4 book ai didi

java - 如何使用 netty 5.0 创建大量连接

转载 作者:可可西里 更新时间:2023-11-01 02:54:03 25 4
gpt4 key购买 nike

我想为测试目的创建大量客户端连接到服务器。我通过为每个连接创建线程来实现这一点,因此我只能在我的机器上创建 3000 个连接。下面是我的代码:

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

我能获得更多连接吗?

我在谷歌搜索后尝试了另一种解决方案,但 channel 会自动关闭。

最佳答案

这是我的另一个解决方案

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

}

关于java - 如何使用 netty 5.0 创建大量连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com