<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 王樹東 閱讀(4368) | 評論 (0)編輯 收藏

    僅列出標題  下一頁
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    公告

    常用鏈接

    留言簿

    隨筆分類(17)

    隨筆檔案(15)

    文章分類(4)

    文章檔案(5)

    收藏夾(4)

    Algorithm

    Design

    Environment Setup

    Installer

    Maven

    MINA

    OS

    Skills for Java

    VIM

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 在线天堂免费观看.WWW| 特级无码毛片免费视频尤物| 日韩特黄特色大片免费视频| 亚洲xxxx18| 国内自产拍自a免费毛片| 亚洲欧美国产国产综合一区| 精品免费久久久久久成人影院| 亚洲久悠悠色悠在线播放| 免费鲁丝片一级在线观看| 精品久久久久亚洲| 久久影视国产亚洲| 免费播放在线日本感人片| 亚洲国产精品自在线一区二区| 精品熟女少妇a∨免费久久| 在线精品亚洲一区二区| 又粗又大又猛又爽免费视频 | 国产片免费在线观看| 亚洲AV日韩AV无码污污网站| 又粗又大又猛又爽免费视频| 在线观看免费无码视频| 久久亚洲AV成人无码电影| 日韩免费一区二区三区在线| 亚洲国产av玩弄放荡人妇| 亚洲精品乱码久久久久久蜜桃| 免费毛片a线观看| 日韩亚洲产在线观看| 高清在线亚洲精品国产二区| 国产免费一区二区视频| 亚洲图片中文字幕| 免费国产一级特黄久久| 外国成人网在线观看免费视频| 亚洲人成网男女大片在线播放| 全部免费a级毛片| 99爱免费观看视频在线| 亚洲a∨无码一区二区| 亚洲精品无码久久久影院相关影片 | 7x7x7x免费在线观看| 亚洲精品无码久久久久APP| 亚洲精品无码mv在线观看网站| 免费中文熟妇在线影片| 国产特黄一级一片免费|