parent
4de53a806b
commit
ab1e5374d7
@ -0,0 +1,74 @@
|
||||
package com.msb.io.aio;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/27 11:35
|
||||
* @Description: com.msb.io.aio
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
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.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
*@ClassName Server
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/27 11:35
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class PoolServer {
|
||||
public static void main(String[] args) throws IOException {
|
||||
ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1);
|
||||
final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open(channelGroup).bind(new InetSocketAddress(8888));
|
||||
serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
|
||||
|
||||
@Override
|
||||
public void completed(AsynchronousSocketChannel client, Object attachment) {
|
||||
// public void completed(AsynchronousSocketChannel result, Object attachment) {
|
||||
serverChannel.accept(null, this);
|
||||
try {
|
||||
System.out.println(serverChannel.getLocalAddress());
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
|
||||
client.read(byteBuffer, byteBuffer, new CompletionHandler<Integer, ByteBuffer>() {
|
||||
@Override
|
||||
public void completed(Integer result, ByteBuffer attachment) {
|
||||
attachment.flip();
|
||||
System.out.println(new String(attachment.array(), 0, result));
|
||||
client.write(ByteBuffer.wrap("hello client!".getBytes()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
});
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, Object attachment) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("server 执行返回");
|
||||
try {
|
||||
while (true) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.msb.io.aio;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/27 11:35
|
||||
* @Description: com.msb.io.aio
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousServerSocketChannel;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
|
||||
/**
|
||||
*@ClassName Server
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/27 11:35
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Server {
|
||||
public static void main(String[] args) throws IOException {
|
||||
final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888));
|
||||
serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
|
||||
|
||||
@Override
|
||||
public void completed(AsynchronousSocketChannel client, Object attachment) {
|
||||
// public void completed(AsynchronousSocketChannel result, Object attachment) {
|
||||
serverChannel.accept(null, this);
|
||||
try {
|
||||
System.out.println(serverChannel.getLocalAddress());
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
|
||||
client.read(byteBuffer, byteBuffer, new CompletionHandler<Integer, ByteBuffer>() {
|
||||
@Override
|
||||
public void completed(Integer result, ByteBuffer attachment) {
|
||||
attachment.flip();
|
||||
System.out.println(new String(attachment.array(), 0, result));
|
||||
client.write(ByteBuffer.wrap("hello client!".getBytes()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
});
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, Object attachment) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("server 执行返回");
|
||||
try {
|
||||
while (true) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue