How to let two apps running on different computer inside android emulator connect

One of my android application listens to incoming connections from a desktop application which then sends data to it. The desktop application client only runs on a macOS or a windows. The android application during my software development or debugging runs inside emulator on my development machine which is Ubuntu. So how do I test my android application and the client without changing my development machine OS/Ubuntu. basically you need SSH on both machines to make it work. With SSJ we can open a tunnel between the two computers.

ssh -NL 4444:localhost:8000 ahmed@myzbook.comCode language: CSS (css)

I run the above command on my macOS where the desktop application is running . This will create a tunnel between macOS and Ubuntu dev machine. Now my desktop application client connects to 127.0.0.1 port 4444 and this connection goes and hits 127.0.0.1:8000 on Ubuntu machine. But this is not yet enough. We need to forward port 127.0.0.1:8000 to emulator-IP:port-android-app-is-listening-on/ for that.

adb forward tcp:8000 tcp:4000Code language: CSS (css)

Assuming my android app in emulator on Ubuntu is listing on port 4000. the above command will redirect 127.0.0.1:8000 to emulator:4000. You may also use socat tool instead of adb

SSH login to remote machines without repeatedly giving password

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"Code language: JavaScript (javascript)

On your local machine (the one you’ll be SSH-ing from), open a terminal and run the above command. Optionally, you can set a passphrase, but if you want it fully passwordless, leave it blank and press Enter.

Now, copy your SSH key to the remote machine (the computer you’re SSH-ing into). This step allows the remote computer to recognize your key and authenticate you automatically.

Run this command (replace username and remote_host with your remote machine’s username and IP address or hostname):

ssh-copy-id username@remote_hostCode language: CSS (css)

This will copy your public key (~/.ssh/id_rsa.pub) to the remote machine’s ~/.ssh/authorized_keys file

If ssh-copy-id is not available on your system, you can manually copy the key:

cat ~/.ssh/id_rsa.pub | ssh username@remote_host 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'Code language: JavaScript (javascript)