how to use adb port forwarding?

11,366

I suggest you to read and follow steps explained in this tutorial. This tutorial really helped me when i was writing an android game controller for PC games, where android application was sending inputs to PC over USB (using adb)

  1. Am I want to start ServerSocket on both developing machine and mobile?

No, only one side must open ServerSocket (for me opening ServerSocket on android device and connecting using client socket from PC worked perfectly)

  1. If So, I started ServerSocket: on port 8080 and device ServerSocket: on 9090 then execute adb forward tcp:8080 tcp:9090 will be diverted to 8080 that is on developing machine?

Dont know if opening ServerSocket on a development machine works for you, it did not worked for me when i used adb. Make sure you are doing following steps

a. Open ServerSocket on one side and listen for connections (ServerSocker.accept()) say port no 38100

b. From development machine execute adb command forward tcp:38300 tcp:38100

c. Then on client side call socket = new Socket("localhost", 38300);

d. Transfer data

  1. How to execute adb commands from java code?

Using java Runtime class

String adbPath = "C:\\android-sdk-windows\\tools\\adb.exe";
Runtime.getRuntime().exec(adbPath + " forward tcp:38300 tcp:38100");

see Execute external program from java

Share:
11,366
noobEinstien
Author by

noobEinstien

Updated on June 27, 2022

Comments

  • noobEinstien
    noobEinstien almost 2 years

    I am creating a debugging plugin in for Android Studio, and this will start a server and listen to a port. The device(mobile with adb deamon running) connected via USB.

    And my application will send some files, requests, logs simply data to a port. This is my concept.

    So I want to send it through USB. While some digging I found ADB port forwarding/reversing is the answer for this. But I didn't understand the whole concept of port forwarding using adb.

    1. Am I want to start ServerSocket on both developing machine and mobile?

    2. If So, I started ServerSocket: on port 8080 and device ServerSocket: on 9090 then execute adb forward tcp:8080 tcp:9090 will be diverted to 8080 that is on developing machine?

    3. How they forward or redirect to machine port?

    4. How to execute adb commands from java code?

    The ultimate aim is I want to communicate with my application/library from phone to developing machine.

    I am a noob on networking and this kind of things.

    Thanks in advance.