<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java NIO 簡單經典示例

    Java NIO 主要是Channel, SelectionKey, Selector 三個類之間的關系,下面的例子就是演示如果使用NIO來處理請求的:/**
     * 
     */
    package dongzi.nio.exercise.nio;

    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.nio.ByteBuffer;
    import java.nio.channels.ClosedChannelException;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;

    /**
     * 
    @author kyle
     * 
     
    */
    public class SelectSockets {

        private static final int PORT_NUMBER = 1234;

        /**
         * 
    @param args
         
    */
        public static void main(String[] args) {
            new SelectSockets().go(args);
        }

        private void go(String[] args) {
            int port = PORT_NUMBER;
            if (args.length > 0) {
                try {
                    port = Integer.parseInt(args[0]);
                } catch (Exception e) {
                }
            }

            System.out.println("Listening port: " + PORT_NUMBER);
            try {
                Selector selector = Selector.open();
                startServer(port, selector);
                while (true) {
                    int n = selector.select();
                    if (n == 0) {
                        continue;
                    }

                    Iterator it = selector.selectedKeys().iterator();
                    while (it.hasNext()) {
                        SelectionKey key = (SelectionKey) it.next();
                        if (key.isAcceptable()) {
                            ServerSocketChannel server = (ServerSocketChannel) key
                                    .channel();
                            SocketChannel channel = server.accept();
                            registerChannel(selector, channel, SelectionKey.OP_READ);
                            sayHello(channel);

                        }
                        if (key.isReadable()) {
                            readDataFromChannel(key);
                        }
                    }

                    it.remove();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private ByteBuffer buffer = ByteBuffer.allocate(1024);

        private void readDataFromChannel(SelectionKey key) throws IOException {
            int count = 0;
            SocketChannel channel = (SocketChannel) key.channel();
            buffer.clear();
            while ((count = channel.read(buffer)) > 0) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    System.out.println(buffer.get());
                }
                buffer.clear();
            }
            if (count < 0) {
                channel.close();
            }

        }

        private void sayHello(SocketChannel channel) throws IOException {
            if (channel == null) {
                return;
            }
            buffer.clear();
            ByteBuffer buffer = ByteBuffer.wrap("Hi, there \r\n".getBytes());
            buffer.flip();
            channel.write(buffer);
        }

        private void registerChannel(Selector selector, SocketChannel channel,
                int opRead) throws IOException {

            if (channel == null) {
                return;
            }

            channel.configureBlocking(false);
            channel.register(selector, opRead);
        }

        private void startServer(int port, Selector selector) throws IOException,
                ClosedChannelException {
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            serverChannel.configureBlocking(false);
            ServerSocket serverSocket = serverChannel.socket();
            serverSocket.bind(new InetSocketAddress(port));
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        }

    }


    Kyle Wang

    posted on 2012-09-26 22:40 王樹東 閱讀(4368) 評論(0)  編輯  收藏 所屬分類: Java Skills Learning and Sharing

    <2012年9月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    導航

    統計

    公告

    常用鏈接

    留言簿

    隨筆分類(17)

    隨筆檔案(15)

    文章分類(4)

    文章檔案(5)

    收藏夾(4)

    Algorithm

    Design

    Environment Setup

    Installer

    Maven

    MINA

    OS

    Skills for Java

    VIM

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国内精品免费麻豆网站91麻豆| 日韩精品无码免费专区午夜 | 久操视频免费观看| 国产亚洲人成A在线V网站| 九九全国免费视频| 亚洲免费一区二区| 丁香六月婷婷精品免费观看| 国产成人免费一区二区三区| 国产成人亚洲毛片| 亚洲国产一区二区三区| 一区二区三区视频免费| 国产成人精品久久亚洲| h视频在线免费观看| 在线观看亚洲精品国产| 日韩精品无码免费专区午夜不卡| 亚洲AV成人无码久久精品老人| 久久精品无码专区免费青青| 亚洲成人网在线播放| 成人免费无码大片A毛片抽搐| 亚洲av成人中文无码专区| 又爽又高潮的BB视频免费看| 夜夜爽妓女8888视频免费观看| 在线观看国产区亚洲一区成人 | 一级做a免费视频观看网站| 国产精品亚洲一区二区三区在线| 日韩在线不卡免费视频一区| 亚洲国产av高清无码| 免费高清资源黄网站在线观看| 免费国产va在线观看| 亚洲精品无码MV在线观看| 99久热只有精品视频免费看| 国产精品亚洲片夜色在线| 国产男女猛烈无遮挡免费网站| 特级毛片aaaa免费观看| 久久丫精品国产亚洲av| 成人一a毛片免费视频| 精品熟女少妇aⅴ免费久久| 久久精品国产亚洲av麻豆小说 | 国产成人亚洲精品91专区手机| 日本一卡精品视频免费| 国产精品亚洲精品日韩电影|