47

I have an app running on my computer at 127.0.0.1:3000

I would like to access that app from an iPhone connected to the same network. I have done this before but blanking out on how I did it. Any ideas?

1
  • 7
    try rails server -b 0.0.0.0 , then browser this on your iPhone.
    – zx1986
    Apr 29, 2016 at 2:25

6 Answers 6

51

First you need to determine the ip address or name of the machine you are running the webserver on. I'm assuming you are running the webserver on a mac since you tagged your post macosx athough the instructions are similar for linux machines. So, on your mac:

  • Open Terminal.app. It's under Applications->Utilities.
  • Run ifconfig in the terminal. That shows you all the network interfaces on the machine. One of them is the network your machine is actively connected to. If you mac is on a wired connection that should be en0. Make a note of the address after inet - that should be the address your machine uses.
    • Let's assume you discover it's 192.168.10.1.
  • Verify that you can connect to that address from your server with nc -v 192.168.10.1 3000. (replace 3000 with the port your application is running on)
    • You should see a message like Connection to 192.168.10.1 3000 port [tcp/http] succeeded!.
    • If that doesn't work, see below.
    • If it does work, hit ctrl-C to exit the nc session.
  • Now try to connect on your client machine.

If you are unable to connect to your application on the server's real address, that means your application isn't listening on that address. You will need to investigate how to change your application configuration to modify that behavior. Since I don't know what application you are running I can't offer any good ideas on that.

4
  • 10x mate... whats the difference when talking about a windows based system with Wamp server on it... is there any difference ?
    – Sagive
    Jan 9, 2012 at 21:09
  • 2
    I am using mac sierra. I am able to access the server from my mac (where the server is running) via 10.55.18.22:3000 but not able to access it from another machine even though both are in the same network. Web sharing option is not available in sierra. How can we make this work? Apr 19, 2018 at 6:19
  • 1
    saanthosh: Look into port forwarding on your router. That's what I was doing before I discovered OSX's internet sharing feature (which apparently has been available since at least OSX 10.6). I did localhost testing on a variety of mobile devices in the same room/network as my OSX dev machine, made possible by standard/small changes to router configuration (ie port forwarding). Other articles and SO threads provide info on router configuration. Ultimately, I'm in a new location now and have no access to the router, so instead I use OSX (High Sierra) internet sharing to achieve the same thing.
    – Kalnode
    Jul 14, 2018 at 18:25
  • Running on port 80 solved my problem. python3 -m http.server 80. Works in both directions. Mar 7, 2023 at 18:33
14

Find the name of your Mac using hostname (at the Terminal prompt) and use that in your URL. E.g. http://Tonys-iMac.local:3000/

If for some reason Bonjour doesn't work in your environment, find the address of the Airport on an iMac or MacBook with

ipconfig getifaddr en1

or in general with

ipconfig getifaddr $(route -n get default|awk '/interface/ { print $2 }')

3
  • This answer has some good optimizations over mine regarding finding your hostname and ip address. Feb 2, 2011 at 4:49
  • 2
    why would bonjour not work ? I find my hostname is gm20152.local . But when I try gm20152.local:3000 on the same machine, it brings up a valid page. But when I try gm20152.local:3000 on my android phone, the browser on my phone could not find the host. I think this is because my phone has no way of mapping gm20152.local to my macbook's ip addr. How can I accomplish this ?
    – gprasant
    Aug 11, 2014 at 20:35
  • @gprasant Getting Android to use Bonjour seems pretty hard
    – James
    Sep 19, 2014 at 11:25
6

Basically, from firewall settings you can allow a certain application (e.g. ruby) to accept incoming connections. Plus to allow access to the outside world (e.g www), you'll need to forward traffic to your internal gateway:port via your router settings.

Here's how to do this:

  1. Mac->Sys Preferences->Sharing->Enable “Web Sharing” checkbox
  2. Mac->Sys Preferences->Security-> allow your application (e.g. ruby) to accept incoming connection
  3. Open a port on the router (via 192.168.1.1) to forward traffic from your_web_ip:port to a local_gateway:port

    1. E.g. from my Verizon's router settings -> Port Forwarding -> create rule: forward to local gateway (e.g. 192.168.1.4), custom port, protocol tcp, source=any, destination=3280, all connection types, forward to port = 3000.

Done. Now from the remote computer, open your browser to your web ip address (find via http://www.whatismyip.com/) + destination port# above, e.g. 72.189.194.65:3280, this will connect to your local 192.168.1.4:3000

Note: I'm running on Mac OSX 10.7.5

3

If the application is listening on 127.0.0.1:3000 only then you can't access it from another computer. To do so you would need to modify the configuration to Listen the IP or 0.0.0.0 (all available interfaces).Thats option one.

The second option is to use a proxy.

Third option is if you can ssh from the iphone you can also use ssh forwarding.

ssh user@host -L 3000:127.0.0.1:3000

Then on your iphone open 127.0.0.1:3000

2
  • 1
    AFAIK, you can't do ssh port forwarding on an iPhone. Not if it's not jailbroken anyway.
    – Sven
    Feb 1, 2011 at 3:42
  • 1
    Listen to 0.0.0.0! How could I didn't think of that? Aug 2, 2018 at 12:05
2

127.0.0.1 is the local address every computer has for itself. You have to find out what the real IP address (or Host/Bonjour name) of the machine is. Go to System Preferences, Network and look up the IP of the machine, either for the Ethernet port if you use a cable or the Airport if you use WLAN . Then open this address together with the :3000 part in Safari on the iPhone.

1
  • Also the Sharing Preference Pane always shows a name or address by which your computer can be reached.
    – James
    Feb 4, 2011 at 21:02
1

Have you tried this, it worked for me (I'm not affiliated with it) :

https://github.com/progrium/localtunnel

Not the answer you're looking for? Browse other questions tagged .