Cannot access localhost from Android device - android

Emulator can access web service fromlocal host but my real device cannot although I use LAN ip address (192.168.xx.yyy) instead of 10.0.2.2. Of course, my android mobile was recognized by adb and it connected my laptop via usb port.
I already read some similar questions at stackoverflow but still didn't know how to access webservice from a real android mobile. The answer of this question How can I access my localhost from my Android device? my be useful but i don't really understand that answer.
Anyone helps me? Thanks you!

Possibly the web server is listening on the loopback interface but not on the network interface. This means that hits on 127.0.0.1 and localhost will work, but 192.168.xxx.xxx will not (whether from localhost, LAN, or WAN).
To determine which interface the server is listening on, look here for a command to tell you about the listening ports (I used lsof -Pan -i tcp -i udp). Then look for your server process in the list. For the lsof command, if for port 8888 you see something like TCP *:8888 (LISTEN) then your server is listening on all interfaces. But if you instead see something like TCP [::127.0.0.1]:8888 (LISTEN) then you have identified your problem!
The next step to solve your problem is to set up your server's run configuration to listen on all interfaces. I don't know what server you are using, but if you can specify an IP address, then you may want to try 0.0.0.0. Usually you can do this near where it lets you specify the listening port. Thus, if you have a configuration like:
--port 8888
Then you can try:
--port 8888 --address 0.0.0.0

In my opinion, the below point is more important for developer community
I am not a network expert; like me many developers may be there.
change to 0.0.0.0:80 in httpd.conf is very important.
In normal(default) settings, port 8888 throws error in WAMPSERVER (even in Windows Browser). That is why I explained the exact environment where port 8888 throws error.
Specific information about what configuration throws error and what configuration works successfully is more important.

Have you tried using a wifi router which is connected to your LAN? Then you just need to connect to the wifi using your mobile and you should be able to access (192.168.xx.yyy)

Related

Run ASP.NET application on server and access from remote computer/phone

I am trying to debug my visual studio 2012 asp.net project from my phone and have followed all steps here and here.
However none of them work, can't even connect from my other laptop. When I add my public IP or use * IISExpress doesnt even seem to pick up on it (Can't find it in IISExpress->show all applications). But When I used my local IP at least it showed up there and I could connect to it from my own computer, but no other computer/phone.
The cmd commands are successful, I add the extra binding, I open the firewall. Dont know what is wrong.
Which IP (local/public) should I use? what should I type in as URL when I want to test it? I am trying http://IP:port and http://IP:port/Default.aspx.
EDIT: Using this solution I got it to work on my laptop, however I still can not get it to work on my phone.
Basically, the points mentioned in the links you have cited are enough for enabling communication with the phone. But here is one point to make sure (since you have not mentioned it): You have to make sure that both the server and the clients (in your case, the machine that runs the visual studio and your phone and the other laptop) are on the same wireless network. To find out the IP that your server/development machine is currently using, you can issue ipconfig command in cmd and look at the Wireless Network Adapter IPV4 address. For example, if your laptop IP address is 192.168.1.2, you should use http://192.168.1.2:8080/Default.aspx (assuming that your site runs on the port 8080).
You use the local IP if you're on the same network as your server (for example, use WiFi on your phone and join the same network that your server is on).
You use the public IP if the device is outside your network, and you have to configure the firewall to forward the correct ports to the local IP. That's NAT traversal. This would be if you're using the phone's cellular data connection, or you're using a WiFi or wired network that's different from the one your server is on.
Your URL should always be of the format <protocol>://<IP or DNS name>:<port>/<path>. However, you can omit the IP (and the colon) if you're using HTTP on port 80, or HTTPS on port 443. And you can omit the path if you're accessing the root of the site, and your site has a page correctly configured for the root (either via default document in IIS, or route defaults if you're using routing features).

how to access my 127.0.0.1:8000 from android tablet

