adb port forwarding and socket connection - android

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

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.

No Consistent Way to Connect ADB over TCP

I am debugging an Android app that usually crashes when I plug into an Arduino it is supposed to be communicating with. Consequently, I need to get a wireless connection to the device's logcat, which is what brought me to ADB over TPC as seen on the Android developers webpage.
The recommended process is:
C:\Users\User> cd AppData\Local\Android\sdk\platform-tools
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb kill-server
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb start-server
* daemon not running; starting now at tcp:5037
* daemon started successfully
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb tcpip 5555
restarting in TCP mode port: 5555
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb connect 10.20.5.160:5555
However, the typical response is:
unable to connect to 10.20.5.160:5555: cannot connect to 10.20.5.160:5555: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)
I could not consistently get a connection between a PC and mobile device. Over the course of three weeks using several networks and devices, I could only connect two times out of an agonizing many attempts.
I have dug deep into the posted questions on stackoverflow and tried literally every recommended alteration to getting an adb connection, from using an Android Studio plugin to forwarding ports to trying several other ports to toggling my device's internet. I still could not get a connection more than a fraction of a percent of the time.
When I look at the network state of my device after adb tpip <port>, I find no evidence of the device trying to look for a connection:
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb shell
j3ltevzw:/ $ netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ::ffff:10.20.5.160:4214 ::ffff:173.194.196.188: ESTABLISHED
udp 4288 0 adb shell ip -f inet addr show wlan0:68 10.20.5.1:67 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
... ... ... ... ... ...
... ... ... ... ... ...
Also, when I try to ping my device, I usually get:
C:\Users\Benjamin\AppData\Local\Android\sdk\platform-tools> ping 10.20.5.160
Pinging 10.20.5.160 with 32 bytes of data:
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Ping statistics for 10.20.5.160:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
What I am looking for is a process or file that can be run and consistently open a tcp adb connection, or at least explain what about my setup is invalid. Ideas?
Update 1:
I have started a bounty on this question. I feel like the problem with the ADB software used to make connection for debugging is two fold:
adb tcpip <port> does not always invoke the mobile device to listen on the specified port
adb connect <ip>:<port>, I am assuming, is not given permission by the network to connect; I am not a networking expert, but the fact that pinging a device returns "destination host unreachable" seems a little off
I will award the first person to find a successful solution. Seeing that having an ADB USB connection gives a user access to both the mobile device's and host's terminal, there really is no excuse for a connection not to be able to be made between the two.
Update 2:
I have recently had more success in connecting to the ADB as well as receiving successful pings at my school network. Note at this network connects are insecure and the host and mobile devices where on separate subnetworks. Any idea why these conditions would work over my secure network at home that uses only one subnet?
Try starting from scratch and setup ADB-over-tcp following steps below:
Turn off USB debugging on your device and turn it on again (just to reset).
Kill the ADB server on your PC using adb kill-server
Connect your device to the PC using the USB cable and type adb devices. This will automatically restart the ABD server again.
Enter adb tcpip 5555 on your PC terminal. This will switch the adbd daemon on your device to tcp-ip mode.
Disconnect your device from USB. Now connect your device to the same wireless network as your PC, either the same wi-fi or use your phone as a hotspot.
Determine your phone's ip. You can do this in the wi-fi settings on your phone. If you are using your phone as hotspot, then generally your ip should be 192.168.43.1 (not required but most of the time it's the same).
Connect to ADB via tcp using adb connect <IP>
This should hopefully connect your device to your pc via tcp-ip.
I found a solution that works for me, but I feel it shouldn't necessary and it may not applicable for everybody else with these issues, so its still not quite what I was aiming for.
Anyways, after seeing riyaz-ali suggest what an IP address I should be for using a mobile hotstop through a phone, I realized any network complications would be solved if I was the network, so I connected my mobile device to the mobile hotspot on my laptop.
Instant ping responses and immediate ADB connection with the popularly suggested commands.
Just found myself in the same situation. Had no problem yesterday but today couldn't connect.
Solution was simple, I realized that today I'm connected to VPN which obviously makes me belonging to a different network.
So, disconnecting and connection to the same Wi-Fi solved the trouble.

Android: Is it possible to connect to remote adb programatically?

I set-up remote debugging with these commands:
adb tcpip 5555
When I connect to the adb service with adb connect <ip>:5555 I am able to read the logcat from my computer.
However, I want to write a small android app for my tablet (device A) which analyzes the logcat of my smartphone (device B). How do I connect to the remote adb and read the logs programatically?
I expect that I have to create a TCP socket to the server. But then I don't know how to continue.
EDIT
As I found out with Wireshark, it actually is possible. The connection seems to be a basic TCP socket connection with a command response handling. ADB sends a 4 letter command with the command shell:logcat -v long.
So, is there a documentation about how this socket connection works?

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

How to forward a localhost port?

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

Categories

Resources