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


Question

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 :



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.


Answer

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.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes