PC端与android手机端使用adb forword通信


服务器端代码如下:
  1. view plaincopy to clipboardprint?
  2. import java.io.IOException;
  3. import java.io.ObjectOutputStream;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6. import java.util.Scanner;
  7. public class Server {
  8. public static final String TAG = "server";
  9. public static int PC_LOCAL_PORT = 22222;
  10. public static int PHONE_PORT = 22222;
  11. public static String ADB_PATH = "adb.exe";
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. // TODO Auto-generated method stub
  17. YingyonghuiHubServer.execAdb();
  18. }
  19. public static void execAdb() {
  20. // run the adb bridge
  21. try {
  22. Process p = Runtime.getRuntime().exec(
  23. ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:"
  24. + String.valueOf(PHONE_PORT));
  25. Scanner sc = new Scanner(p.getErrorStream());
  26. // If there is some output, it failed to start adb
  27. if (sc.hasNext()) {
  28. while (sc.hasNext())
  29. System.out.println(sc.next());
  30. System.err.println("Cannot start the Android debug bridge");
  31. return;
  32. }
  33. initializeConnection();
  34. } catch (Exception e) {
  35. System.err.println(e.toString());
  36. }
  37. }
  38. static Socket socket;
  39. public static void initializeConnection() {
  40. // Create socket connection
  41. try {
  42. socket = new Socket("localhost", PC_LOCAL_PORT);
  43. ObjectOutputStream oos = new ObjectOutputStream(
  44. socket.getOutputStream());
  45. oos.writeObject("lalala");
  46. oos.close();
  47. socket.close();
  48. } catch (UnknownHostException e) {
  49. System.err.println("Socket connection problem (Unknown host)"
  50. + e.getStackTrace());
  51. e.printStackTrace();
  52. } catch (IOException e) {
  53. System.err.println("Could not initialize I/O on socket");
  54. e.printStackTrace();
  55. }
  56. }
  57. }
客户端代码如下:
  1. view plaincopy to clipboardprint?
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. public class Client extends Activity {
  14. public static final String TAG = "client";
  15. public static int PHONE_PORT = 22222;
  16. Context mContext = null;
  17. TextView textView = null;
  18. ServerSocket server = null;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. this.mContext = this;
  24. this.textView = (TextView) this.findViewById(R.id.textView1);
  25. try {
  26. server = new ServerSocket(PHONE_PORT);
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. return;
  30. }
  31. new RepackTestTask().execute();
  32. }
  33. private class RepackTestTask extends AsyncTask {
  34. @Override
  35. protected Object doInBackground(Object... params) {
  36. Socket client = null;
  37. // initialize server socket
  38. while (true) {
  39. try {
  40. // attempt to accept a connection
  41. client = server.accept();
  42. Log.d(TAG, "Get a connection from "
  43. + client.getRemoteSocketAddress().toString());
  44. ObjectInputStream ois = new ObjectInputStream(
  45. client.getInputStream());
  46. String somewords = (String) ois.readObject();
  47. Log.d(TAG, "Get some words" + somewords);
  48. this.publishProgress(somewords);
  49. client.close();
  50. } catch (IOException e) {
  51. Log.e(TAG, "" + e);
  52. } catch (ClassNotFoundException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. @Override
  59. protected void onProgressUpdate(Object... values) {
  60. super.onProgressUpdate(values);
  61. Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG)
  62. .show();
  63. }
  64. }
  65. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值