Can't connect device to localhost - java

I'm trying to connect my real android device to local server.
So at the beginning I connect all devices to 1 network via Wi-Fi.
Next step is copy IPv4 from ipconfig.
Now I'm entering myIpv4:8080 into browser in my android Device and I get ERR_CONNECTION_TIMED_OUT.
I forgot something ? What can be wrong ?
I should get JSON object like in PC browser.

can you check your web server is listening on a 0.0.0.0 or just localhost?
Localhost is a loop back IP for every machine. If a webserver is listening to Localhost interface, it only accepts network request from the machine it runs on. Your Android request may be ignored.

Related

Socket TIMEOUT when phone is on 3G, and works when on Wifi- Why?

I'm programming a client-server app, my client being an android phone, my server being my laptop.
So my issue is that this one line of code:
Socket connectionToServerSocket = new Socket(hostName, portNumber);
works perfectly fine when my cellular phone connects to the Internet with my home wifi connection, and simply times out when connected through 3G (cellular provider) eg it blocks for a while then throws a timeout exception.
The funky thing is,that I can see (using OS Monitor) that some apps are connected through very common ports, for example port #80, but switching portNumber (as well as the port that the server is listening to) to 80 doesn't help (eg it still times out), and I've tried many different ports-same result.
DNS works fine (eg it translates the logical String which I gave hostName to the correct IP) but it doesn't send the server anything...
I'm lost,what could be the reason? How can I check and resolve it?
I've run into this issue as well doing a similar application.
Your laptop and phone can connect to each other while on the same network because they share a IP address lookup through your router.
When the device is connected to the WIFI, it's request get passed through the router to check for IP addresses, it will find your laptops IP and save a request to a DNS because it can find the laptops IP already. The laptop works the same way, it finds the Ip address of the client through the router as well.
However, when your phone is on 3G, it has no way of knowing exactly where your laptop's IP address is. That's why it times out: it goes from your router to your nearest DNS (where it tries to resolve the correct IP Address), if it cannot find a domain or IP that matches it will fail.
Some steps to fix this . . .
Depending on your router you can set up port forwarding for your laptop's IP. This means incoming requests to your router can be piped to your laptop's server implementation.
Then go to any site like this http://touch.whatsmyip.org/ on your laptop to get your laptops IP. Save this to add to your clients Socket set up.
For debugging until your laptop server is visible for DNS lookup, go into your client code and add this.
Socket debugSocket = new Socket("the.laptop.ip", 80);
Some warnings:
Depending on your Router, your ip may change during restarts
With port-forwarding any browser with your ip, ex) 178.12.434.01 can log onto your laptops personal server
Future Changes:
Once a dedicated server is up and running, registered with a domain you can change the above ip parameter to "your.domain.com", and behind the scenes the actual IP address to your server will be found via DNS lookup.
the reason for that is that the server in your laptop is closed to external network by default, what you need to do is something called port forwarding
*take note: port forwarding put your server in a cyber security risk, make sure you make the right adjustments to keep your server safe.
case 1: It is working when your laptop and your phone is connected to wifi right ?
Try this once
case 2 :
connect your phone to 3G.
enable hotspot on phone.
connect your laptop to your phone's hotspot.
check the IP of your laptop if it is changed replace that in Socket object. Socket connectionToServerSocket = new Socket(newIP, portNumber);
Run your project.
Just try this once you will get what i am trying to say.
You are getting the timeout exception because your server that is
having the service is not reachable from the external network.
Hope this will help :)

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 :)

Openfire Sever IP address not able to connect on Android Application

I'm new to Openfire server so I'm not very sure on how to use it.
I'm trying to connect the server to an Android application which I had found on the web :
From the code , there is a host , port , username and password.
I tried input the following:
host : 127.0.0.1
port : 5222
However , the result of the application had failed which stated the error message that the server was not connected.
I had found on the web (Why can't I connect to the openfire server?) which the methods stated to edit the openfire.xml file. I had edited the file and the host number from the code into my ip address and it manage to work.
I'm just wondering why doesn't the original server ip address (127.0.0.1) not able to work while my ip address able to work?I'm also not sure if this problem only applicable to me alone.
Currently , I'm using emulator from Eclipse to run the application. But i believe in the future I will faced this issue again as I doubt using my own IP address able to work in the phone.
Edited :
I understand that Emulator needed network connection to connect to Openfire server which is why I have to change.
Currently my doubt is how to change the code to such that my application is able to connect and send messages via Openfire on mobile phone devices ? I had tried searching solution but I couldn't find any. One solution I found in this website was to put the connection part of the code in AsyncTask. Is this true ?
I'm just wondering why doesn't the original server ip address (127.0.0.1) not able to work while my ip address able to work?
127.0.0.1 isn't the server's real IP address. It means localhost, which means whichever computer the code is running on. In other words, the Android application would try to connect to the server on the Android device, but I don't think your OpenFire server is running on the Android device, is it?

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