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

本文标签: