作家
登录

多线程NIO客户端实例

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

  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性能测试

  推荐阅读

  Swing开发时必定会碰到的11个问题及解决方案

1.JTable ,JTable如何在内容里面存放组件(下拉框,图片)和修改数据。2.系统托盘TrayIcon的使用3.JPopupMenu的使用4.JTree的使用及替换样式5.BorderLayout的灵活使用6.进度条的同步读取和显示7.滚动条下拉时候滚动>>>详细阅读


本文标题:多线程NIO客户端实例

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

关键词: 探索发现

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

网友点评
自媒体专栏

评论

热度

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