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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaNIO(異步IO)Socket通信例子

服務器代碼:

10年積累的做網(wǎng)站、成都做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有虹口免費網(wǎng)站建設讓你可以放心的選擇與我們合作。

 
 
 
  1. import java.net.*; 
  2. import java.nio.*; 
  3. import java.nio.channels.*; 
  4. import java.util.*; 
  5. public class server 
  6. ServerSocketChannel ssc ; 
  7. public void start() 
  8. try 
  9. Selector selector = Selector.open(); 
  10. ServerSocketChannel ssc=ServerSocketChannel.open(); 
  11. ssc.configureBlocking(false); 
  12. ServerSocket ss=ssc.socket(); 
  13. InetSocketAddress address = new InetSocketAddress(55555); 
  14. ss.bind(address); 
  15. ssc.register(selector, SelectionKey.OP_ACCEPT); 
  16. System.out.println("端口注冊完畢!"); 
  17. while(true) 
  18. selector.select(); 
  19. Set selectionKeys=selector.selectedKeys(); 
  20. Iterator iter=selectionKeys.iterator(); 
  21. ByteBuffer echoBuffer=ByteBuffer.allocate(20); 
  22. SocketChannel sc; 
  23. while(iter.hasNext()) 
  24. SelectionKey key=iter.next(); 
  25. if((key.readyOps()&SelectionKey.OP_ACCEPT)==SelectionKey.OP_ACCEPT) 
  26. ServerSocketChannel subssc=(ServerSocketChannel)key.channel(); 
  27. sc=subssc.accept(); 
  28. sc.configureBlocking(false); 
  29. sc.register(selector, SelectionKey.OP_READ); 
  30. iter.remove(); 
  31. System.out.println("有新連接:"+sc); 
  32. else if((key.readyOps()&SelectionKey.OP_READ)==SelectionKey.OP_READ) 
  33. sc=(SocketChannel) key.channel(); 
  34. while(true) 
  35. echoBuffer.clear(); 
  36. int a; 
  37. try 
  38. a=sc.read(echoBuffer); 
  39. catch(Exception e) 
  40. e.printStackTrace(); 
  41. break; 
  42. if(a==-1) break; 
  43. if(a>0) 
  44. byte[] b=echoBuffer.array(); 
  45. System.out.println("接收數(shù)據(jù): "+new String(b)); 
  46. echoBuffer.flip(); 
  47. sc.write(echoBuffer); 
  48. System.out.println("返回數(shù)據(jù): "+new String(b)); 
  49. sc.close(); 
  50. System.out.println("連接結束"); 
  51. System.out.println("============================="); 
  52. iter.remove(); 
  53. catch (Exception e) 
  54. e.printStackTrace(); 

客戶端代碼:

 
 
 
  1. import java.net.*; 
  2. import java.nio.*; 
  3. import java.nio.channels.*; 
  4. public class client 
  5. public void start() 
  6. try 
  7. SocketAddress address = new InetSocketAddress("localhost",55555); 
  8. SocketChannel client=SocketChannel.open(address); 
  9. client.configureBlocking(false); 
  10. String a="asdasdasdasddffasfas"; 
  11. ByteBuffer buffer=ByteBuffer.allocate(20); 
  12. buffer.put(a.getBytes()); 
  13. buffer.clear(); 
  14. int d=client.write(buffer); 
  15. System.out.println("發(fā)送數(shù)據(jù): "+new String(buffer.array())); 
  16. while(true) 
  17. buffer.flip(); 
  18. int i=client.read(buffer); 
  19. if(i>0) 
  20. byte[] b=buffer.array(); 
  21. System.out.println("接收數(shù)據(jù): "+new String(b)); 
  22. client.close(); 
  23. System.out.println("連接關閉!"); 
  24. break; 
  25. catch(Exception e) 
  26. e.printStackTrace(); 

網(wǎng)站標題:JavaNIO(異步IO)Socket通信例子
文章網(wǎng)址:http://www.5511xx.com/article/coiodpp.html