作家
登录

Java NIO(异步IO)Socket通信例子

作者: 来源: 2012-06-12 18:33:09 阅读 我要评论

服务器代码:

  1. import java.net.*; 
  2. import java.nio.*; 
  3. import java.nio.channels.*; 
  4. import java.util.*; 
  5. public class server 
  6. ServerSocketChannel ssc ; 
  7. public void start() 
  8. try 
  9. Selector selector = Selector.open(); 
  10. ServerSocketChannel ssc=ServerSocketChannel.open(); 
  11. ssc.configureBlocking(false); 
  12. ServerSocket ss=ssc.socket(); 
  13. InetSocketAddress address = new InetSocketAddress(55555); 
  14. ss.bind(address); 
  15. ssc.register(selector, SelectionKey.OP_ACCEPT); 
  16. System.out.println("端口注册完毕!"); 
  17. while(true
  18. selector.select(); 
  19. Set<SelectionKey> selectionKeys=selector.selectedKeys(); 
  20. Iterator<SelectionKey> iter=selectionKeys.iterator(); 
  21. ByteBuffer echoBuffer=ByteBuffer.allocate(20); 
  22. SocketChannel sc; 
  23. while(iter.hasNext()) 
  24. SelectionKey key=iter.next(); 
  25. if((key.readyOps()&SelectionKey.OP_ACCEPT)==SelectionKey.OP_ACCEPT) 
  26. ServerSocketChannel subssc=(ServerSocketChannel)key.channel(); 
  27. sc=subssc.accept(); 
  28. sc.configureBlocking(false); 
  29. sc.register(selector, SelectionKey.OP_READ); 
  30. iter.remove(); 
  31. System.out.println("有新连接:"+sc); 
  32. else if((key.readyOps()&SelectionKey.OP_READ)==SelectionKey.OP_READ) 
  33. sc=(SocketChannel) key.channel(); 
  34. while(true
  35. echoBuffer.clear(); 
  36. int a; 
  37. try 
  38. a=sc.read(echoBuffer); 
  39. catch(Exception e) 
  40. e.printStackTrace(); 
  41. break
  42. if(a==-1break
  43. if(a>0
  44. byte[] b=echoBuffer.array(); 
  45. System.out.println("接收数据: "+new String(b)); 
  46. echoBuffer.flip(); 
  47. sc.write(echoBuffer); 
  48. System.out.println("返回数据: "+new String(b)); 
  49. sc.close(); 
  50. System.out.println("连接结束"); 
  51. System.out.println("============================="); 
  52. iter.remove(); 
  53. catch (Exception e) 
  54. e.printStackTrace(); 

客户端代码:

  1. import java.net.*; 
  2. import java.nio.*; 
  3. import java.nio.channels.*; 
  4. public class client 
  5. public void start() 
  6. try 
  7. SocketAddress address = new InetSocketAddress("localhost",55555); 
  8. SocketChannel client=SocketChannel.open(address); 
  9. client.configureBlocking(false); 
  10. String a="asdasdasdasddffasfas"
  11. ByteBuffer buffer=ByteBuffer.allocate(20); 
  12. buffer.put(a.getBytes()); 
  13. buffer.clear(); 
  14. int d=client.write(buffer); 
  15. System.out.println("发送数据: "+new String(buffer.array())); 
  16. while(true
  17. buffer.flip(); 
  18. int i=client.read(buffer); 
  19. if(i>0
  20. byte[] b=buffer.array(); 
  21. System.out.println("接收数据: "+new String(b)); 
  22. client.close(); 
  23. System.out.println("连接关闭!"); 
  24. break
  25. catch(Exception e) 
  26. e.printStackTrace(); 

原文链接:http://blog.sina.com.cn/s/blog_5df388620100plwi.html

【编辑推荐】

  1. 影响Java NIO框架性能的因数
  2. java.nio.Buffer的一些基础知识的备忘
  3. 甲骨文Java专利遭拒 起诉Android侵权受挫
  4. Java企业应用问题代码最多
  5. Java的NIO以及线程并发

  推荐阅读

  影响Java NIO框架性能的因数

最近打算用kilim做一个rpc框架, kilim有自己的nio框架 而在业界有强劲的netty和mina。所以问了一下kilim的作者,他的回答说 因为底层用的都是java nio的api,所以留给nio框架最主要的问题是这2点:(i) 为了处理很多>>>详细阅读


本文标题:Java NIO(异步IO)Socket通信例子

地址:http://www.17bianji.com/kaifa2/Java/1474.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)