private void go() throws IOException{
        Selector selector = Selector.open();

        //监听三个端口
        for(int i=0; i<ports.length; ++i){
            //一个channel对应一个socket
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ServerSocket ss = ssc.socket();
            InetSocketAddress address = new InetSocketAddress(ports[i]);
            ss.bind(address);

            SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);

            System.out.println("Going to listen on " + ports[i]);
        }

        while(true){
            System.out.println("gogogo");
            // 返回在selector中哪个channel被选中了。
//            Selects a set of keys whose corresponding channels are ready for I/O operations.
//             This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's wakeup method is invoked, or the current thread is interrupted, whichever comes first.
            int num = selector.select(); //如果一个都没有，就会blocking 住；
            System.out.println("after select");


            //选中的所有keys
            Set selectedKeys = selector.selectedKeys();
            Iterator it = selectedKeys.iterator();

            while(it.hasNext()){
                //读到了一个key
                SelectionKey key = (SelectionKey) it.next();

                if((key.readyOps() & SelectionKey.OP_ACCEPT)
                    == SelectionKey.OP_ACCEPT){
                    //Accept the new connection
                    ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                    SocketChannel sc = ssc.accept();
                    sc.configureBlocking(false);

                    //Add the connection to the selector
                    SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ);

                    System.out.println("Got connection from " + sc);
                }else if ((key.readyOps() & SelectionKey.OP_READ)
                        == SelectionKey.OP_READ){
                    SocketChannel sc = (SocketChannel)key.channel();

                    int bytesEchoed = 0;
                    while(true){
                        echoBuffer.clear();

                        int r = sc.read(echoBuffer);

                        if(r<=0){
                            break;
                        }

                        echoBuffer.flip();

                        sc.write(echoBuffer);
                        bytesEchoed += r;
                    }

                    System.out.println( "Echoed "+bytesEchoed+" from "+sc );
                    it.remove();
                }

            }
        }
    }