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 : 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 :

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


    } 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.


  • If you already solved your problem, post the solution as an answer, so that other visitors can see the problem is solved. Putting the answer in an edit to your question still makes the question show as unanswered. – Dan Hulme Apr 5 '16 at 21:57
  • When I adb shell onto my device I get /system/bin/sh: redir: not found when I try to execute the redir add command. – Csaba Toth Jun 10 '16 at 6:39
  • As I recall, redir need to be run on the android device via adb but as I saw that it wasn't the thing that I needed, I abandoned the idea... – geauser Jun 10 '16 at 10:24

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.
| improve this answer | |
  • But this would mean that you'd transfer data through wifi. I'd like to transfer UDP streaming data through USB tether. – Csaba Toth Jun 10 '16 at 6:38
  • 1
    When you enable USB tethering your phone and the computer forms a LAN so no needs for Wifi as long as you have the USB cable plugged. – geauser Jun 10 '16 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.