adb.exe forward tcp:5902 tcp:5902
前面本地5901 转发到 后面设备为5902
查看转发
adb forward --list
删除所有转发
adb forward --remove-all
public static final String TAG = "Communicate";
private static boolean isEnable;
private final WebConfig webConfig;
private final ExecutorService threadPool;
private ServerSocket socket;
public void startServerAsync() {
new Thread(new Runnable() {
@Override
public void run() {
doProcSync();
}
}).start();
}
private void doProcSync() {
try {
InetSocketAddress socketAddress = new InetSocketAddress(webConfig.getPort());
socket = new ServerSocket();
socket.setReuseAddress(true);
socket.bind(socketAddress);
AppServiceApplication.mHandler.post(new Runnable() {
@Override
public void run() {
AppUtils.setTextView(MainActivity.getTextViewInfo(), "start server success");
}
});
isEnable = true;
LogS.e(TAG, "remotePeer...............进入循环等待客户端连接 " + isEnable);
while (isEnable) {
final Socket remotePeer = socket.accept();
LogS.d(TAG, "有一个新连接");
threadPool.submit(new Runnable() {
@Override
public void run() {
LogS.d(TAG, "接收处理线程 isEnable:" + isEnable);
receiveData(remotePeer);
}
});
}
LogS.d(TAG, "退出等待客户端循环");
} catch (IOException e) {
e.printStackTrace();
}
}
private void receiveData(final Socket remotePeer) {
try {
InputStream inputStream = remotePeer.getInputStream();
byte buffer[] = new byte[2048];
int len=0;
while ((len = inputStream.read(buffer)) != -1) {
String message = new String(buffer).substring(0, len);
LogS.d(TAG, "接收报文:" + message);
sendData(remotePeer, message);
}
LogS.d(TAG, "客户端断开连接");
} catch (IOException e) {
e.printStackTrace();
}
}