PC 与 Android 的adb同步通信(二)

客户端(pc端):

  1. import java.io.BufferedInputStream; 
  2. import java.io.BufferedOutputStream; 
  3. import java.io.BufferedReader; 
  4. import java.io.ByteArrayOutputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import java.io.InputStreamReader; 
  8. import java.net.InetAddress; 
  9. import java.net.Socket; 
  10. import java.net.UnknownHostException; 


  11. public class testPcClient { 
  12. /** 
  13. * @param args 
  14. * @throws InterruptedException 
  15. */ 


  16. public static void main(String[] args) throws InterruptedException { 
  17. try { 
  18. Runtime.getRuntime().exec( 
  19. "adb shell am broadcast -a NotifyServiceStop"); 
  20. Thread.sleep(3000); 
  21. Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086"); 
  22. Thread.sleep(3000); 
  23. Runtime.getRuntime().exec( 
  24. "adb shell am broadcast -a NotifyServiceStart"); 
  25. Thread.sleep(3000); 
  26. } catch (IOException e3) { 
  27. e3.printStackTrace(); 



  28. Socket socket = null; 
  29. try { 
  30. InetAddress serverAddr = null; 
  31. serverAddr = InetAddress.getByName("127.0.0.1"); 
  32. System.out.println("TCP 1111" + "C: Connecting..."); 
  33. socket = new Socket(serverAddr, 12580); 
  34. String str = "hi,wufenglong"; 
  35. System.out.println("TCP 221122" + "C:RECEIVE"); 
  36. BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); 
  37. BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); 
  38. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
  39. boolean flag = true; 


  40. while (flag) { 
  41. System.out.print("请输入1~6的数字,退出输入exit:"); 
  42. String strWord = br.readLine();// 从控制台输入1~6 
  43. if (strWord.equals("1")) { 
  44. out.write("1".getBytes()); 
  45. out.flush(); 
  46. System.out.println("1 finish sending the data"); 
  47. String strFormsocket = readFromSocket(in); 
  48. System.out.println("the data sent by server is:/r/n"+ strFormsocket); 
  49. System.out.println("============================================="); 
  50. } else if (strWord.equals("2")) { 
  51. out.write("2".getBytes()); 
  52. out.flush(); 
  53. System.out.println("2 finish sending the data"); 
  54. String strFormsocket = readFromSocket(in); 
  55. System.out.println("the data sent by server is:/r/n"+ strFormsocket); 
  56. System.out.println("============================================="); 
  57. } else if (strWord.equals("3")) { 
  58. out.write("3".getBytes()); 
  59. out.flush(); 
  60. System.out.println("3 finish sending the data"); 
  61. String strFormsocket = readFromSocket(in); 
  62. System.out.println("the data sent by server is:/r/n" + strFormsocket); 
  63. System.out.println("============================================="); 
  64. } else if (strWord.equals("4")) { 
  65. /* 发送命令 */ 
  66. out.write("4".getBytes()); 
  67. out.flush(); 
  68. System.out.println("send file finish sending the CMD:"); 
  69. /* 服务器反馈:准备接收 */ 
  70. String strFormsocket = readFromSocket(in); 
  71. System.out.println("service ready receice data:UPDATE_CONTACTS:"+ strFormsocket); 
  72. byte[] filebytes = FileHelper.readFile("R0013340.JPG"); 
  73. System.out.println("file size=" + filebytes.length); 
  74. /* 将整数转成4字节byte数组 */ 
  75. byte[] filelength = new byte[4]; 
  76. filelength = tools.intToByte(filebytes.length); 
  77. /* 将.apk字符串转成4字节byte数组 */ 
  78. byte[] fileformat = null; 
  79. fileformat = ".apk".getBytes(); 
  80. System.out.println("fileformat length=" + fileformat.length); 
  81. /* 字节流中前4字节为文件长度,4字节文件格式,以后是文件流 */ 
  82. /* 注意如果write里的byte[]超过socket的缓存,系统自动分包写过去,所以对方要循环写完 */ 
  83. out.write(filelength); 
  84. out.flush(); 
  85. String strok1 = readFromSocket(in); 
  86. System.out.println("service receive filelength :" + strok1); 
  87. // out.write(fileformat); 
  88. // out.flush(); 
  89. // String strok2 = readFromSocket(in); 
  90. // System.out.println("service receive fileformat :" + 
  91. // strok2); 
  92. System.out.println("write data to android"); 
  93. out.write(filebytes); 
  94. out.flush(); 
  95. System.out.println("*********"); 
  96. /* 服务器反馈:接收成功 */ 
  97. String strread = readFromSocket(in); 
  98. System.out.println(" send data success:" + strread); 
  99. System.out.println("============================================="); 
  100. } else if (strWord.equalsIgnoreCase("EXIT")) { 
  101. out.write("EXIT".getBytes()); 
  102. out.flush(); 
  103. System.out.println("EXIT finish sending the data"); 
  104. String strFormsocket = readFromSocket(in); 
  105. System.out.println("the data sent by server is:/r/n"+ strFormsocket); 
  106. flag = false; 
  107. System.out.println("============================================="); 


  108. } catch (UnknownHostException e1) { 
  109. System.out.println("TCP 331133" + "ERROR:" + e1.toString()); 
  110. } catch (Exception e2) { 
  111. System.out.println("TCP 441144" + "ERROR:" + e2.toString()); 
  112. } finally { 
  113. try { 


  114. if (socket != null) { 
  115. socket.close(); 
  116. System.out.println("socket.close()"); 

  117. } catch (IOException e) { 
  118. System.out.println("TCP 5555" + "ERROR:" + e.toString()); 





  119. /* 从InputStream流中读数据 */ 
  120. public static String readFromSocket(InputStream in) { 
  121. int MAX_BUFFER_BYTES = 4000; 
  122. String msg = ""; 
  123. byte[] tempbuffer = new byte[MAX_BUFFER_BYTES]; 
  124. try { 
  125. int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length); 
  126. msg = new String(tempbuffer, 0, numReadedBytes, "utf-8"); 
  127. tempbuffer = null; 
  128. } catch (Exception e) { 
  129. e.printStackTrace(); 

  130. // Log.v(Service139.TAG, "msg=" + msg); 
  131. return msg; 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值