zoukankan      html  css  js  c++  java
  • 使用nio实现web服务器

    package com.nio;
    
    import java.io.IOException;
    import java.net.InetAddress;
    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.Date;
    import java.util.Iterator;
    import java.util.Set;
    
    public class NIOServer {
        public static void main(String[] args) throws IOException, InterruptedException {
            Selector selector = Selector.open();
    
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8080);
            serverSocketChannel.socket().bind(address);
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            String content="";
            while (true) {
                if (selector.select() > 0) {
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();
                    Iterator<SelectionKey> it = selectionKeys.iterator();
    
                    while (it.hasNext()) {
                        SelectionKey selectionKey = it.next();
                        if (selectionKey.isAcceptable()) {
                            serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
                            SocketChannel socketChannel = serverSocketChannel.accept();
                            socketChannel.configureBlocking(false);
                            socketChannel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
                            System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
                        } else if (selectionKey.isReadable()) {
                            SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                            ByteBuffer buffer = ByteBuffer.allocate(1024);
                            String msg="";
                            while (socketChannel.read(buffer) > 0) {
                                buffer.flip();
                                byte[] dis = new byte[buffer.limit()];
                                buffer.get(dis);
                                content+=new String(dis);
                                buffer.flip();
      msg+=new String(dis);
                                System.out.println("当前线程="+Thread.currentThread().getId()+"--"+new String(dis));
                            }
                            socketChannel.write(ByteBuffer.wrap((new Date()).toString().getBytes()));
                            socketChannel.close();
    
                        }
                        else  if (selectionKey.isWritable()){
    //                        String msg="";
    //                        SocketChannel  socketChannel=(SocketChannel)selectionKey.channel();
    //
    //                        socketChannel.write(ByteBuffer.wrap(("abc001"+msg).getBytes()));
    //                        socketChannel.close();
    
                        }
    
                        it.remove(); //若不删除,下次执行到selector.select 会持续返回0 造成 死循环
                    }
                }
    
                Thread.sleep(100);
            }
        }
    }
  • 相关阅读:
    九度oj 题目1465:最简真分数
    九度oj 题目1083:特殊乘法 清华大学2010年机试题目
    九度oj 题目1084:整数拆分 清华大学2010年机试题目
    九度oj 题目1085:求root(N, k) 清华2010年机试题目
    九度oj 题目1460:Oil Deposit
    九度oj 题目1459:Prime ring problem
    九度oj 题目1458:汉诺塔III
    九度oj 题目1457:非常可乐
    题目1451:不容易系列之一
    移动端滚动不流畅,添加-webkit-overflow-scrolling属性 值为touch
  • 原文地址:https://www.cnblogs.com/tiancai/p/9388907.html
Copyright © 2011-2022 走看看