AIO示例

服务端代码:

package com.self.netty.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.ExecutorService;
import java.util.concurrent.Executors;/*** AIO 服务端 */
public class AIOServer {private final int port;public static void main(String args[]) {int port = 8000;new AIOServer(port);}public AIOServer(int port) {this.port = port;listen();}private void listen() {try {ExecutorService executorService = Executors.newCachedThreadPool();// 工作线程, 用来进行回调, 事件响应时候进行回调AsynchronousChannelGroup threadGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1);final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(threadGroup);server.bind(new InetSocketAddress(port));System.out.println("服务已启动,监听端口" + port);// 准备接收数据server.accept(null, new CompletionHandler() {final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);// 实现 Completed 方法, 进行回调public void completed(AsynchronousSocketChannel result, Object attachment) {System.out.println("IO 操作成功,开始获取数据");try {buffer.clear();result.read(buffer).get();buffer.flip();result.write(buffer);buffer.flip();} catch (Exception e) {System.out.println(e.toString());} finally {try {result.close();server.accept(null, this);} catch (Exception e) {System.out.println(e.toString());}}System.out.println("操作完成");}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("IO 操作是失败: " + exc);}});try {Thread.sleep(Integer.MAX_VALUE);} catch (InterruptedException ex) {ex.printStackTrace();}} catch (IOException e) {e.printStackTrace();}}
}

客户端代码:

package com.self.netty.aio;import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;/*** @author pj_zhang* @create 2019-11-12 22:43**/
public class AIOClient {private final AsynchronousSocketChannel client;public AIOClient() throws Exception {client = AsynchronousSocketChannel.open();}public void connect(String host, int port) throws Exception {client.connect(new InetSocketAddress(host, port), null, new CompletionHandler() {@Overridepublic void completed(Void result, Void attachment) {try {client.write(ByteBuffer.wrap("这是一条测试数据".getBytes())).get();System.out.println("已发送至服务器");} catch (Exception ex) {ex.printStackTrace();}}@Overridepublic void failed(Throwable exc, Void attachment) {exc.printStackTrace();}});final ByteBuffer bb = ByteBuffer.allocate(1024);client.read(bb, null, new CompletionHandler() {@Overridepublic void completed(Integer result, Object attachment) {System.out.println("IO 操作完成" + result);System.out.println("获取反馈结果" + new String(bb.array()));}@Overridepublic void failed(Throwable exc, Object attachment) {exc.printStackTrace();}});try {Thread.sleep(Integer.MAX_VALUE);} catch (InterruptedException ex) {ex.printStackTrace();}}public static void main(String args[]) throws Exception {new AIOClient().connect("localhost", 8000);}}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部