首页  

java AIO echo server     所属分类 AIO 浏览量 81
服务端接收消息 xxx ,服务端回应 response: xxx 

EchoServer.java
AcceptHandler.java
ReadHandler.java 
WriteHandler.java 


EchoServer.java import java.net.InetSocketAddress; import java.nio.channels.AsynchronousServerSocketChannel; public class EchoServer { static final int PORT = 8889; public static void main(String args[]) throws Exception { final AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(PORT)); serverSocketChannel.accept(null,new AcceptHandler()); System.out.println("EchoServer bind done,port="+PORT); Thread.sleep(100000000); } }
AcceptHandler.java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class AcceptHandler implements CompletionHandler< AsynchronousSocketChannel, Void> { // 256 static final int MAX_SIZE = 8; @Override public void completed(AsynchronousSocketChannel asynchronousSocketChannel, Void attachment) { try { System.out.println("connected from: " + asynchronousSocketChannel.getRemoteAddress()); } catch (IOException e) { e.printStackTrace(); } // 字节缓冲区大小 8 , 0123456789 会读取2次 ByteBuffer buffer = ByteBuffer.allocate(MAX_SIZE); asynchronousSocketChannel.read(buffer, buffer, new ReadHandler(asynchronousSocketChannel)); } @Override public void failed(Throwable throwable, Void attachment) { System.out.println("AcceptHandler.failed,"+throwable); throwable.printStackTrace(); } }
ReadHandler.java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.nio.charset.Charset; public class ReadHandler implements CompletionHandler< Integer, ByteBuffer> { static final Charset charSet = Charset.forName("UTF-8"); private final AsynchronousSocketChannel channel; public ReadHandler(AsynchronousSocketChannel channel) { this.channel = channel; } @Override public void completed(Integer intResult, ByteBuffer byteBuffer) { System.out.println("read-handler-"+Thread.currentThread()); byteBuffer.flip(); int remaining = byteBuffer.remaining(); System.out.println("intResult=" + intResult+",remaining="+remaining); byte[] message = new byte[remaining]; byteBuffer.get(message); String data = new String(message, charSet); System.out.println("received: " + data); String response = "response:" + data; doWrite(response); } @Override public void failed(Throwable throwable, ByteBuffer byteBuffer) { System.out.println("ReaderHandler.failed,"+throwable); try { channel.close(); } catch (IOException e) { e.printStackTrace(); } } private void doWrite(String result) { System.out.println("send back: " + result); byte[] bytes = result.getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length); writeBuffer.put(bytes); writeBuffer.flip(); channel.write(writeBuffer, writeBuffer, new WriteHandler(channel)); } }
WriteHandler.java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class WriteHandler implements CompletionHandler< Integer, ByteBuffer> { private final AsynchronousSocketChannel channel; public WriteHandler(final AsynchronousSocketChannel channel) { this.channel = channel; } @Override public void completed(Integer result, ByteBuffer buffer) { System.out.println("write-hanlder-"+Thread.currentThread()); // 没有发送完,就继续发送直到完成 if (buffer.hasRemaining()) { channel.write(buffer, buffer, this); } else { // 写完成 继续读取 // 创建新的Buffer 用于读取 // 1024 8 ByteBuffer readBuffer = ByteBuffer.allocate(8); channel.read(readBuffer, readBuffer, new ReadHandler(channel)); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println("WriterHandler.failed,"+exc); try { channel.close(); } catch (IOException e) { e.printStackTrace(); } } }
测试 telnet 127.0.0.1 8889 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 1 response:1 2 response:2 123456789 response:12345678response:9 3 response:3 4 response:4 5 response:5 6 response:6 123456789abcd response:12345678response:9abcd 7 response:7
https://gitee.com/dyyx/work2024/tree/master/demo/aiodemo/src/main/java/demo/echo2

上一篇     下一篇
java AIO 笔记

java AIO 使用注意点

JAVA AIO 例子 客户端发送与服务端接收消息

smart-http 1.6.1 实例

java NIO ByteBuffer 读写整数

java NIO ByteBuffer 使用技巧