400-028-6601
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

JavaNIO(异步IO)Socket通信例子

服务器代码:

10年积累的做网站、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有虹口免费网站建设让你可以放心的选择与我们合作。

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

客户端代码:

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

网页标题:JavaNIO(异步IO)Socket通信例子
文章位置:http://www.hnjierui.cn/article/coiodpp.html

其他资讯