Accessing android app through TCP/IP by PC connected to android hotspot - java

I'm creating an Android app which is Mod-Bus TCP slave simulator and I have a problem when trying to connect from pc to it while sharing a connection via wifi hotspot.
I tried using gateway IP but it does not work. When phone and pc are connected to the same wifi (other than themselves) everything is working fine but I need to make it also work with hotspot running and pc connected to it. Is it possible? How?
App is using modbus4j lib.

Actually, I don't think so that Modbus TCP works on mobile hotspot too because recently when i was working on MB TCP I found this. I tried to connect with the ip and gateway of my mobile hotspot but it didn't work this could happen because our mobile hotspot works on DHCP so the gateway is not static. But Modbus TCP works very fine on Wifi router ip and gateway.

Your problem has nothing to do with java or android or Modbus, but it is a problem of understanding how routing works on the internet and on internal networks (your WiFi network is an internal network)
The problem is that the ports waiting for connections on the internal network, like your android device, are not accessible from the internet.
To make them accessible, one or more NAT rules must be defined in the WiFi router. But this will only work if your internet provider assigns you a public IP, which is less and less frequent due to the shortage of public IPs.

Related

Java. How to get a list of exactly my servers in the local network?

Using tcp sockets, I wrote a server, launched it on a computer. Wrote a client for android. Being in the same local network, I successfully connect from my smartphone knowing the local ip address of the computer and the server port.
The problem is that I want to run this server on multiple computers on the local network, and their local addresses can change.
Need to somehow scan and select an available server in the application.
How is it implemented? What is the special protocol for scanning? Or just try to connect by sorting through all possible addresses and ports?
Is UDP broadcast a bad solution?
Sorry for google-translator!
UPD: I am trying to make a remote PC application. This will not work on the global internet. only local network. Thanks for the answer
I solve it by use DatagramSocket to UDP broadcast to 192.168.1.255.

How to find IP address of OTHER WiFi Devices on network programmatically in android

I have an ESP8266 that connects to my WiFi network after being configured (via WiFi Manager library) and runs a HTTP web server that displays certain sensor data. It connects to my specific network with a 192.168.0.XX IP and it works well.
My problem comes with the android app, and how to actually programmatically retrieve the IP that the webserver is running on? My routers DHCP only reserves IP's for a week - so hardcoding the IP into the app is not a good option, and having less tech-savvy people find the IP and configure it themselves is also not what I'm looking to do.
I also can't hardcode a static IP as friends & family will be using this with different networks and routers (so 192.168.0.X would not work on a router with an IP of 10.0.0.1 for example).
So, how do I go about programmatically getting the IP of my ESP8266 that is connected to my local network?
There is a solution that maybe isn't very optimal and professional, but will do for hobby project. You can make your android app scan your local network using http GET on all addresses - e.g. trying to GET something like this: http://192.168.0.X/sensor_status. You can configure your esp8266 device to respond with code 200 (OK) and save this address in your android app's persistent storage. Next time you can simply check if there is ip saved in persistent storage and try to connect to it. If esp8266 is not available under remembered ip, you can re-scan your network.

Find an application running on a network

I'm new to networking and was wondering a way to find out from an android phone java app whether there is an application running on any computer on a wifi network it's connected to and a point in the right direction on what to google or a tutorial?
edit: The application I'd be finding would have been made to be found by the android app
Thanks,
Harold
You should use a UDP broadcast. Basically, a server announces its presence periodically on a local network with broadcast packets. A client then picks up on these packets, finds the source and connects to it. A networking library like KryoNet (available for both J2SE and Android Java) makes it much easier.
InetAddress address = client.discoverHost(54777, 5000);
System.out.println(address);
usually application discover their peers in the same network using UDP broadcasts, maybe thats what you are looking for. this requires the app on the lan to listen on a specified port, the phone (or whoever is looking for that app) sends a udp packet to that port on the broadcast address (255.255.255.255), and the app replies with its individual ip address. not sure if that is what you are looking for and if its possible with android

Networking in Android

I know in Android you can do networking and I've done it but I've only been able to do it when the phone is connected to a WiFi network. I'm wondering if is there a way for my Activity to connect to my server without having an active WiFi connection.
It would kind of be a pain to market my game if you must be connected to a WiFi network in order to play with other people. When I tried connecting to my server without an active WiFi connection i get this error:
java.net.SocketException: No route to host
All answers are welcome. Thanks in advance!
In order to make network calls and use network resources, you need to be connected to a network: either with "cellular" data, such as 3G or EDGE, or WiFi.
However, Android should handle most this behavior pretty well behind the scenes.
For more information, check out Android's Connectivity Manager, Content Provider, and Java's Sockets.

Access dev machine localhost via usb

My activity needs to communicate with a java server located on my dev machine. Actually, using the emulator, I can access the server socket via the special 10.0.2.2 address. Now I'd like to test my app on my phone connected via usb, but it seems like I can't use 10.0.2.2 to access my dev machine localhost anymore.
Naturally using a wifi router and a proper lan addressing does the trick, but I need to use usb connection instead. Any hint ?
edit:
Just added different scenarios to let everyone understand better:
1. Emulator-based scenario:
standard java server running on my pc #localhost:15000
android java client running on my emulator.
When the android app needs to contact the server it uses 10.0.2.2:15000 like specified here.
Everything works fine.
2. Proper wifi scenario with physical connected device:
standard java server running on my pc #192.168.1.10:15000.
android java client running on my device with 192.168.1.11 address assigned.
When the android app needs to contact the server it uses 192.168.1.10:15000 and everything works fine too.
3. Desired scenario:
standard java server running on my pc #localhost:15000.
android java client running on my device connected via usb cable. No network is active.
When the android app needs to contact the server it tries to connect to 10.0.2.2:15000 but instead an exception is thrown (Network unreachable). It seems like special address 10.0.2.2 works only for emulator instances.
I just wonder if in the adb/adt suite there's a way to access pc address from the device connected via usb cable.
I hope it's a bit more clear now.
Thanks.
It seems like there's no way to let scenario #3 work properly, because usb connection is not a real network connection and there's no ip associated to it by the phone.
Actually my solution is to use an ad-hoc wifi connection between my phone and my pc. This scenario is similar to #2, but without router or dhcp. Unfortunately my Hero can't handle ad-hoc connections natively because of this issue. So I had to root my device and to change manually some config script. I hope functionality like this can be available in next releases.
You can setup a connect to your phone over USB from your dev pc, but you have to adb forward. However, I don't think you can route the otherway around over usb: setting up a connection from the phone to the pc.
You could have your pc connect to the phone and check which IP it has.
While thinking about it, I realized, using adb forward you have to use localhost as your host, so maybe you could also do this on your phone: adb forward the port you want to use and on your phone, connect to that port on localhost.

Categories