如何为 PC 和 Android 应用程序之间的 USB 通信激活 UDP 转发

How can I activate UDP forwarding for USB communication between a PC and an Android app

我有一个 real Android 设备通过 USB 连接到计算机。我设法用 adb forward tcp:<port> tcp:<port> 激活 TCP 端口转发并成功地与套接字通信,但我不知道如何为 UDP[=43= 做同样的事情].

EDIT v1 :我在模拟器上启动了我的应用程序并输入了 telnet localhost 5554redir add udp:<port>:<port> 然后它 有效,我的函数 executeCMD 是函数式的,因为当我尝试 ls 它正在工作。

一些网站说要像这样使用 redir redir add udp:<port>:<port> 但是当我这样做时我得到一个错误:

usage:
    redir --lport=<n> --cport=<n> [options]
    redir --inetd --cport=<n>

    Options are:-
        --lport=<n>     port to listen on
        --laddr=IP      address of interface to listen on
        --cport=<n>     port to connect to
        --caddr=<host>      remote host to connect to
        --inetd     run from inetd
        --debug     output debugging info
        --timeout=<n>   set timeout to n seconds
        --syslog    log messages to syslog
        --name=<str>    tag syslog messages with 'str'
        --connect=<str> CONNECT string passed to proxy server
        --bind_addr=IP  bind() outgoing IP to given addr
        --ftp=<type>        redirect ftp connections
            where type is either port, pasv, both
        --transproxy    run in linux's transparent proxy mode
        --bufsize=<octets>  size of the buffer
        --max_bandwidth=<bit-per-sec>   limit the bandwidth
        --random_wait=<millisec>    wait before each packet
        --wait_in_out=<flag>    1 wait for in, 2 out, 3 in&out

    Version 2.2.1.

然后我认为这个命令应该在 Android 设备上启动所以我创建了这个方法:

 public String executeCMD(String cmd){
    StringBuffer output = new StringBuffer();
    try{
        Process process = Runtime.getRuntime().exec(cmd);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        while ((read = reader.read(buffer)) > 0) output.append(buffer, 0, read);

        reader.close();
        process.waitFor();

    } catch(IOException e){ e.printStackTrace();
    } catch(InterruptedException e){ e.printStackTrace(); }

    return output.toString();
}

当我这样称呼它时:

executeCMD("redir add udp:" + UDP_PORT + ":" + UDP_PORT)

我没有得到任何输出,Android 应用程序上的 UDP 服务器无法与 UDP 客户端通信。

所以我有点迷路...我会继续搜索,但如果你能帮助我;继续。

谢谢。

所以最后,我没有找到如何为 USB 通信启用 UDP 转发,但我做了其他一些工作得很好的东西,但是 您必须启用 USB 网络共享 (从计算机 ping phone 以查看它是否已正确启用):

  1. 在 phone 上创建一个 TCP 服务器,在桌面上创建一个 TCP 客户端。
  2. 在桌面上执行 adb tcp:<port> tcp:<port>,以便它可以通过 TCP 套接字与 localhost 上的 phone 通信。
  3. 使 TCP 服务器将您 phone 的 IP 地址 (在 USB 网络共享网络上) 发送到 TCP 客户端。
  4. 然后您可以关闭TCP连接,并使用该地址可以像在正常情况下一样使用套接字发起UDP连接。