zoukankan      html  css  js  c++  java
  • NIO

    package com.test;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.concurrent.TimeUnit;
    
    public class TestNIO {
        @Test
        public void clientNIO() {
            ByteBuffer buf = ByteBuffer.allocate(1024);
            SocketChannel socketChannel = null;
            try {
                socketChannel = SocketChannel.open();
                socketChannel.configureBlocking(false);
                socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
                if (socketChannel.finishConnect()) {
                    int i = 0;
                    while (true) {
                        TimeUnit.SECONDS.sleep(1);
                        String info = "I'm " + i++ + "-th information from client";
                        buf.clear();
                        buf.put(info.getBytes());
                        buf.flip();
                        while (buf.hasRemaining()) {
                            System.out.println(buf);
                            socketChannel.write(buf);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socketChannel != null) {
                        socketChannel.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        @Test
        public void serverNIO() {
            Selector selector = null;
            ServerSocketChannel ssc = null;
            try {
                selector = Selector.open();
                ssc = ServerSocketChannel.open();
                ssc.socket().bind(new InetSocketAddress(8080));
                ssc.configureBlocking(false);
                ssc.register(selector, SelectionKey.OP_ACCEPT);
                while (true) {
                    if (selector.select(3000) == 0) {
                        System.out.println("没有准备就绪的链接");
                        continue;
                    }
                    Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
                    while (iter.hasNext()) {
                        SelectionKey key = iter.next();
                        if (key.isAcceptable()) {
                            handleAccept(key);
                        }
                        if (key.isReadable()) {
                            handleRead(key);
                        }
                        if (key.isWritable() && key.isValid()) {
                            handleWrite(key);
                        }
                        if (key.isConnectable()) {
                            System.out.println("isConnectable = true");
                        }
                        iter.remove();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (selector != null) {
                        selector.close();
                    }
                    if (ssc != null) {
                        ssc.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void handleAccept(SelectionKey key) throws IOException {
            ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
            SocketChannel sc = ssc.accept();
            sc.configureBlocking(false);
            sc.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocateDirect(1024));
        }
    
        public void handleRead(SelectionKey key) throws IOException {
            SocketChannel sc = (SocketChannel) key.channel();
            ByteBuffer buf = (ByteBuffer) key.attachment();
            long byteSize = sc.read(buf);
            while (byteSize > 0) {
                buf.flip();
                while (buf.hasRemaining()) {
                    System.out.print((char) buf.get());
                }
                System.out.println();
                buf.clear();
                byteSize = sc.read(buf);
            }
            if (byteSize == -1) {
                sc.close();
            }
        }
    
        public void handleWrite(SelectionKey key) throws IOException {
            ByteBuffer buf = (ByteBuffer) key.attachment();
            buf.flip();
            SocketChannel sc = (SocketChannel) key.channel();
            while (buf.hasRemaining()) {
                sc.write(buf);
            }
            buf.compact();
        }
    }
    
  • 相关阅读:
    数据库中总结2
    PyMySQL的基本使用
    数据库总结
    并发编程之多线程
    并发编程之多进程知识
    并发编程之多进程
    操作系统基础知识
    模块二总结
    Python函数进阶
    文件操作
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327722.html
Copyright © 2011-2022 走看看