How to forward a localhost port? - android

How can I simply forward a port from the Android emulator (127.0.0.1:5555) to my LAN (192.168.1.102:5555)?
I don't think I can change the IP address of the emulator so the only way I see I can do this is by forwarding that port to the network.
I just want to access my powerful PC emulator from my laptop through adb.

If you want to forward the port, you have to change its number. You should not be allowed to listen to 5555 from 2 applications (you application and the forwarder).
I don't know about android emulator, but you can probably configure it (and your firewall) to accept incoming connections from your LAN (and not only from localhost).
If you still want to perform a port redirect you can use netcat (installed on most baseline unix/linux systems, you can find ports for Windows from google)
e.g. redirect incoming connections on port 8080 to local service on port 5555
nc -L 127.0.0.1:5555 -p 8080 -vvv

Related

Who should be a socket server and who should be a client in the case of `adb forward` and `usbmuxd`

I'm trying to create a server-client connection between a desktop computer and many mobile devices via USB.
To me, it makes sense to create one socket server on the desktop side to listen to a specific port for connections, while letting multiple mobile devices connect to this port. However, the info I found on usbmuxd says the mobile device is supposed to be the socket server instead:
In practice it is possible to **create a socket server in a free port from an iOS** App and to connect to it from the host machine sending the following command via the /var/run/usbmuxd Unix socket (File Descriptor):
<?xml version=”1 .0" encoding=”UTF-8"?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList1.0.dtd”>
<plist version=”1.0">
<dict>
<key>ClientVersionString</key>
<string>usbmuxd-423.206.4</string>
<key>DeviceID</key>
<integer>4</integer>
<key>MessageType</key>
<string>Connect</string>
<key>PortNumber</key>
<integer>My port</integer> <--- The port number (with endian transform htonl)
<key>ProgName</key>
<string>usbmuxd</string>
</dict>
</plist>
With Android, it seems that adb forward tcp:<port1> tcp:<port2> does similar by forwarding desktop-host port but the host is essentially a socket client. To my understanding, then the host must know all the ports on arbitrary devices and the devices must make sure those ports are unique, making it hard to automate the handshaking process with multiple devices.
Is this by design? Is it impossible to invert the server-client relationship between desktop and mobile this way?
Or am I wrong about the whole thing?
Both adb forward and usbmuxd (usually via iproxy) forward a TCP port on your PC to your mobile device. That means your PC is a client, and your mobile device acts as a TCP server.
Normally you start a well-known application on your mobile device, which always exposes the same port (say, 2000). You can connect to your app running on your device by doing adb forward tcp:3000 tcp:2000 or iproxy 3000 2000. You can then connect to port 2000 on your device by connecting to port 3000 on your PC.
If you have multiple devices, you can use different ports on your PC, and have them forward to the same port on the device. For example, you could use adb forward -s device_1 tcp:3001 tcp:2000 and adb forward -s tdevice_2 tcp:3002 tcp:2000. Connecting to port 3001 on your PC would end up connecting to port 2000 on device 1, and port 3002 on your PC would map to port 2000 on device 2.

adb port forwarding and socket connection

Situation-
I have two android devices with a server app installed in it. App listens in a particular port. I have forwarded one local port to device-1's server port and connected to it. I am able to communicate with server app.
I tried forwarding same local port and connected to device-2's server port. And i am able to communicate with device-2 and device-1.
With the two socket connections i made, I can separately communicate with two devices using same local port.
Question-
I wonder how this works. same local port is forwarded to two different paths.
My understanding was in the low level everything goes through the port which we mention at time of socket connection. Which is wrong? Port is used only at time of connection and there after some other connection handle is used for communication ?
<Run device 1 server>
adb -s serial1 forward tcp:8088 tcp:8088
connect to device-1 # I used python
<Run device 2 server>
adb -s serial2 forward tcp:8088 tcp:8088
connect to device-2 # I used python

