Connecting Android client to a Java servlet via wifi - java

I am following this tutorial to implement this: http://www.veereshr.com/Android/AndroidToServlet
I got my server up and running and I am able to open its localhost link in my browser. However, when I run the Android app on my Nexus 5, I get this error:
11-26 01:34:46.501 1092-1340/com.example.nav.wificlient D/Exception:java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: EHOSTUNREACH (No route to host)
11-26 01:35:29.171 1092-1557/com.example.nav.wificlient D/Exception: java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
Both my laptop (which is running the server program) and my phone are connected to the same network (called eduroam) on a college campus. I made sure that my firewall settings are not blocking incoming connections on my laptop.
I'm wondering if maybe the campus network settings are blocking this somehow...but I'm not sure. Any ideas on how I can narrow down what may be going wrong?

10.0.2.2 is a unique address directly interpreted by the emulator to reference the machine running the emulator. This is useful as "localhost" would simply reference the emulator itself.
As you are not running your android application on an emulator, but rather an actual device, you need to change your application so that, instead of connecting to 10.0.2.2, it connects to the local IP of the computer hosting the server.
See here for more information.

Related

Connecting Android Studio emulator to host machine SSH server

I am developing a SSH app on Android using Android Studio and Java. The SSH code I am using is from the library JSch.
Right now I am having a issue connecting to my host SSH server. I can connect to it from other virtual machines and computers but my emulator is not able to. I am trying to use the ip 10.0.2.2 to connect to localhost but I continue to get failed to connect to /`0.1.1 (port 22) from /:: (port 58062): connect failed: ECONNREFUSED (Connection refused).
I know the Windows host SSH server is working. If I use ADB to get into the emulator I can ping 10.0.2.2 without issue. I feel like it is some sort of limitation inside the virtual network that the emulator is setup in however I am not sure. Does anyone know of any ways to make my Android SSH connect to my host windows SSH server?

Connect Android App to IoT device using TCP through Wi-Fi

I have an Android App and an IoT device where the connectivity and the communication will be through Wi-Fi. The IoT device will provide an hotspot (without Internet) so that we can connect our Mobile Wi-Fi with that device hotspot. After connecting the Mobile with Wi-Fi, when we open the app, the app will try to find the device using TCP socket connection to connect and communicate to the IoT device.
The App works fine and communicates with the device successfully. But the problem here is, the connection and communication is not happening if the Mobile Data is turned ON. The app works only with the Mobile Data turned OFF.
I guess the App is searching for the IoT device through Mobile Data since the Mobile data has an active internet connection and the Wi-Fi does not. I want the App to look for the IoT device only through Wi-Fi even if Mobile Data is ON.
I have a similar App in React Native which has the same functionality and had the same problem. I fixed it with using react-native-tcp-socket package which has an option to make the app search through a particular interface (which has the options wifi, ethernet and cellular).
Both the Apps have an hardcoded IP Address for connecting to the IoT device. This works only when Mobile Data is turned Off. In the React Native App, I fixed it using that package mentioned previously with the Dynamic IP address by getting it from the WifiManager. When I tried the same with the Android App it did not work.
I looked into the package's native code and tried to replicate the same, but getting the below error:
java.net.ConnectException: failed to connect to /192.168.1.2 (port 6378) from /:: (port 39495): connect failed: ECONNREFUSED (Connection refused)
Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
Any Suggestions?
Is any other way of implementation possible to achieve the output?
The solution is to bind the socket to the Wi-Fi network. We must create a listener and get the Network from the listener's result.
So, Whenever we open or reopen a socket we must bind that socket to Wi-Fi network. This is what I was missing. After adding this, it worked fine.
network.bindSocket(socket);
And, another update is, in my case, The dynamic IP address didn't work but the pre-defined hardcoded IP address works and I am unable to find the reason, unlike my previous case which was vice-versa.

failed to connect to port80 after 100000ms in android

I am hitting otp api's from Emulator its work but if I am using real device then getting failed to http connecting error found.
2020-11-06 13:33:23.603 24840-24840/a3.amp35.in E/Genrate: failed to connect to /192.168.132.101 (port 80) from /26.81.35.26 (port 39596) after 100000ms
need help
Your device is outside of your local network. Api is on 192.168.132.101 while device is out given net 26.81.35.26
Either expose your API to the outside world (eg. port forward it or host in on globally accessible device) or connect your device to your local network (eg, use the same WIFI, or via VPN)
Also firewall settings on the 192.168.132.101 might block the incoming connections from different subnets.
And probably many more possible causes...

Failed to connect to local IP from Android application

The client failed to establish a connection to the local address localhost:3000.
The error encountered was:
dial tcp [::1]:3000: connectex: No connection could be made because the target machine actively refused it.
How can I solve it?
Change your localhost:3000 address to local IP address for example 192.168.1.15:3000 etc.
Your server and client must be in same network
Emulator
On an emulator the localhost is accessible using the IP 10.0.2.2 .
So localhost:3000 will be 10.0.2.2:3000 in your emulator.
Physical device
First of all you must be on the same network to be able to connect.
Get the PC ip using ipconfig (windows) and ip a or ifconfig (linux)
Connect to the PC localhost using it's ip. i.g 192.168.2.121:3000
(your firewall must be OK with this)
If you are trying to connect using code, same things are needed for this

How to connect Android device to a Java program via sockets?

I need to connect an android device to my java program via socket connection. The device is working as server. The problem is that while trying out socket connection I need to give IP address and port number of server.
Is there something like a static IP address for an Android device to connect? If not, is there any alternative way to establish a socket connection between the device and my program?
As the device has to have a connection to the same network as the computer, it has to have an IP address configured. So you have to use that one.
The used port is defined by the server application running on your device.
If you have the problem that anyhow a normal network connection is not possible, but you have an ADB connection, you can forward local pots to the device and let your server listen:
http://developer.android.com/tools/help/adb.html#forwardports
Over a mobile connection (GPRS, EDGE, UMTS, HSDPA) there will be no practicable way to establish a connection from somewhere to your device (instead you have to do it the other way, while canceling the whole client-server directive), in cause of the used IP sharing. In that case you have a problem of which I'm not aware if it's possible in general, not to mention how you could achieve this.
Otherwise you simply have to configure a static IP for your device when connected to your local network, or you have to evaluate the actual IP of your device every most of the time, while using it with this configuration:
Resolve it by the device itself
Resolve InetAdress from DhcpInfo
Documentation for DhcpInfo
Resolve by using ADB command

Categories

Resources