第一步:在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;
}