Android adb forward and own bridge to reach remote server

i have a question about the adb forwarder stuff
My situation:
Ressources: 1 Windows PC where i write my code, 1 Android device (Android 2.2) connected via USB to the Windows PC, 1 Linux Server running inside a virtual Machine (VMWare) on the Windows PC
Task: the android device has to connect the virtual Machine via TCP (Port 65001)
restrictions: I am not allowed to use wlan for that (because of some issues that i can't influence). I am not able to do usb (reverse) tethering because my devices does not have the usb tethering option in the android menu (disabled by the manufacturer).
I am able to telnet the port to the virtual machine (telnet 192.168.1.2 65001) and get my data. So there are no network problems between local and virtual machine. The local ip address is 192.168.1.1.
Now i need a bridge from the incoming port 65000 to the listening Port on the virtual machine (65001). I have used the following command adb forward tcp:65000 tcp:65000 to get a tcp connection over usb from my android device to my localhost (and vice versa). As I know you can't forward with adb to a remote machine.
Now the problem is that i can't bridge from 127.0.0.1:65000 to any other port to get the full communication path. If I try to create a listening connection on 127.0.0.1:6500 i get an error with the message that the port is already bind (because of adb forward tcp:65000 tcp:65000). If the forward blocks all other connection on this port, how can i get the incoming data from the android device?
I've tried to tunnel via putty (localhost:65000 (win) -> server:65001) and I've written a little program that creates a listening server (socket) on 127.0.0.1:65000 and sends the information to 192.168.1.2:65001 via 192.168.1.1:65001.
Has anybody an idea how to solve my situation? If so please explain. Maybe this helps others too to handle the adb forwarding function.
hint: The Ports and addresses are only examples. The only fix port number is the listening port on the virtual machine

TCP port number - android phone usb connected

How do I get the TCP port number for a usb connected android phone. I can do this
netstat -an |find /i "listening"
but then it lists all the listening port, how do I know to which port my phone is listening to. anyone on this pls?
Perhaps I am misunderstanding what you are trying to do here but if your phone is connected via USB then it does not have a TCP port open it is addressed via the USB device. Take a look at the AdbTest example program provided with the API.
You can get Adb to connect over wifi (see bootom of this page) but I believe that in most cases this requires a rooted device.

Use adb forward instead of redir

I have a client and a server app on Android (that uses ZeroC - Ice platform for communication). Server listens on TCP port 10000 and client uses TCP port 5501 (arbitrary port numbers I have chosen).
When I telnet to server and run redir add tcp:5501 tcp:10000 it works smooth.
However, due to my current system setup (using androidx86 binary through a virtual machine), I can no longer telnet to the server. So I was hoping I could use adb forward instead. However, whatever port combination I tried, I couldn't get it to work.
I would really appreciate if someone could shed some light on these:
- Is it possible to telnet to a device replicate or device? I've read on another post on stackoverflow that telnet interface is only available for emulator
- What is the difference between redir and adb forward? How should I use adb forwarding to achieve redir tcp:A:B on server?
Thanks
I've finally managed to get it to work. Several comments
USB connected devices cannot be reached via ethernet. They can be reached via wireless network (assuming they are connected to the same network with the other party)
VirtualBox has its own NAT forwarding. Without port forwarding, under default networking configuration (which is NAT), the virtual device is inaccesible.
My working setup is as follows
A USB-connected device connects to the PC via wireless (Find out your PC's local IP address, which is often something like 192.168.1.x)
I have enabled VirtualBox forwarding for NAT. My option is TCP 10000:10000 (where 10000 is an arbitrary port number. Note that it should be higher than 1024) So when my USB-connected device connects to the PC at port 10000, it is forwarded to the virtual device via VirtualBox's forwarding.
Virtual device listens to TCP port 10000. (I didn't specify host address to make life easier)
If you get a 'NetworkOnMainThread' exception, use
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Hope this helps someone

Categories

Resources