Server:
package aio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Server implements Runnable {
int clientId = 0;
/**
* @param args
*/
public static void main(String[] args) {
Server server = new Server();
server.run();
}
@Override
public void run() {
try {
ExecutorService executor = Executors.newCachedThreadPool();
AsynchronousChannelGroup asyncChannelGroup = AsynchronousChannelGroup.withCachedThreadPool(executor, 1024);
final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open(asyncChannelGroup)
.bind(new InetSocketAddress(7910));
Future<AsynchronousSocketChannel> future = listener.accept(null,
new CompletionHandler<AsynchronousSocketChannel, Context>() {
@Override
public void completed(AsynchronousSocketChannel ch, Context context) {
clientId++;
// accept the next connection
listener.accept(null, this);
// handle this connection
System.out.println("连接:" + clientId);
handle(ch);
}
@Override
public void failed(Throwable exc, Context context) {
}
@Override
public void cancelled(Context context) {
}
});
AsynchronousSocketChannel ch = future.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void handle(final AsynchronousSocketChannel ch) {
final ByteBuffer buf = ByteBuffer.allocate(2048);
final int id = clientId;
try {
Future result = ch.read(buf, null, new CompletionHandler<Integer, Context>() {
@Override
public void completed(Integer result, Context context) {
if (result > 0) {
buf.flip();
byte[] data = new byte[buf.limit()];
buf.get(data);
System.err.println(id + "读完成!" + result + ",data=" + bin2hexstr(data));
buf.clear();
}
ch.read(buf, null, this);
}
@Override
public void cancelled(Context context) {
}
@Override
public void failed(Throwable exc, Context context) {
}
});
result.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public String bin2hexstr(byte[] src) {
return bin2hexstr(src, 0, src.length);
}
public String bin2hexstr(byte[] src, int start, int len) {
char[] hex = new char[2];
StringBuffer strBuffer = new StringBuffer(len * 2);
int abyte;
for (int i = start; i < start + len; i++) {
abyte = src[i] < 0 ? 256 + src[i] : src[i];
hex[0] = HEX[abyte / 16];
hex[1] = HEX[abyte % 16];
strBuffer.append(hex);
}
return strBuffer.toString();
}
public final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
}
Client:
package aio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Client {
static int connectnum = 0;
static int msgnum = 0;
static boolean isStart = false;
/**
* @param args
*/
public static void main(String[] args) {
int threadNum = 5000;
final int perNum = 10;
for (int i = 0; i < threadNum; i++) {
new Thread() {
@Override
public void run() {
newClient(perNum);
connectnum++;
System.out.println("建立连接:" + connectnum);
}
}.start();
isStart = true;
}
}
public static void newClient(int perNum) {
try {
final AsynchronousSocketChannel channels[] = new AsynchronousSocketChannel[perNum];
for (int i = 0; i < channels.length; i++) {
channels[i] = AsynchronousSocketChannel.open();
channels[i].setOption(StandardSocketOption.TCP_NODELAY, true);
channels[i].setOption(StandardSocketOption.SO_REUSEADDR, true);
channels[i].setOption(StandardSocketOption.SO_KEEPALIVE, true);
Future<Void> future1 = channels[i].connect(new InetSocketAddress("192.168.0.194", 7910));
try {
future1.get(60, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
} catch (TimeoutException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
byte[] d = "abcdefg1234567890".getBytes();
final ByteBuffer buf1 = ByteBuffer.allocate(512);
int i = 0;
while (true) {
for (AsynchronousSocketChannel channel : channels) {
if (isStart) {
i++;
msgnum++;
buf1.putInt(d.length + 4 * 4);
buf1.putInt(msgnum);
buf1.putInt(connectnum);
buf1.putInt(i);
buf1.put(d);
buf1.flip();
Future<Integer> future = channel.write(buf1, null, new CompletionHandler<Integer, Void>() {
@Override
public void cancelled(Void attachment) {
}
@Override
public void completed(Integer result, Void attachment) {
System.err.println("----完成写!" + result);
buf1.clear();
}
@Override
public void failed(Throwable exc, Void attachment) {
}
});
try {
future.get();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}