SoFunction
Updated on 2025-03-08

Android mobile phone and PC use adb forward communication

Use adb forward communication between PC and Android mobile phone

The server-side code is as follows:

import ; 
import ; 
import ; 
import ; 
import ; 
public class Server { 
  public static final String TAG = "server"; 
  public static int PC_LOCAL_PORT = 22222; 
  public static int PHONE_PORT = 22222; 
  public static String ADB_PATH = ""; 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    (); 
  } 
  public static void execAdb() { 
    // run the adb bridge 
    try { 
      Process p = ().exec( 
          ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:" 
              + (PHONE_PORT)); 
      Scanner sc = new Scanner(()); 
      // If there is some output, it failed to start adb 
      if (()) { 
        while (()) 
          (()); 
        ("Cannot start the Android debug bridge"); 
        return; 
      } 
      initializeConnection(); 
    } catch (Exception e) { 
      (()); 
    } 
  } 
  static Socket socket; 
  public static void initializeConnection() { 
    // Create socket connection 
    try { 
      socket = new Socket("localhost", PC_LOCAL_PORT); 
      ObjectOutputStream oos = new ObjectOutputStream( 
          ()); 
      ("lalala"); 
      (); 
      (); 
    } catch (UnknownHostException e) { 
      ("Socket connection problem (Unknown host)" 
          + ()); 
      (); 
    } catch (IOException e) { 
      ("Could not initialize I/O on socket"); 
      (); 
    } 
  } 
} 

The client code is as follows:

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class Client extends Activity { 
  public static final String TAG = "client"; 
  public static int PHONE_PORT = 22222; 
  Context mContext = null; 
  TextView textView = null; 
  ServerSocket server = null; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(); 
     = this; 
     = (TextView) (.textView1); 
    try { 
      server = new ServerSocket(PHONE_PORT); 
    } catch (IOException e) { 
      (); 
      return; 
    } 
    new RepackTestTask().execute(); 
  } 
  private class RepackTestTask extends AsyncTask { 
    @Override 
    protected Object doInBackground(Object... params) { 
      Socket client = null; 
      // initialize server socket 
      while (true) { 
        try { 
          // attempt to accept a connection 
          client = (); 
          (TAG, "Get a connection from " 
              + ().toString()); 
          ObjectInputStream ois = new ObjectInputStream( 
              ()); 
          String somewords = (String) (); 
          (TAG, "Get some words" + somewords); 
          (somewords); 
          (); 
        } catch (IOException e) { 
          (TAG, "" + e); 
        } catch (ClassNotFoundException e) { 
          // TODO Auto-generated catch block 
          (); 
        } 
      } 
    } 
    @Override 
    protected void onProgressUpdate(Object... values) { 
      (values); 
      (mContext, values[0].toString(), Toast.LENGTH_LONG) 
          .show(); 
    } 
  } 
} 

Thank you for reading, I hope it can help you. Thank you for your support for this site!