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

开发 后端
本文主要给出了Java NIO(异步IO)Socket通信的例子代码。

服务器代码:

  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以及线程并发
责任编辑:林师授 来源: neal的博客
相关推荐

2011-12-13 17:31:07

2011-12-07 17:17:02

JavaNIO

2023-08-01 08:43:29

Python多线程

2010-03-22 10:42:37

Java Socket

2023-02-07 19:46:35

NIOCQ内核

2019-10-18 08:22:43

BIONIOAIO

2010-03-22 13:25:47

Java Socket

2011-12-07 17:05:45

JavaNIO

2021-01-09 13:59:49

异步IO驱动式

2009-08-03 16:45:02

C#异步Socket

2010-03-19 13:48:15

Java Socket

2015-09-08 10:06:18

JavaSocket编程通信

2010-03-18 20:13:03

Java socket

2012-02-15 10:34:29

JavaJava Socket

2023-12-06 07:28:47

阻塞IO异步IO

2013-01-24 10:14:54

SilverlightRIASocket

2023-04-12 18:36:20

IO框架内核

2010-03-19 11:12:23

Java Socket

2012-02-15 10:26:40

JavaJava Socket

2020-03-27 11:14:18

IONIOJava
点赞
收藏

51CTO技术栈公众号