Android - adb forward實現PC和App的Socket通訊

PC端的XX助手和手機App的通訊原理:

# 把PC端8000端口的數據, 轉發到Android端的9000端口上.
adb forward tcp:8000 tcp:9000

 什麼是轉發?

 

 

執行命令後, PC端的8000端口會被 adb
監聽, 這個時候我們只需要往8000端口寫數據, 這個數據就會發送到手機端的9000端口上.

PC端程序

把輸入內容發送給8000端口

 

public class PCClient {
    public static void main(String[] args) throws IOException {
        System.out.println("任意字符, 回車鍵發送Toast");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String msg = scanner.next();
            sendToast(msg);
        }
    }
    public static void sendToast(String msg) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8000);
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeUTF(msg);
        socket.close();
    }
}

 

Android端程序

監聽9000端口, 把收到的數據, Toast在屏幕上

 

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ServerThread";
    ServerThread serverThread;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(getApplicationContext(), msg.getData().getString("MSG", "Toast"), Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        serverThread = new ServerThread();
        serverThread.start();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        serverThread.setIsLoop(false);
    }
    class ServerThread extends Thread {
        boolean isLoop = true;
        public void setIsLoop(boolean isLoop) {
            this.isLoop = isLoop;
        }
        @Override
        public void run() {
            Log.d(TAG, "running");
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(9000);
                while (isLoop) {
                    Socket socket = serverSocket.accept();
                    Log.d(TAG, "accept");
                    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                    String msg = inputStream.readUTF();
                    Message message = Message.obtain();
                    Bundle bundle = new Bundle();
                    bundle.putString("MSG", msg);
                    message.setData(bundle);
                    handler.sendMessage(message);
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Log.d(TAG, "destory");
                if (serverSocket != null) {
                    try {
                        serverSocket.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

 

運行效果



 

源碼

Android-Pc-Socket-Connection

實際開發中的問題

  1. Android端的程序 有可能被幹掉
  2. adb forward 有可能會被幹掉

由於連接不穩定性,判斷真正連接成功的方法,只有輪詢收發握手數據包:
C發送一個數據包,等待S回覆;
C如果收到了S的回覆包,說明連通。
如果接收超時,則認爲沒有連通.
在沒有連通的情況下,需要重新建立Socket,並Connect(),然後再嘗試握手。


作者:口袋FPV
鏈接:http://www.jianshu.com/p/fee5b31774be
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
發佈了159 篇原創文章 · 獲贊 55 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章