我有一个真正的安卓设备通过USB连接到电脑。我设法用adb forward tcp:<port> tcp:<port>激活tcp端口转发,并成功地与套接字通信,但我不知道如何对udp执行同样的操作。
编辑v1:我在模拟器上启动了我的应用程序,输入了telnet localhost 5554redir add udp:<port>:<port>,然后
我的函数executecmd是functional,因为当我尝试ls
它起作用了。
有些网站说像这样使用redir,但是当我这样做时,我得到一个错误:

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客户端通信。
所以我有点迷茫…我会继续寻找,但如果你能帮助我,请继续。
谢谢。

最佳答案

所以最后,我没有找到如何启用udp转发来进行usb通信,但是我做了一些其他的事情,效果很好,但是
您必须启用USB连接(从计算机ping电话以查看是否正确启用):
在电话上创建TCP服务器,在桌面上创建TCP客户端。
在桌面上执行adb tcp:<port> tcp:<port>,以便它可以通过tcp套接字与localhost上的电话通信。
使TCP服务器将电话的IP地址(在USB系链网络上)发送到TCP客户端。
然后,您可以关闭TCP连接,使用该地址,您可以像在正常情况下那样使用套接字启动UDP连接。

关于android - 如何激活PC和Android应用之间的USB通信的UDP转发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36430370/

10-12 02:44
查看更多