自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

多線程NIO客戶端實例

開發(fā) 后端
本文只給出了多線程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. //發(fā)送   
  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. //將緩沖的數(shù)據(jù)打包到數(shù)組中   
  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實現(xiàn)Echo服務(wù)
  2. Java NIO 深入研究
  3. Java NIO聊天窗口實例
  4. Java NIO 經(jīng)典實例代碼
  5. Java NIO性能測試
責(zé)任編輯:林師授 來源: ReFly的博客
相關(guān)推薦

2010-03-17 18:04:55

java Socket

2009-06-08 20:16:15

Eclipse客戶端多線程

2011-08-17 10:10:59

2021-09-22 15:46:29

虛擬桌面瘦客戶端胖客戶端

2010-05-31 10:11:32

瘦客戶端

2011-10-26 13:17:05

2011-08-01 14:25:00

2011-03-02 14:36:24

Filezilla客戶端

2010-12-21 11:03:15

獲取客戶端證書

2011-03-24 13:00:31

配置nagios客戶端

2025-01-10 10:19:40

多線程socket數(shù)據(jù)

2011-03-21 14:53:36

Nagios監(jiān)控Linux

2009-03-04 10:27:50

客戶端組件桌面虛擬化Xendesktop

2011-04-06 14:24:20

Nagios監(jiān)控Linux

2013-05-09 09:33:59

2021-06-29 07:47:23

多線程協(xié)作數(shù)據(jù)

2012-10-11 17:02:02

IBMdw

2012-11-28 11:05:42

IBMdW

2010-07-06 15:21:25

UDP客戶端

2011-05-24 16:47:20

數(shù)據(jù)存儲
點贊
收藏

51CTO技術(shù)棧公眾號