Java AIO 入门实例
来源:互联网 发布:ios开发需要学linux 编辑:程序博客网 时间:2024/05/29 03:21
原文出处:http://tigerlchen.iteye.com/blog/1747221
Java7 AIO入门实例,首先是服务端实现:
服务端代码
SimpleServer:
- public class SimpleServer {
- public SimpleServer(int port)throws IOException {
- final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));
- listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
- public void completed(AsynchronousSocketChannel ch, Void att) {
- // 接受下一个连接
- listener.accept(null, this);
- // 处理当前连接
- handle(ch);
- }
- public void failed(Throwable exc, Void att) {
- }
- });
- }
- public void handle(AsynchronousSocketChannel ch) {
- ByteBuffer byteBuffer = ByteBuffer.allocate(32);
- try {
- ch.read(byteBuffer).get();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ExecutionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- byteBuffer.flip();
- System.out.println(byteBuffer.get());
- // Do something
- }
- }
public class SimpleServer { public SimpleServer(int port) throws IOException { final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port)); listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() { public void completed(AsynchronousSocketChannel ch, Void att) { // 接受下一个连接 listener.accept(null, this); // 处理当前连接 handle(ch); } public void failed(Throwable exc, Void att) { } }); } public void handle(AsynchronousSocketChannel ch) { ByteBuffer byteBuffer = ByteBuffer.allocate(32); try { ch.read(byteBuffer).get(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } byteBuffer.flip(); System.out.println(byteBuffer.get()); // Do something } }
跟着是客户端实现:
客户端代码
SimpleClient:
- public class SimpleClient {
- private AsynchronousSocketChannel client;
- public SimpleClient(String host, int port) throws IOException, InterruptedException, ExecutionException {
- this.client = AsynchronousSocketChannel.open();
- Future<?> future = client.connect(new InetSocketAddress(host, port));
- future.get();
- }
- public void write(byte b) {
- ByteBuffer byteBuffer = ByteBuffer.allocate(32);
- byteBuffer.put(b);
- byteBuffer.flip();
- client.write(byteBuffer);
- }
- }
public class SimpleClient { private AsynchronousSocketChannel client; public SimpleClient(String host, int port) throws IOException, InterruptedException, ExecutionException { this.client = AsynchronousSocketChannel.open(); Future<?> future = client.connect(new InetSocketAddress(host, port)); future.get(); } public void write(byte b) { ByteBuffer byteBuffer = ByteBuffer.allocate(32); byteBuffer.put(b); byteBuffer.flip(); client.write(byteBuffer); }}
写一个简单的测试用例来跑服务端和客户端,先运行testServer(),在运行testClient();
测试用例
AIOTest
- public class AIOTest {
- @Test
- public void testServer()throws IOException, InterruptedException {
- SimpleServer server = new SimpleServer(7788);
- Thread.sleep(10000);
- }
- @Test
- public void testClient()throws IOException, InterruptedException, ExecutionException {
- SimpleClient client = new SimpleClient("localhost",7788);
- client.write((byte) 11);
- }
- }
public class AIOTest { @Test public void testServer() throws IOException, InterruptedException { SimpleServer server = new SimpleServer(7788); Thread.sleep(10000); } @Test public void testClient() throws IOException, InterruptedException, ExecutionException { SimpleClient client = new SimpleClient("localhost", 7788); client.write((byte) 11); }}
因为是异步的,所以在运行server的时候没有发生同步阻塞,在这里我加了一个线程sleep(),如果没有的话,程序会直接跑完回收掉。
- Java AIO 入门实例
- AIO入门实例
- java aio 入门
- Java AIO编程入门
- java aio
- aio java
- Java NIO.2 AIO 高性能服务器程序实例
- Java NIO.2 AIO 高性能服务器程序实例 收藏
- java 之异步套接字编程实例(AIO)
- java 之异步套接字编程实例(AIO)
- BIO NIO AIO实例
- jdk7 AIO 入门
- Netty入门--AIO
- JDK7 AIO初体验实例
- JDK7 AIO初体验实例
- java AIO学习
- Java IO NIO AIO
- java BIO NIO AIO
- javaSE之反射
- ubuntu双网卡分别连接内外网的配置
- java 获取图片高和宽
- RGBA alpha 透明度混合算法
- Java 将List按照指定大小分段
- Java AIO 入门实例
- 图的邻接表
- Spring --- AOP
- activity 见传递数据
- VELOCITY
- Java SVG Toolkit--Apache™ Batik
- 保留小数点位数和格式
- Attribute "resultClass" must be declared for element type "insert".
- java总结(1)