I am developing a webpage in django (on my pc with windows 7) and now i need to test some pages in tablet pcs. suddenly the thought came if i can have an access to my localhost in windows from android tablet. is that possible? I am in the same wifi connection in both devices at home.
i read a lot questions and answers regarding this issue in stackoverflow and other places, but none of them gave me some concrete solutions.
I have samsung tab 2 10.1 with android 4.0.4.
appreciate any help, thanks
So, there are a couple of issues it seems. The question most of the answers are addressing is "how do you connect to another server in your local network?" (or variants). There are two answers, you can use the computer's IP directly, or you can use the computer's name (you may need to append .local). For example, my computer is xavier.local.
The second issue is that you seem to be addressing is that runserver is not accessible via other computers on the network (this is your actual question). The reason is that by default Django's runserver will only acknowledge requests from the machine which is calling them. This means that the default settings would make it so that you would only be able to access the server from Windows (and they did this on purpose for security reasons). In order for it to listen to other requests you have two options:
runserver 192.168.1.101:8000
# Only handle requests which are made to the IP address 192.168.1.101
Or (and this is easier when dealing with more than one environment):
runserver 0.0.0.0:8000 # handle all requests
So, if your IP address is 192.168.1.101:
runserver # only requests made on the machine will be handled
runserver 127.0.0.1 # only requests made on the machine will be handled
runserver 192.168.1.101 # handles all requests (unless IP changes)
runserver 192.168.1.100 # does not handle any requests (wrong IP)
runserver 0.0.0.0 # handles all requests (even if the IP changes)
I do think it important to note that 0.0.0.0 is realistically not a security question when dealing with a local, development machine. It only becomes a significant problem when working on a large app with a machine which can be addressed from the outside world. Unless you have port forwarding (I do), or something wonky like that, you should not be too concerned.
Though this thread was active quite a long time ago. This is what worked for me on windows 10. Posting it in details. Might be helpful for the newbies like me.
Add ALLOWED_HOSTS = ['*'] in django settings.py file
run django server with python manage.py 0.0.0.0:YOUR_PORT. I used 9595 as my port.
Make firewall to allow access on that port:
Navigate to control panel -> system and Security -> Windows Defender Firewall
Open Advanced Settings, select Inbound Rules then right click on it and then select New Rule
Select Port, hit next, input the port you used (in my case 9595), hit next, select allow the connections
hit next again then give it a name and hit next for the last time.
Now find the ip address of your PC.
Open Command Promt as adminstrator and run ipconfig command.
You may find more than one ip addresses. As I'm connected through wifi I took the one under Wireless LAN adapter WiFi. In my case it was 192.168.0.100
Note that this ip may change when you reconnect to the network. So you need to check it again then.
Now from another device (pc, mobile, tablet etc.) connected to the same network go to ip_address:YOUR_PORT (in my case 192.168.0.100:9595)
Hopefully you'll be good to go !
You can find out what the ip address of your PC is with the ipconfig command in a Windows command prompt. Since you mentioned them being connected over WiFi look for the IP address of the wireless adapter.
Since the tablet is also in this same WiFi network, you can just type that address into your tablet's browser, with the :8000 appended to it and it should pull up the page.
127.0.0.1 is a loopback address that means, roughly, "this device"; your PC and your android tablet are separate devices, so each of them has its own 127.0.0.1. In other words, if you try to go to 127.0.0.1 on your Android tab, it's trying to connect to a webserver on the Android device, which is not what you want.
However, you should be able to connect over the wifi. On your windows box, open a command prompt and execute ipconfig. Somewhere in the output should be your windows box's address, probably 192.168.1.100 or something similar. You tablet should be able to see the Django server at that address.
need to know the ip address of your machine ..
Make sure both of your machines (tablet and computer) connected to same network
192.168.0.22 - say your machine address
do this :
192.168.0.22:8000 -- from your tablet
this is it !!!
If both are connected to the same network, all you need to do is provide the IP address of your server (in your network) in your Android app.
Tested using easy EasyPHP DevServer 14.1.
The trick is you must first add your local ip address to the Apache server to listen to it.
Right click on the tray icon, go to "Configuration" -> "Apache" in the "httpd.config"
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 127.0.0.1:80
Listen 192.168.0.201:80 <<-- ADD THIS LINE
Then go to your Control Panel and deactivate your Windows Firewall or add your Smartphone/Tablet Ip Address to the exception.
That's it. I hope it helps.
The IP Address 192.168.0.201 may be different from yours. 192.168.0.X
I've struggled with this problem myself, and I couldn't figure out what it was, since it worked perfectly on my iPhone, I decided to look into the problem and find a quick solution.
My local machine is a Mac OSX 10.10+, one option would have been to start an Apache server, but that's super unhandy - changing the DocumentRoot every time you want to check something quickly on your Android device. Besides that, changing the DocumentRoot is a pain in the a** on Mac OSX 10.10.
If you want to use:
python -m SimpleHTTPServer
on your Android, do this:
sudo python -m SimpleHTTPServer [ANY PORT YOU WANT, BUT NOT 8000]
I hope this helps.
cheerz -Pit
Try this
python manage.py runserver
then connect both tablet and system to same wifi and browse in the address
eg: python manage.py runserver 192.168.0.100:8000
In tablet type that url in adress bar
In my case I was required to disconnect and reconnect to the same network (both phone and pc), after changing the firewall settings.

