使用JavaNIO的10000并发连接
我编写了一个服务器(类似于一个),并使用JavaNIO 我正在努力建立尽可能多的联系。 根据之前的建议,我放慢了客户端创建的速度,给操作系统(Windows8)足够的时间来处理请求 我在不同的机器上运行了客户机代码,这样服务器就有了所有可用的运行空间 当我尝试创建10000个连接时,大约8500个连接被连接,其余的连接被拒绝,而客户端(客户端代码中的线程)的连接被拒绝的情况发生得更多,这些情况是稍后创建的(客户端代码中的循环) 我的CPU和内存使用率急剧上升。我分析发现,大部分(占CPU总消耗量的48%)是由select方法消耗的(其余大部分由gui事件消耗)。是因为这么多客户吗?我还看到一些人抱怨JRE7中的这个错误,并建议使用JRE6 对于使用JavaNIO的10000并发连接,java,nio,Java,Nio,我编写了一个服务器(类似于一个),并使用JavaNIO 我正在努力建立尽可能多的联系。 根据之前的建议,我放慢了客户端创建的速度,给操作系统(Windows8)足够的时间来处理请求 我在不同的机器上运行了客户机代码,这样服务器就有了所有可用的运行空间 当我尝试创建10000个连接时,大约8500个连接被连接,其余的连接被拒绝,而客户端(客户端代码中的线程)的连接被拒绝的情况发生得更多,这些情况是稍后创建的(客户端代码中的循环) 我的CPU和内存使用率急剧上升。我分析发现,大部分(占CPU总消耗量
javaw.exe
进程,内存使用率为2000+MB。(我注意到有一个进程内存不足,但CPU使用率很高)。当所有8500个左右的客户端都连接时,总体使用率大约为98%。系统挂起太多次,但仍在继续运行。我看到非页面池内存使用量在从178 MB增加到310 MB的过程中有所增加(最大限制是多少?)。这是因为当我们写入套接字时,使用了非页面池内存吗
有谁能告诉我,我可能会达到哪些极限,所以10000个成功的连接是不可能的?
(每个进程的套接字限制?)(非分页内存?)(是否再次出现积压队列?)
可能允许限制被推的调整?(Windows计算机)
我在4GB系统上使用Windows 8
`
公共类服务器实现可运行{
公共最终静态字符串地址=“192.168.2.14”;
公共最终静态int端口=8511;
公共最终静态长超时=10000;
公共int客户;
ByteBuffer readBuffer=ByteBuffer.allocate(1024);
专用服务器socketchannel服务器通道;
专用选择器;
私有映射数据跟踪=新HashMap();
公共服务器(){
init();
}
私有void init(){
System.out.println(“初始化服务器”);
如果(选择器!=null)返回;
if(serverChannel!=null)返回;
试一试{
选择器=selector.open();
serverChannel=ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(新的InetSocketAddress(地址,端口));
serverChannel.register(选择器、SelectionKey.OP_ACCEPT);
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
公开募捐{
System.out.println(“正在接受连接…”);
试一试{
而(!Thread.currentThread().isInterrupted()){
int ready=selector.select();
如果(就绪==0)
继续;
迭代器键=选择器。selectedKeys()。迭代器();
while(keys.hasNext()){
SelectionKey=keys.next();
键。移除();
如果(!key.isValid()){
继续;
}
if(key.isAcceptable()){
System.out.println(“接受连接”);
接受(钥匙);
}
if(key.isWritable()){
System.out.println(“写入…”);
写(键);
}
if(key.isReadable()){
System.out.println(“读取连接”);
读(键);
}
}
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
closeConnection();
}
}
私有无效写入(SelectionKey)引发IOException{
SocketChannel=(SocketChannel)key.channel();
字节[]数据=数据跟踪.get(通道);
数据跟踪。删除(通道);
**int count=channel.write(ByteBuffer.wrap(数据));
如果(计数=0)
{
key.interesttops(选择key.OP_WRITE);
返回;
}
否则,如果(计数>0)
{
关键字:0;
key.interesttops(选择key.OP_READ);
}**
}
私有void closeConnection(){
System.out.println(“关闭服务器”);
if(选择器!=null){
试一试{
selector.close();
serverChannel.socket().close();
serverChannel.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
私有void accept(SelectionKey)引发IOException{
ServerSocketChannel ServerSocketChannel=(ServerSocketChannel)key.channel();
SocketChannel SocketChannel=serverSocketChannel.accept();
if(socketChannel==null)
{
抛出新IOException();
}
socketChannel.configureBlocking(假);
客户端++;
**//socketChannel.register(选择器,SelectionKey.OP|u WRITE | SelectionKey.OP|u READ);
SelectionKey skey=socketChannel.register(选择器,SelectionKey.OP_READ)**
byte[]hello=新字符串(“来自服务器的hello”).getBytes();
dataTracking.put(socketChannel,你好);
}
私有无效读取(SelectionKey)引发IOException{
SocketChannel=(SocketChannel)key.channel();
readBuffer.clear();
整数长度;
试一试{
长度=通道读取(读缓冲区);
}捕获(IOE异常){
System.out.println(“读取问题,关闭连接”);
System.out.println(“客户数量:+客户);
键。取消();
channel.close();
返回;
}
如果(长度==-1){
System.out.println(“没有要读取的内容,正在关闭连接”);
channel.close();
键。取消();
返回;
}
readBuffer.flip();
字节[]数据=新字节[1000];
get(数据,0,长度);
String fromclient=新字符串(数据,0,长度,“UTF-8”);
System.out.println(“接收:“+来自客户”);
字符串dat=fromclient+channel.getRemoteAddress();
data=dat.getBytes();
回波(键、数据);
}
普里瓦特
public class Server implements Runnable {
public final static String ADDRESS = "192.168.2.14";
public final static int PORT = 8511;
public final static long TIMEOUT = 10000;
public int clients;
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
private ServerSocketChannel serverChannel;
private Selector selector;
private Map<SocketChannel,byte[]> dataTracking = new HashMap<SocketChannel, byte[]>();
public Server(){
init();
}
private void init(){
System.out.println("initializing server");
if (selector != null) return;
if (serverChannel != null) return;
try {
selector = Selector.open();
serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(new InetSocketAddress(ADDRESS, PORT));
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
System.out.println("Now accepting connections...");
try{
while (!Thread.currentThread().isInterrupted()){
int ready = selector.select();
if(ready==0)
continue;
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()){
SelectionKey key = keys.next();
keys.remove();
if (!key.isValid()){
continue;
}
if (key.isAcceptable()){
System.out.println("Accepting connection");
accept(key);
}
if (key.isWritable()){
System.out.println("Writing...");
write(key);
}
if (key.isReadable()){
System.out.println("Reading connection");
read(key);
}
}
}
} catch (IOException e){
e.printStackTrace();
} finally{
closeConnection();
}
}
private void write(SelectionKey key) throws IOException{
SocketChannel channel = (SocketChannel) key.channel();
byte[] data = dataTracking.get(channel);
dataTracking.remove(channel);
**int count = channel.write(ByteBuffer.wrap(data));
if(count == 0)
{
key.interestOps(SelectionKey.OP_WRITE);
return;
}
else if(count > 0)
{
key.interestOps(0);
key.interestOps(SelectionKey.OP_READ);
}**
}
private void closeConnection(){
System.out.println("Closing server down");
if (selector != null){
try {
selector.close();
serverChannel.socket().close();
serverChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void accept(SelectionKey key) throws IOException{
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
if(socketChannel == null)
{
throw new IOException();
}
socketChannel.configureBlocking(false);
clients++;
**//socketChannel.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ);
SelectionKey skey = socketChannel.register(selector, SelectionKey.OP_READ);**
byte[] hello = new String("Hello from server").getBytes();
dataTracking.put(socketChannel, hello);
}
private void read(SelectionKey key) throws IOException{
SocketChannel channel = (SocketChannel) key.channel();
readBuffer.clear();
int length;
try {
length = channel.read(readBuffer);
} catch (IOException e) {
System.out.println("Reading problem, closing connection");
System.out.println("No of clients :"+clients);
key.cancel();
channel.close();
return;
}
if (length == -1){
System.out.println("Nothing was there to be read, closing connection");
channel.close();
key.cancel();
return;
}
readBuffer.flip();
byte[] data = new byte[1000];
readBuffer.get(data, 0, length);
String fromclient = new String(data,0,length,"UTF-8");
System.out.println("Received: "+fromclient);
String dat = fromclient+channel.getRemoteAddress();
data= dat.getBytes();
echo(key,data);
}
private void echo(SelectionKey key, byte[] data) throws IOException{
SocketChannel socketChannel = (SocketChannel) key.channel();
dataTracking.put(socketChannel, data);
**//key.interestOps(SelectionKey.OP_WRITE);
try
{
write(key);
}
catch(IOException e)
{
System.out.println("Problem in echo"+e);
e.printStackTrace();
}
}
public static void main(String [] args)
{
Thread serv = new Thread(new Server());
serv.start();
}
socketChannel.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ);