多线程NIO客户端实例

开发 后端
本文只给出了多线程NIO客户端的代码实例。
  1. package night;   
  2. import java.net .InetSocketAddress;   
  3. import java.io .IOException;   
  4. import java.io .BufferedReader;   
  5. import java.io .InputStreamReader;   
  6. import java.nio.ByteBuffer;   
  7. import java.nio.channels .SocketChannel;   
  8. import java.nio.channels.*;   
  9.  
  10.  
  11. public class Kfd extends Thread {   
  12.  
  13. /**   
  14. * @param args   
  15. */   
  16.  
  17.  
  18.  
  19. public static void main(String[] args) {   
  20.  
  21.     // TODO Auto-generated method stub   
  22.             new Kfd();   
  23.             System.out.println("请输入:");   
  24. }   
  25.  
  26.  
  27.  
  28.  
  29. static SocketChannel sc;   
  30. String host="127.0.0.1";   
  31. int port=10000;   
  32. ByteBuffer readbuf =ByteBuffer.allocate(1024);   
  33. ByteBuffer writebuf=ByteBuffer.allocate(1024);   
  34. InetSocketAddress ad=new InetSocketAddress(host,port);   
  35. public  Kfd  (){   
  36. start();   
  37. try {sc=SocketChannel.open();   
  38. Selector sl=Selector.open();   
  39. sc.configureBlocking(false);   
  40. //连接到server;   
  41.      sc.connect(ad);   
  42.      System.out.println("客服端连接成功");   
  43.      if(!sc.finishConnect()){   
  44.     System.out.print("客户端连接失败");   
  45.      }   
  46. catch (Exception e) {   
  47. // TODO: handle exception   
  48. e.printStackTrace();   
  49. }   
  50. }   
  51.  
  52.  
  53.  
  54. public void run() {   
  55. while(true){   
  56. try {   
  57. BufferedReader buf=   
  58. new BufferedReader   
  59.    (new InputStreamReader(System.in));   
  60.     String str =buf.readLine();   
  61.     byte[]pack=str.getBytes();   
  62. writebuf.clear();   
  63. writebuf.put(pack);   
  64. writebuf.flip();   
  65. System.out.println("到这里");   
  66. //发送   
  67.         send(sc);   
  68.         //接受   
  69.         recive(sc);   
  70.       
  71. }   
  72. catch (Exception e) {   
  73. // TODO: handle exception   
  74. e.printStackTrace();   
  75. }   
  76. }   
  77. }   
  78.  
  79.  
  80.  
  81. private void recive(SocketChannel sc)throws IOException{   
  82. while(true){   
  83. if(sc.isConnectionPending()){   
  84. sc.read(readbuf);   
  85. }   
  86. readbuf.flip();   
  87. byte[]pack=null;   
  88. pack=new byte[readbuf.limit()];   
  89. //将缓冲的数据打包到数组中   
  90. readbuf.get(pack);   
  91. System.out.println(new String(pack.toString()));   
  92. }   
  93. }   
  94.  
  95.  
  96. private void send(SocketChannel sc){   
  97. while(true){   
  98. while(writebuf.hasRemaining()){   
  99. try {   
  100. sc.write(writebuf);   
  101.  
  102. catch (Exception e) {   
  103. // TODO: handle exception   
  104. e.printStackTrace();   
  105.  
  106. }   
  107. }   
  108. }   
  109. }   
  110. }  

 原文链接:http://refly.iteye.com/blog/1053152

【编辑推荐】

  1. 用nio实现Echo服务
  2. Java NIO 深入研究
  3. Java NIO聊天窗口实例
  4. Java NIO 经典实例代码
  5. Java NIO性能测试
责任编辑:林师授 来源: ReFly的博客
相关推荐

2010-03-17 18:04:55

java Socket

2009-06-08 20:16:15

Eclipse客户端多线程

2021-09-22 15:46:29

虚拟桌面瘦客户端胖客户端

2011-08-17 10:10:59

2010-05-31 10:11:32

瘦客户端

2011-03-24 13:00:31

配置nagios客户端

2010-12-21 11:03:15

获取客户端证书

2011-03-02 14:36:24

Filezilla客户端

2011-10-26 13:17:05

2011-08-01 14:25:00

2011-04-06 14:24:20

Nagios监控Linux

2011-03-21 14:53:36

Nagios监控Linux

2009-03-04 10:27:50

客户端组件桌面虚拟化Xendesktop

2013-05-09 09:33:59

2010-02-22 09:03:22

零客户端瘦客户端VDI终端

2009-11-17 15:02:27

Oracle客户端

2011-03-25 14:25:38

NagiosWindows监控

2009-07-15 17:33:08

Swing客户端

2021-06-29 07:47:23

多线程协作数据

2012-10-11 17:02:02

IBMdw
点赞
收藏

51CTO技术栈公众号