Android - adb forward realizes Socket communication between PC and App

The communication principle between the XX assistant on the PC side and the mobile app:

# Forward the data of port 8000 on the PC side to port 9000 on the Android side.
adb forward tcp:8000 tcp:9000

 What is forwarding?

 

 

After executing the command, the port 8000 on the PC will be
monitored by adb. At this time, we only need to write data to port 8000, and the data will be sent to port 9000 on the mobile phone.

PC-side program

Send input to port 8000

 

public class PCClient {
    public static void main(String[] args) throws IOException {
        System.out.println("Any character, enter 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 side program

Listen to port 9000, put the received data, Toast on the screen

 

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 ();
                    }
                }
            }
        }
    }
}

 

running result



 

source code

Android-Pc-Socket-Connection

Problems in actual development

  1. The program on the Android side may be killed
  2. adb forward may be killed

Due to the instability of the connection, the only way to judge the success of the connection is to poll the sending and receiving handshake data packets:
C sends a data packet and waits for the reply from S;
if C receives the reply packet from S, it means that it is connected.
If the reception times out, it is considered that there is no connection.
In the case of no connection, the Socket needs to be re-established, and Connect(), and then try the handshake.


Author: Pocket FPV
Link: http://www.jianshu.com/p/fee5b31774be
Source: Jianshu
Copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613828&siteId=291194637