Access dev machine localhost via usb - java

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.

Related

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.

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

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.

Internet of Things : ESP module Wifi Ip address

as you can read in title I'm making a super simple IOT (Not really)
and
using a esp8266 and want to make it connect to my home wifi network and make a communication between a android app and the module (android app connects to home wifi)
Note : Iam Using CODE VISION AVR
you may say :
Why not directly connect to ESP when its on Server Mode?
that's fine cause when ESP is server "he" can set ip for him self so
ip will be always same BUT I want it to connect to home wifi
-
Why you wont connect with IP ?
you cant find the module ip easily because its given by Wifi
-
Find ESP ip using IP scanner
Its not possible cause it changes every time and user have to change
IP every time in android app AND my app cant scann every time it want
to connect to a single device its so dumb...
-
Do a Static Ip for ESP !
well that's not possible too because maybe that IP was taken by
someone before ESP send static IP AT command ! + if IP wasent taken it may taken after Home Wifi Restarts !
Sorry for bad English :X
You can use software serial to configure esp8266 from arduino. You need to know the setup circuit perfectly. Keep it in mind that esp8266 operates on 3.3V where arduino gives 5V output. The most important thing is to send AT commands to esp8266. You can use the following command to connect with your wifi.
AT+CWJAP="Your_WiFi","password"
If you need to see the ip address of esp8266, you can use this command.
AT+CIFSR
If you want to set static ip to esp8266 , then you can try this one.
AT+CIPSTA="ip module","ip gateway","subnet mask"
Use mDNS responder:
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino
This way you would be able to connect to ESP8266 via hardcoded url like http://esp8266.local.

connect android device to servlet

I'm building an android application that should connect to servlet called "Login" in order to verify the user name and the password. It works fine en genyMotion emulator when I use :
http://192.168.1.100:8081/Android/Login
. But , when I try to connect using my real device , I got time out connection error. I'm using "my wifi router 3.0" to make my laptop hosting my phone. I even tried
http://192.168.23.1:8081/Android/Login
but still didn't work.
Here's my ipconfig :
This seems like a firewall issue on your system.
Emulator is able to connect because the connection is originating from localhost (same system). However the connection from real device is considered as incoming connection from remote host.
By default windows does not allow any incoming connection from remote host. You change this in firewall setting to allow incoming connections on port 8081.

Why cant I access glassfish server from my local network

I installed netbeans with glassfish server for an ejb project.
If I enter localhost:9999/myproject/ in my browser its works.
When I also tried mylocalip:9999/myproject/ it worked.
But when I try to connect from another PC or phone on the same network I can't reach my project (pc and phone connect via wifi)
notes:
I connect to the network via wifi to router.
I turn off the firewall and add rule in the firewall with port 9999 but doesn't work.
I can't ping other devices on the wireless network.
i try to ping the address of another pc from my pc is work.
my system is windows 7
netbeans 8.0.1
glassfish 4.1
As I see you are trying to host a project on your LAN network, so all devices on your LAN can connect to it. Firstly check your settings for your server and make sure everything is alright. I suggest you take a look at Configuration > HTTP Service > HTTP Listeners and check what it is bound to. If the IP address is not your network interface address or 0.0.0.0 then change the IP address to your network interface address or 0.0.0.0. Note that after editing any settings you must restart the server. Also make sure that all are on the same LAN network (connected to the same router / Wi-Fi network). Then to connect to it hold the Windows key and press R. Run window should appear. In there type cmd and hit enter, then in the black window type ipconfig/all and look for a Local Area Connetion or Wireless LAN Adapter. At one of these there should be an IPV4 address that starts with 192.168. Use that to connect (in your case 192.168.x.xxx:9999/myproject/).
Hope I helped!
I just had the same issue..
First make sure you can ping your server
ping 192.168.xxx.xxx:9999
The make sure you have no blocking firewall (on ubuntu)
sudo ufw status
Check if your port is allowed (9999 ALLOW).
If you can't see it on the list, add it:
sudo ufw allow 9999
Happy glassfishing :)

Categories