JAVA AIO 例子 客户端发送与服务端接收消息
所属分类 AIO
浏览量 77
客户端发送消息
服务端接收客户端消息并打印 ,不响应
服务端
AioServer.java
AcceptHandler.java
ReadHandler.java
客户端
AioClient2.java
AioServer.java
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
public class AioServer {
static final int PORT = 8050;
public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getName() + " AioServer start,port="+PORT);
AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(PORT));
serverChannel.accept(null, new AcceptHandler());
Thread.sleep(Integer.MAX_VALUE);
}
}
AcceptHandler.java
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
public class AcceptHandler implements CompletionHandler< AsynchronousSocketChannel, Void> {
@Override
public void completed(java.nio.channels.AsynchronousSocketChannel clientChannel, java.lang.Void attachment) {
System.out.println(Thread.currentThread().getName() + " client is connected");
// 预分配缓冲区 用于读取
ByteBuffer buffer = ByteBuffer.allocate(1024);
clientChannel.read(buffer, buffer, new ReadHandler(clientChannel));
}
@Override
public void failed(Throwable exc, java.lang.Void attachment) {
System.out.println("accept fail");
}
}
ReadHandler.java
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.StandardCharsets;
public class ReadHandler implements CompletionHandler< Integer, ByteBuffer> {
final AsynchronousSocketChannel clientChannel;
public ReadHandler(final AsynchronousSocketChannel clientChannel){
this.clientChannel = clientChannel;
}
@Override
public void completed(Integer result, ByteBuffer buffer) {
// flip 翻转 ,写模式转为读取模式
buffer.flip();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
System.out.println(Thread.currentThread().getName() + " received:" + new String(data, StandardCharsets.UTF_8));
// 继续新的读取
ByteBuffer bufferNew = ByteBuffer.allocate(1024);
clientChannel.read(bufferNew,bufferNew,this);
}
@Override
public void failed(Throwable exc, ByteBuffer buffer) {
System.out.println("ReadHandler.failed,"+exc);
}
}
AioClient2.java
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
public class AioClient2 {
static final String HOST = "127.0.0.1";
static final int PORT = 8050;
static final int MAX = 1000000;
static final AtomicInteger NO = new AtomicInteger(0);
public static void main(String[] args) throws Exception {
System.out.println("AioClient start,server host="+HOST+",port="+PORT);
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
channel.connect(new InetSocketAddress(HOST, PORT));
for(int i=0;i f = channel.write(buffer);
Integer result = f.get();
System.out.println("write done,"+result+","+NO.get());
}
}
}
https://gitee.com/dyyx/work2024/blob/master/demo/aiodemo/src/main/java/demo1/server/AioServer.java
上一篇
下一篇
java ByteBuffer flip
java AIO 笔记
java AIO 使用注意点
java AIO echo server
smart-http 1.6.1 实例
java NIO ByteBuffer 读写整数