标签:
上次写了一个socket的基本编程,但是有个问题,阻塞特别严重,于是小编便去找了nio学习了一下...
1 public class TimeServer { 2 3 public static void main(String[] args) { 4 MultipexerTimeServer timersServer=new MultipexerTimeServer("192.168.1.102", 8400); 5 new Thread(timersServer, "nio-多路复用器").start(); 6 } 7 8 }
1 public class MultipexerTimeServer implements Runnable { 2 3 private Selector selector; 4 5 private ServerSocketChannel serverSocketChannel; 6 7 private volatile boolean stop; 8 9 public MultipexerTimeServer(String addr,int point){ 10 try { 11 selector=Selector.open(); 12 serverSocketChannel=ServerSocketChannel.open(); 13 serverSocketChannel.socket().bind(new InetSocketAddress(addr, point)); 14 serverSocketChannel.configureBlocking(false); 15 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); 16 System.out.println("socket服务端:"+serverSocketChannel.socket().getInetAddress()+":"+serverSocketChannel.socket().getLocalPort()+""); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 System.exit(1); 20 } 21 } 22 23 public void stop(){ 24 this.stop=true; 25 } 26 27 @Override 28 public void run() { 29 while(!stop){ 30 try { 31 selector.select(1000); 32 Set<SelectionKey> selectionKeys=selector.selectedKeys(); 33 if(selectionKeys !=null && selectionKeys.size()>0){ 34 Iterator<SelectionKey> it=selectionKeys.iterator(); 35 SelectionKey key=null; 36 while(it.hasNext()){ 37 key=it.next(); 38 it.remove(); 39 try { 40 handleInput(key); 41 } catch (Exception e) { 42 if(key!=null){ 43 key.cancel(); 44 if(key.channel()!=null){ 45 try { 46 key.channel().close(); 47 } catch (Exception e1) { 48 e1.printStackTrace(); 49 } 50 } 51 } 52 } 53 } 54 } 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 } 59 if(selector!=null){ 60 try { 61 selector.close(); 62 } catch (Exception e) { 63 e.printStackTrace(); 64 } 65 } 66 } 67 68 private void handleInput(SelectionKey key) throws IOException{ 69 if(key.isValid()){ 70 //处理新接入的请求 71 if(key.isAcceptable()){ 72 ServerSocketChannel ssc=(ServerSocketChannel)key.channel(); 73 SocketChannel sc=ssc.accept(); 74 sc.configureBlocking(false); 75 sc.register(selector,SelectionKey.OP_READ); 76 SocketAddress clientAddress =sc.getRemoteAddress(); 77 System.out.println("socket链接新客户端:" + clientAddress); 78 } 79 if(key.isReadable()){ 80 //读取数据 81 SocketChannel sc=(SocketChannel)key.channel(); 82 ByteBuffer readBuffer=ByteBuffer.allocate(1024); 83 int readBytes=sc.read(readBuffer); 84 if(readBytes>0){ 85 readBuffer.flip(); 86 byte[] bytes=new byte[readBuffer.remaining()]; 87 readBuffer.get(bytes); 88 String body=new String(bytes, Charset.forName("GBK")); 89 SocketAddress clientAddress =sc.getRemoteAddress(); 90 System.out.println("socket链接老客户端:" + clientAddress); 91 System.out.println("服务器接收数据:"+body); 92 //然后回写点数据给客服端 93 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 94 String time=dateFormat.format(new Date()); 95 String res="来自与服务端的回应,时间:"+ time; 96 doWrite(sc,res); 97 }else if(readBytes<0){ 98 key.cancel(); 99 sc.close(); 100 }else{ 101 //System.out.println("服务器收到0字节数据..."); 102 } 103 } 104 } 105 } 106 107 private void doWrite(SocketChannel sc,String res) throws IOException{ 108 if(res!=null && res.trim().length()>0){ 109 byte[] bytes=res.getBytes(); 110 ByteBuffer byteBuffer=ByteBuffer.allocate(1024); 111 byteBuffer.put(bytes); 112 byteBuffer.flip(); 113 sc.write(byteBuffer); 114 } 115 } 116
代码不想解释太多,有空整理一下nio的基本概念...
标签:
原文地址:http://www.cnblogs.com/huzi007/p/5496665.html