Android adb与PC端通信

第一步:在CMD中运行adb forward tcp:8000 tcp:9000,把PC端的8000端口映射到android端的9000端口上

第二步:打开Android设备adb

第三部:启动服务

PC端代码:

package com.socket.txt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class adbSocketCont extends Thread {
	Socket client;
	BufferedOutputStream out;
	BufferedInputStream in;

	public adbSocketCont() {
		try {
			client = new Socket("127.0.0.1", 8000);
			out = new BufferedOutputStream(client.getOutputStream());
			in = new BufferedInputStream(client.getInputStream());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
			 Scanner scanner = new Scanner(System.in);
			    String msg = scanner.next();
			    if (!client.isConnected()) {
					return;
				}
			    out.write(msg.getBytes());
			    out.flush();
			    
			while (true) {
				String readMsg = "";
				if (!client.isConnected()) {
					break;
				}
				readMsg = readMsgFromSocket(in);
				System.out.println(readMsg);
				if (readMsg.length() == 0) {
					break;
				}
				 readMsg = scanner.next();
				
				out.write((readMsg ).getBytes());
			    out.flush();
			}
			 in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String readMsgFromSocket(InputStream in) {

		String msg = "";
		byte[] tempbuffer = new byte[1024];
		try {
			int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
			msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}
}

Android端源码:

 class ServerThread extends Thread {

        boolean isLoop = true;

        public void setIsLoop(boolean isLoop) {
            this.isLoop = isLoop;
        }

        @Override
        public void run() {
            Log.d("anzi", "running");

            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(9000);

                while (isLoop) {
                    final Socket socket = serverSocket.accept();
                    new Thread(new SocketReadThread(socket)).start();
                }

            } catch (Exception e) {
                e.printStackTrace();

            } finally {
                Log.d("anzi", "destory");

                if (serverSocket != null) {
                    try {
                        serverSocket.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

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

    class SocketReadThread implements Runnable {

        private BufferedInputStream in;
        private BufferedOutputStream outputStream;
        Socket mSocket;

        public SocketReadThread(Socket mSocket) throws IOException {

            this.in = new BufferedInputStream(mSocket.getInputStream());
            outputStream = new BufferedOutputStream(mSocket.getOutputStream());
            this.mSocket = mSocket;
        }

        public void run() {
            try {
                String readMsg = "yyyy";
                String currCMD = "";
                while (true) {
                    try {
                        if (!mSocket.isConnected()) {
                            break;
                        }

                        //   读到后台发送的消息  然后去处理
                        currCMD = readMsgFromSocket(in);
                        //    处理读到的消息(主要是身份证信息),然后保存在sp中;
                        if (currCMD.length() == 0) {
                            break;
                        }
                        if (readMsg.equals("0002")) {
                        }
                        //  将要返回的数据发送给 pc
                        outputStream.write((currCMD + "flag").getBytes());
                        outputStream.flush();


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

    public String readMsgFromSocket(InputStream in) {
        int MAX_BUFFER_BYTES = 2048;
        String msg = "";
        byte[] tempbuffer = new byte[MAX_BUFFER_BYTES];
        try {
            int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
            msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值