日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
多線程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

創(chuàng)新互聯(lián)主要業(yè)務有網(wǎng)站營銷策劃、網(wǎng)站設計、網(wǎng)站制作、微信公眾號開發(fā)、小程序制作、H5高端網(wǎng)站建設、程序開發(fā)等業(yè)務。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務的過程中,公司還積累了豐富的行業(yè)經(jīng)驗、成都全網(wǎng)營銷推廣資源和合作伙伴關系資源,并逐漸建立起規(guī)范的客戶服務和保障體系。 


新聞名稱:多線程NIO客戶端實例
當前路徑:http://www.5511xx.com/article/cdhjjhe.html