Search code examples
androidhttp-redirectnetwork-programmingudpadb

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


I have an real Android device connected to a computer via USB. I managed to activate TCP port forwarding with adb forward tcp:<port> tcp:<port> and successfully communicate with sockets but I don't know how to do the same for UDP.

EDIT v1 : I launched my application on the emulator and typed telnet localhost 5554 and redir add udp:<port>:<port> and it worked, my function executeCMD is functionnal because when I try ls it's working.

Some websites said to use redir like this redir add udp:<port>:<port> but when I do so I obtain an error :

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.

Then I thought this command should be launch on the Android device so I created this method :

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

And when I called it like that :

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

I get no output and the UDP server on the Android app can't communicate with the UDP client.

So I'm a bit lost... I'll continue searching but If you can help me; go ahead.

Thanks.


Solution

  • So finally, I did not find out how to enable UDP forwarding for USB communication but I did somethings else that works pretty well, but you have to enable USB tethering (from the computer ping the phone to see if it's properly enabled):

    1. Create a TCP server on the phone and a TCP client on the desktop.
    2. Execute adb tcp:<port> tcp:<port> on the desktop so it can communicate with the phone on localhost via TCP sockets.
    3. Make the TCP server send the IP address (on the USB tethering network) of your phone to the TCP client.
    4. Then you can close the TCP connection and with the address you can initiate an UDP connection with socket as you would in a normal situation.