国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android手機(jī)端與PC端使用adb forword通信

2019-10-23 18:30:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PC端與Android手機(jī)端使用adb forword通信

服務(wù)器端代碼如下:

import java.io.IOException; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; 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 = "adb.exe";   /**    * @param args    */   public static void main(String[] args) {     // TODO Auto-generated method stub     YingyonghuiHubServer.execAdb();   }   public static void execAdb() {     // run the adb bridge     try {       Process p = Runtime.getRuntime().exec(           ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:"               + String.valueOf(PHONE_PORT));       Scanner sc = new Scanner(p.getErrorStream());       // If there is some output, it failed to start adb       if (sc.hasNext()) {         while (sc.hasNext())           System.out.println(sc.next());         System.err.println("Cannot start the Android debug bridge");         return;       }       initializeConnection();     } catch (Exception e) {       System.err.println(e.toString());     }   }   static Socket socket;   public static void initializeConnection() {     // Create socket connection     try {       socket = new Socket("localhost", PC_LOCAL_PORT);       ObjectOutputStream oos = new ObjectOutputStream(           socket.getOutputStream());       oos.writeObject("lalala");       oos.close();       socket.close();     } catch (UnknownHostException e) {       System.err.println("Socket connection problem (Unknown host)"           + e.getStackTrace());       e.printStackTrace();     } catch (IOException e) {       System.err.println("Could not initialize I/O on socket");       e.printStackTrace();     }   } } 

 客戶端代碼如下:

import java.io.IOException; import java.io.ObjectInputStream; import java.net.ServerSocket; import java.net.Socket; import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; 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) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     this.mContext = this;     this.textView = (TextView) this.findViewById(R.id.textView1);     try {       server = new ServerSocket(PHONE_PORT);     } catch (IOException e) {       e.printStackTrace();       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 = server.accept();           Log.d(TAG, "Get a connection from "               + client.getRemoteSocketAddress().toString());           ObjectInputStream ois = new ObjectInputStream(               client.getInputStream());           String somewords = (String) ois.readObject();           Log.d(TAG, "Get some words" + somewords);           this.publishProgress(somewords);           client.close();         } catch (IOException e) {           Log.e(TAG, "" + e);         } catch (ClassNotFoundException e) {           // TODO Auto-generated catch block           e.printStackTrace();         }       }     }     @Override     protected void onProgressUpdate(Object... values) {       super.onProgressUpdate(values);       Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG)           .show();     }   } } 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漳平市| 吉木萨尔县| 临泉县| 关岭| 通州市| 紫云| 镇坪县| 德钦县| 嘉兴市| 南华县| 张家口市| 大竹县| 石楼县| 福鼎市| 建平县| 莎车县| 库伦旗| 纳雍县| 宣汉县| 南阳市| 太仆寺旗| 县级市| 香格里拉县| 太原市| 含山县| 上思县| 大同市| 衡阳市| 宁南县| 阳原县| 宁武县| 福安市| 合肥市| 潞城市| 马尔康县| 台安县| 宣城市| 铜川市| 灵石县| 上饶市| 盖州市|