Localhost running on mac.. Can I view it on my Android phone?

Running a ruby on rails project on my mac. I need to test it on my android phone. Is there a way to view my mac localhost on my android phone?
The name "localhost" is just a loopback to your own computer. To access it on your Android, you need to find the computer's IP address.
The most general way of finding this info, which works on both Mac and Linux, is to go into the Terminal and type ifconfig. Look for either "en0" or "en1", and under that entry look for the "inet" listing. It will be something along the lines of "192.168.1.100". When you find that address, that's what you'll want to put in your browser's address bar.
(On a Mac specifically, you can go to the Sharing pane in System Preferences and it'll tell you there.)
Here's a quick to-do to have your localhost available for tests on other devices :
1) identify the IP of your Android : select the Wi-Fi you're connected (the same as the one the Mac is on), you'll have the IP detailed on it). For the example : we suppose your Android IP is : 192.168.0.10
2) open a Shell on your Mac and edit your host :
sudo nano /etc/hosts
3) edit the file as this :
127.0.0.1 192.168.0.10
4) Identify your Mac IP : (as #Chuck perfectly explained)
(in your shell) :
ifconfig
5) Open your favourite webbrowser you use on your phone and connect to your Mac IP (with the port if needed) with directly something like :
http://192.168.x.x:8000/
6) Enjoy your test :)
Notice you can do that for every support connected on your Wi-Fi.
Chunk's answer is correct, assuming your mobile device and your computer are on the same network. However, if you want your localhost server to be visible to the wider Internet (e.g. for testing over 3G, developing webhooks or collaboration with a remote colleague/client), more is needed as local addresses (starting with 10.* or 192.168.*) are not visible to the wider Internet.
The traditional solution to that is port forwarding and dynamic DNS, but lately a few services have popped up which aim to make this process simpler (disclaimer: I am the author of one of them, PageKite).
These services provide you with a public DNS name and software which connect your "localhost" with an in-the-cloud relay server (a.k.a. a reverse proxy). For example, if you are using PageKite, you can run the following command in the terminal:
$ pagekite.py 80 yourname.pagekite.me
... to create a mapping from http://yourname.pagekite.me/ to the web server running on http://localhost:80. While the program is running, your localhost site will be visible to the rest of the Internet. In order to make it private again, you simply turn off the pagekite.py connector program.
For completeness, here are some of the localhost tunneling services I am aware of:
PageKite is Free Software (Python) with a "pay-what-you-want" on-line service. You can create as many long-lived subdomains as you want, a wild-card SSL certificate is included and front-end relays run in multiple geographic locations to provide redundancy and responsiveness.
Localtunnel is a free-of-charge (sponsored by Twilio) Ruby solution which gives connections temporary names. Note that names are recycled so you may see unexpected traffic while the connection is live.
Showoff.io and Tunnlr.com are proprietary paid service comparable to Localtunnel, based on the same basic technology (ssh tunnels).
(Sorry about not linking to the last two, SO spam protection is preventing me from being fair to my competitors. ;-)
Found this great, free tool today - really easy to set up and works like a charm! Versions for Mac OS, Linux and Windows also available.
https://ngrok.com/
(am not associated with it in any way)
This worked for me for accessing rails server with IP over local network:
The firewall has to be turned off.
/etc/hosts should have this entry:
127.0.0.1 192.168.100.12
where 192.168.100.12 is the ip address which can be found by ifconfig command in terminal.
Start rails server with this command:
rails server -b 0.0.0.0 -p 8080
I was able to access my localhost through http://192.168.100.12:8080/
Additionally, if you want to test on Android/iOS device a PWA Apps developed with Angular, you will need to use:
ng serve --host 0.0.0.0 to start up the server CLI.
If you receive "Invalid Host Header", uses:
ng serve --host 0.0.0.0 --disableHostCheck true
after reading this thread (and the suggestions working!) I put together a single guide to solving this issue. This link to that guide that has screenshots for every step and where to look. Or the text is pasted below. Thank you for the help!
First off, both your phone and laptop must be connected to the same WiFi network. If you're using your phones mobile hotspot, it will still work. However, make sure to connect both devices to the same network before moving forward.
Next, collect your phones IP address. To do so, I use the Network Info II app. There is most definitely another way to accomplish this. However, Network Info II works as needed and is document for this tutorials sake.
Now open the terminal on your computer (don't worry about which directory you're within) and run the command sudo nano /etc/hosts/. After entering your system password, you'll see near the top of the terminal a string of numbers with the word localhost following. Localhost is in fact just an alias for your computer's own server address so that when you go to localhost in a browser it simply routes the http request to your local machine. In order to tie your phone into this loop, enter its IP address right between the string of numbers and Localhost.
To save this, hit control + X and then Y when prompted to save. After that, the enter key will bring you back to the standard command line.
Open a second tab in the terminal and launch a local server. I've only tested this using a simple python server, which can be run by running in the terminal python -m SimpleHTTPServer 8000. However, I'm assuming that you can launch any local server you like, being that all you'll need to reference is the port number. If you do use the simple python server, that port number is 8000.
Run ifconfig in the terminal. This will bring up a slew of information, to which you should scroll about halfway down. What you are looking for is a string of numbers that follow after an inet and before netmask within either the en0: or en1: key.
Done! On your Android, open up a browser and visit the inet number, followed by a colon (:) and the port number.
Although one answer has been accepted but this worked for me:
Make sure both your computer and android device are on same network.
get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.)
Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.
search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**
try accessing 1**.1**.15*.2**:port/ from your mobile device.
It is as simple as this.
MacOS Catalina 10.15.4
Go to Settings -> Security and Privacy.
Select Firewall tab and unlock the settings using your laptop password:
Select Firewall Options... and in the dialog uncheck the box for Block all incoming connections and hit OK.
Open Terminal and type ifconfig. Look for the en0. In the section find the address sitting near inet - this is the address of your laptop in a local network.
en0: flags0=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 0
...
inet 192.168.0.123 netmask 0xffffff00 broadcast 192.168.0.255
...
Use the IP address + the port (in case you are using localhost:4200 for development), otherwise omit the port:
http://<IP address found in en0 -> inet>:<port if needed>
Profit!
If you run your localhost with php the solution was replacing localhost with the Macs IP
php -S 192.168.x.x:8080
instead of
php -S localhost:8080
For MacOs, open up System Preferences > Network.
you should see the message " and has the IP address: my-ip-address"
That will be your IP Address, then in the browser navigate to http://my-ip-address/port-number. Remember not to put an "s" in the http.
As mentioned in other answers, url has to consist of the IP address of computer and not localhost. If that isn't working, before trying anything else, check if the port is correctly mentioned along with IP address when making an api call in the Android app. That is, you should be making an api call to a url which is of the format: http://192.168.X.X:80/api/..
For using localhost on Android you should use your local ip address when connecting to it. First check your local ip address of your computer. Then follow along:
In addition to the good accepted answer, if you need to use localhost via your local ip address follow this steps:
Add your local ip address into httpd.conf (/etc/apache2/httpd.conf) as below:
(Change the port according to server you are using)
Listen 12.34.56.78:80
Listen: Allows you to bind Apache to specific IP addresses and/or ports, instead of the default.
Save, close and restart apache.

