package com.github.booknara.nioexample;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Daehee Han(@daniel_booknara) on 2/12/16.
 * This example is an incomplete code.
 */
public class SelectorExample {
    public static void main(String[] args) {
        Selector selector;
        // Define socket channel
//         SocketChannel channel = new SocketChannel();
//
//        try {
//            selector = Selector.open();
//            channel.configureBlocking(false);
//
//            SelectionKey selectionKey = channel.register(selector, SelectionKey.OP_READ);
//
//
//            while(true) {
//
//                // Return how many channels are ready to read
//                int readyChannels = selector.select();
//
//                if(readyChannels == 0)
//                    continue;
//
//
//                Set<SelectionKey> selectedKeys = selector.selectedKeys();
//
//                Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
//
//                while(keyIterator.hasNext()) {
//
//                    SelectionKey key = keyIterator.next();
//
//                    if(key.isAcceptable()) {
//                        // a connection was accepted by a ServerSocketChannel.
//                        // ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
//                        // SocketChannel socketChannel = (SocketChannel) key.channel();
//
//                    } else if (key.isConnectable()) {
//                        // a connection was established with a remote server.
//
//                    } else if (key.isReadable()) {
//                        // a channel is ready for reading
//
//                    } else if (key.isWritable()) {
//                        // a channel is ready for writing
//                    }
//
//                    keyIterator.remove();
//                }
//            }
//
//        } catch (IOException e) {
//            System.out.println(e.getStackTrace());
//        }

    }
}

@@ -62,16 +62,51 @@
 eption;%0A
+import java.net.InetSocketAddress;%0A
 import j
@@ -324,47 +324,8 @@
 16.%0A
- * This example is an incomplete code.%0A
  */%0A
@@ -355,16 +355,56 @@
 ample %7B%0A
+    public static final int PORT = 80;%0A%0A
     publ
@@ -467,24 +467,41 @@
 lector;%0A
+%0A        try %7B%0A  
         
 // Defin
@@ -492,16 +492,18 @@
         
+  
 // Defin
@@ -519,18 +519,19 @@
 channel%0A
-//
+   
         
@@ -558,12 +558,8 @@
 el =
- new
  Soc
@@ -572,33 +572,125 @@
 nnel
-();%0A//%0A//        try %7B%0A//
+.open();%0A            InetSocketAddress address = new InetSocketAddress(PORT);%0A            channel.connect(address);%0A%0A
     
@@ -721,26 +721,24 @@
 tor.open();%0A
-//
             
@@ -771,21 +771,17 @@
 false);%0A
-//%0A//
+%0A
         
@@ -862,24 +862,18 @@
 _READ);%0A
-//%0A//%0A//
+%0A%0A
         
@@ -886,29 +886,25 @@
 ile(true) %7B%0A
-//%0A//
+%0A
             
@@ -953,18 +953,16 @@
 to read%0A
-//
         
@@ -1004,29 +1004,25 @@
 r.select();%0A
-//%0A//
+%0A
             
@@ -1048,18 +1048,16 @@
 s == 0)%0A
-//
         
@@ -1082,16 +1082,10 @@
 ue;%0A
-//%0A//%0A//
+%0A%0A
     
@@ -1150,29 +1150,25 @@
 ctedKeys();%0A
-//%0A//
+%0A
             
@@ -1229,29 +1229,25 @@
 iterator();%0A
-//%0A//
+%0A
             
@@ -1281,21 +1281,17 @@
 xt()) %7B%0A
-//%0A//
+%0A
         
@@ -1337,29 +1337,25 @@
 tor.next();%0A
-//%0A//
+%0A
             
@@ -1379,34 +1379,32 @@
 Acceptable()) %7B%0A
-//
                 
@@ -1466,18 +1466,16 @@
 hannel.%0A
-//
         
@@ -1568,26 +1568,24 @@
 .channel();%0A
-//
             
@@ -1660,21 +1660,17 @@
 nnel();%0A
-//%0A//
+%0A
         
@@ -1707,34 +1707,32 @@
 onnectable()) %7B%0A
-//
                 
@@ -1793,21 +1793,17 @@
 server.%0A
-//%0A//
+%0A
         
@@ -1837,34 +1837,32 @@
 isReadable()) %7B%0A
-//
                 
@@ -1903,21 +1903,17 @@
 reading%0A
-//%0A//
+%0A
         
@@ -1951,26 +1951,24 @@
 itable()) %7B%0A
-//
             
@@ -2013,18 +2013,16 @@
 writing%0A
-//
         
@@ -2027,37 +2027,33 @@
               %7D%0A
-//%0A//
+%0A
                 
@@ -2078,18 +2078,16 @@
 move();%0A
-//
         
@@ -2096,18 +2096,16 @@
       %7D%0A
-//
         
@@ -2114,13 +2114,9 @@
   %7D%0A
-//%0A//
+%0A
     
@@ -2145,18 +2145,16 @@
 on e) %7B%0A
-//
         
@@ -2196,18 +2196,16 @@
 ace());%0A
-//
         

Add SocketChannel configuration