admin管理员组文章数量:1122846
I am trying to use a data cable to allow my tablet and PC to communicate normally. I am currently using the adb mapping port method and then using socket for communication. If I do not add sleep when sending, my PC cannot receive messages normally. If I add sleep, my PC can receive messages normally. This is strange, no matter what I add, it is not as effective as a sleep
I tried adding the close and flush methods to the socket, but they had no effect
this is my code
clinet:
` static int loaclPort = 10025;
static int servicePort = 10025;
public static void main(String[] args) {
try {
List<String> devices = getConnectedDevices();
if (devices.isEmpty()) {
System.out.println("没有连接设备");
} else {
boolean checkport = isPortUsed("127.0.0.1",loaclPort);
if(!checkport){
try {
// 执行adb forward命令
Process process = Runtime.getRuntime().exec("adb forward tcp:" + loaclPort + " tcp:" + servicePort);
process.waitFor();
System.out.println("端口转发成功!");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
System.out.println("端口转发失败!");
}
}
System.out.println("连接的设备有:"+checkport);
for (String device : devices) {
System.out.println(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<String> getConnectedDevices() throws IOException {
Process process = Runtime.getRuntime().exec("adb devices");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
List<String> devices = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("device")) {
devices.add(line.split("\t")[0]);
}
}
reader.close();
return devices;
}
public static boolean isPortUsed(String ip, int port) throws IOException {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(ip, port));
socket.setSoLinger(true, 100);
OutputStream os = socket.getOutputStream();
String jsonString = "{\"name\":\"123\",\"id\":123456}";
byte[] data = jsonString.getBytes("UTF-8");
os.write(data);
os.write(data);
os.write(data);
os.write(data);
os.write(data);
try {
Thread.sleep(100);
} catch (Exception e) {
}
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String serverMessage = in.readLine();
System.out.println("服务器回复:" + serverMessage);
os.flush();
socket.close();
return true; // 端口被占用
} catch (IOException ex) {
throw ex;
// return false; // 端口未被占用
}
}
}`
service:
serverSocket = new ServerSocket(10025);
while(true) {
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.e("hehe", "run: " + inputLine+clientSocket.getPort());
System.out.println("Received: " + inputLine);
}
// OutputStream os = clientSocket.getOutputStream();
// String serverMessage = "Hello from server!";
// byte[] data = serverMessage.getBytes("UTF-8");
// os.write(data);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
clientSocket.close();
}
本文标签:
版权声明:本文标题:sockets - Android and PC use adb for communication, sleep needs to be added to execute normally - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311935a1934919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论