Use adb forward instead of redir

I have a client and a server app on Android (that uses ZeroC - Ice platform for communication). Server listens on TCP port 10000 and client uses TCP port 5501 (arbitrary port numbers I have chosen).
When I telnet to server and run redir add tcp:5501 tcp:10000 it works smooth.
However, due to my current system setup (using androidx86 binary through a virtual machine), I can no longer telnet to the server. So I was hoping I could use adb forward instead. However, whatever port combination I tried, I couldn't get it to work.
I would really appreciate if someone could shed some light on these:
- Is it possible to telnet to a device replicate or device? I've read on another post on stackoverflow that telnet interface is only available for emulator
- What is the difference between redir and adb forward? How should I use adb forwarding to achieve redir tcp:A:B on server?
Thanks
I've finally managed to get it to work. Several comments
USB connected devices cannot be reached via ethernet. They can be reached via wireless network (assuming they are connected to the same network with the other party)
VirtualBox has its own NAT forwarding. Without port forwarding, under default networking configuration (which is NAT), the virtual device is inaccesible.
My working setup is as follows
A USB-connected device connects to the PC via wireless (Find out your PC's local IP address, which is often something like 192.168.1.x)
I have enabled VirtualBox forwarding for NAT. My option is TCP 10000:10000 (where 10000 is an arbitrary port number. Note that it should be higher than 1024) So when my USB-connected device connects to the PC at port 10000, it is forwarded to the virtual device via VirtualBox's forwarding.
Virtual device listens to TCP port 10000. (I didn't specify host address to make life easier)
If you get a 'NetworkOnMainThread' exception, use
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Hope this helps someone

Android Emulator (qemu) does not allow certain tcp connections (RST,ACK as answer to SYN)

Although Internet browsing works in the emulator, my app who also runs in the emulator, is unable to connect to a server on the internet on port 5222 via TCP. I'm pretty sure that there is no firewall involved on my router or on my pc, as I can telnet to that ip:port from my pc.
Here is a screenshot of the packettrace taken on the emulator: http://i.stack.imgur.com/sIFFM.png
A packet-trace on the emulated device showed that every SYN packet to that particular port is answered with RST,ACK and the connection is closed by my app with a "connection error".
Why does the emulator block (?) these ports? How can I change it?
The Emulator runs on it's own network, so if you want to communicate through different ports you have to setup forwarding correctly for non-standard ports.
If you want to have external applications connecting to your application running in the emulator you will need to setup your forwarding so that the external application will connect to the port on your development machine and emulator will forward it connection to the 10.0.2.15 which is the emulators address.
See the dev guide for more info:
http://developer.android.com/guide/developing/tools/emulator.html#connecting
Got it. As http://developer.android.com/guide/developing/tools/emulator.html#proxy states, the proxy set in the environment variable http_proxy will be used for all TCP connections.
But my proxy is configured to allow only certain TCP ports.
unset $http_proxy
or use
emulator -http-proxy none
("none" is not a correct proxy, so it will give you an error message. But at least the environment variable is not processed)
PS:
If the -http-proxy command is not supplied, the emulator looks up the http_proxy environment variable and automatically uses any value matching the format described above.
Google, thats crazy. I've set the http_proxy environment variable because I wan't the http(s) traffic to go through that proxy. Not all TCP Connections.

Categories

Resources