多线程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

创新互联主要业务有网站营销策划、网站设计、网站制作、微信公众号开发、小程序制作H5高端网站建设、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、成都全网营销推广资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

网页名称:多线程NIO客户端实例
路径分享:http://www.shufengxianlan.com/qtweb/news9/32909.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联