在java的套接字编程中,大部分一般使用阻塞IO套接字编程。套接字的读取和写入会阻塞(也就是说不管现在有没有写入/读出数据 调用read和write方法将会阻塞)。而NIO将I/O事件注册,当特定的注册I/O事件到达时会通知您。不需要轮询,也不需要创建大量的线程下面一个例子:下载

Server代码  

  1. package simple.socket;  

  2.   

  3. import java.io.IOException;  

  4. import java.net.InetSocketAddress;  

  5. import java.net.ServerSocket;  

  6. import java.nio.ByteBuffer;  

  7. import java.nio.channels.*;  

  8. import java.util.Iterator;  

  9. import java.util.Set;  

  10.   

  11. /**  

  12.  * Created by banxia on 16/10/12.  

  13.  */  

  14. public class UseNioSocket {  

  15.   

  16.   

  17.     public static void main(String[] args) throws IOException {  

  18.   

  19.         // 创建一个selector 对象  

  20.         Selector selector = Selector.open();  

  21.   

  22.         ServerSocketChannel ssc = ServerSocketChannel.open();  

  23.         ssc.configureBlocking(false);  

  24.         ssc.socket().bind(new InetSocketAddress(8888));  

  25.         ssc.register(selector, SelectionKey.OP_ACCEPT);  

  26.         ByteBuffer buffer = ByteBuffer.allocate(1024);  

  27.   

  28.         System.out.println("服务已经启动端口:" + 8888);  

  29.   

  30.         while (true) {  

  31.   

  32.             int selectNum = selector.select();  

  33.             System.out.println("selectNum=" + selectNum);  

  34.   

  35.             if (selectNum <= 0) {  

  36.                 continue;  

  37.             }  

  38.             // 获取  

  39.   

  40.             Iterator<SelectionKey> it = selector.selectedKeys().iterator();  

  41.   

  42.             while (it.hasNext()) {  

  43.   

  44.                 SelectionKey key = it.next();  

  45.   

  46.                 if (key.readyOps() == SelectionKey.OP_ACCEPT) {  

  47.                     // 接收 accept  

  48.   

  49.                     ServerSocketChannel channel = (ServerSocketChannel) key.channel();  

  50.                     SocketChannel accept = channel.accept();  

  51.                     accept.configureBlocking(false);  

  52.                     System.out.println("接收客户端:" + accept.socket().getInetAddress().getHostAddress() + ":" +  accept.socket().getPort());  

  53.                     accept.register(selector, SelectionKey.OP_READ);  

  54.                     it.remove();  

  55.   

  56.   

  57.                 } else {  

  58.   

  59.                     SocketChannel sc = (SocketChannel) key.channel();  

  60.                     System.out.println("接收客户端:" + sc.socket().getInetAddress().getHostAddress() + ":" + sc.socket().getPort() +"开始处理...");  

  61.                     while (true) {  

  62.                         buffer.clear();  

  63.                         long l = sc.read(buffer);  

  64.                         if (l <= 0) {  

  65.                             break;  

  66.                         }  

  67.                         System.out.print(".");  

  68.                         buffer.flip();  

  69.                         sc.write(buffer);  

  70.   

  71.   

  72.                     }  

  73.                     System.out.print("\n");  

  74.                     it.remove();  

  75.                     System.out.println("接收客户端:" + sc.socket().getInetAddress().getHostAddress() + ":" + sc.socket().getPort() +"处理完成处理...");  

  76.   

  77.                 }  

  78.   

  79.             }  

  80.   

  81.   

  82.         }  

  83.   

  84.   

  85.     }  

  86. }