<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);
        }

    }

    posted @ 2012-09-26 22:40 王樹東 閱讀(4372) | 評論 (0)編輯 收藏

    僅列出標題  下一頁
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    公告

    常用鏈接

    留言簿

    隨筆分類(17)

    隨筆檔案(15)

    文章分類(4)

    文章檔案(5)

    收藏夾(4)

    Algorithm

    Design

    Environment Setup

    Installer

    Maven

    MINA

    OS

    Skills for Java

    VIM

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久永久免费人妻精品| 国产精品免费一区二区三区| 久久久久久夜精品精品免费啦| 亚洲国产精品一区二区第四页| 亚洲国产成人综合精品| 无码视频免费一区二三区| 亚洲最大福利视频| 一个人免费观看www视频在线| 色偷偷女男人的天堂亚洲网| 69成人免费视频无码专区| 亚洲一区二区三区写真| 日本xxwwxxww在线视频免费| 国产亚洲蜜芽精品久久| 亚洲va中文字幕无码| 中文成人久久久久影院免费观看| 亚洲国产精品无码久久一区二区| 无码一区二区三区免费| 亚洲国产成+人+综合| 国产精品另类激情久久久免费 | 亚洲综合色在线观看亚洲| 国产高潮久久免费观看| 亚洲欧洲免费视频| 无码少妇一区二区浪潮免费| 含羞草国产亚洲精品岁国产精品| 亚洲综合久久夜AV | 亚洲免费二区三区| 久久久久久久久无码精品亚洲日韩| 高清在线亚洲精品国产二区| 毛片在线播放免费观看| 亚洲av午夜精品无码专区| 国产免费啪嗒啪嗒视频看看| 3344在线看片免费| 久久乐国产综合亚洲精品| av无码东京热亚洲男人的天堂| 七色永久性tv网站免费看| 亚洲午夜福利在线视频| 亚洲真人无码永久在线| www.黄色免费网站| 成人无码区免费A∨直播| 久久精品国产亚洲αv忘忧草| 亚洲国产成人影院播放|