SocketUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.uas.esop.util;
  2. import android.annotation.SuppressLint;
  3. import android.util.Log;
  4. import java.io.BufferedReader;
  5. import java.io.DataInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.io.PrintWriter;
  11. import java.net.Socket;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.concurrent.TimeUnit;
  15. import io.reactivex.rxjava3.core.Observable;
  16. import io.reactivex.rxjava3.disposables.Disposable;
  17. /**
  18. * @author qiaohao
  19. * @date 21-7-15 下午8:02
  20. */
  21. public class SocketUtils {
  22. private static final String TAG = "MainActivity:SocketUtils" ;
  23. private ExecutorService mExecutorService = Executors.newCachedThreadPool();
  24. public static final int INIT_CODE = 0x10;
  25. public static final int RESP_CODE = 0x11;
  26. public static final int NOT_CONNECTED_CODE = 0x12;
  27. private Socket mSocket;
  28. private InputStream inputStream;
  29. private DataInputStream input;
  30. private OutputStream outputStream;
  31. private SocketCallback mSocketCallback;
  32. private String deviceId;
  33. private Disposable subscribe;
  34. private static class Inner {
  35. private static final SocketUtils instance = new SocketUtils();
  36. }
  37. public static SocketUtils getInstance() {
  38. return Inner.instance;
  39. }
  40. private SocketUtils() {
  41. }
  42. public String getDeviceId(){
  43. return deviceId;
  44. }
  45. @SuppressLint("LongLogTag")
  46. public void initSocket(String ip, String port, SocketCallback socketCallback, String deviceId) {
  47. mSocketCallback = socketCallback;
  48. this.deviceId = deviceId;
  49. mExecutorService.execute(() ->{
  50. try {
  51. Log.e(TAG, "initSocket: "+ ip +":"+port);
  52. mSocket = new Socket(ip, Integer.parseInt(port));
  53. mSocket.setKeepAlive(true);
  54. mSocketCallback.call(INIT_CODE, mSocket.isConnected());
  55. Log.e(TAG, "initSocket: "+mSocket.isConnected());
  56. if (!mSocket.isConnected()){
  57. return;
  58. }
  59. // send2Server("hello!!");
  60. // startTimer();
  61. inputStream = mSocket.getInputStream();
  62. InputStreamReader isr = new InputStreamReader(inputStream);
  63. BufferedReader br = new BufferedReader(isr);
  64. String data = null;
  65. //读取客户端数据
  66. while((data = br.readLine()) != null){
  67. mSocketCallback.call(RESP_CODE, data);
  68. }
  69. //关闭输入流
  70. // mSocket.shutdownInput();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. mSocketCallback.call(NOT_CONNECTED_CODE, e.getLocalizedMessage());
  74. }
  75. });
  76. }
  77. @SuppressLint("LongLogTag")
  78. private void startTimer(){
  79. stopTime();
  80. subscribe = Observable.interval(1, TimeUnit.SECONDS).subscribe((Long aLong) -> {
  81. send2Server("testData:"+aLong+"");
  82. Log.e(TAG, "startTimer: "+"testData:"+aLong+"" );
  83. });
  84. }
  85. private void stopTime(){
  86. if (subscribe!=null){
  87. subscribe.dispose();
  88. subscribe=null;
  89. }
  90. }
  91. public void send2Server(String content){
  92. mExecutorService.execute(()->{
  93. if (mSocket==null || !mSocket.isConnected()){
  94. mSocketCallback.call(NOT_CONNECTED_CODE, "服务未连接!");
  95. return;
  96. }
  97. try {
  98. outputStream = mSocket.getOutputStream();
  99. PrintWriter pw = new PrintWriter(outputStream);
  100. pw.write(content+"\n");
  101. pw.flush();
  102. //关闭输出流
  103. // mSocket.shutdownOutput();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. });
  108. }
  109. public void closeSocket(){
  110. try {
  111. stopTime();
  112. if (inputStream!=null){
  113. inputStream.close();
  114. inputStream = null;
  115. }
  116. if (input!=null){
  117. input.close();
  118. input = null;
  119. }
  120. if (mSocket!=null){
  121. mSocket.close();
  122. }
  123. Log.e("SocketUtils", "closeSocket: "+ mSocket.isConnected());
  124. if (mSocketCallback!=null){
  125. mSocketCallback.call(NOT_CONNECTED_CODE, "断开连接成功!");
  126. }
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. }