Why different behavior of localhost and 127.0.0.1?

Feb 17, 2014 at 10:58 PM
Edited Feb 17, 2014 at 10:59 PM
Why does localhost:3901 work ok, but 127.0.0.1:3901 doesn't work (hangs browser before timing out)? Shouldn't they operate the same way? I'm using Windows 8.1. I'm new so maybe I'm missing something obvious.
Works:
    m_listener = web::http::experimental::listener::http_listener(L"http://localhost:3901");
No work:
    m_listener = web::http::experimental::listener::http_listener(L"http://127.0.0.1:3901");
Hosts file fragment:
# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
Coordinator
Feb 18, 2014 at 12:20 AM
From our tests, we did not find any problems. Can you post your complete code to help us repro.

Thanks
Feb 19, 2014 at 2:18 AM
Edited Feb 19, 2014 at 11:45 PM
Something is really strange on my system. Maybe it's related to this issue, maybe not. I'm posting a uri issue on a new thread.

Update: 19-Feb-2014: My bad. I had intermingled the release .lib file with the debug .dll file.
Feb 19, 2014 at 11:42 PM
Edited Feb 19, 2014 at 11:43 PM
I tracked down the error. Here's the error message:

Access denied: attempting to add Address 'http://127.0.0.1:3901/'. Run as administrator to listen on an hostname other than localhost, or to listen on port 80.

Indeed, running the program as administrator works.

Questions:
  1. Why is it necessary to run as administrator?
  2. Seems odd that http://127.0.0.1:3901 doesn't work but http://localhost:3901 does work. Why is that?
  3. Is the need to run in administrator mode documented anywhere?
  4. What are my options to enable the program to run in user mode?
  5. Why hasn't this issue been raised before? I'm running Windows 8.1 64-bit.
  6. Does this issue also effect Linux?
Coordinator
Feb 20, 2014 at 9:51 PM
Hi BSalita,

What is going on here is how Windows works with HTTP URI namespace reservations. Take a look at these two msdn pages, one explaining for the HTTP Server API (which we build on) and another describing the same in the context of WCF also built on the HTTP Server API.

Basically by default all non administrator users don't have any HTTP URI namespace reservations. Localhost is special, but doesn't actually listen allowing external machines to connect. Only an administrator can setup and reserve a URL HTTP namespace. The idea is that this is usually done during installation or setup of your program on a user's machine, when administrator privileges are already required.

There are several ways this can be done, the command line tools netsh or HttpCfg.exe can be used. Also the Windows API HttpSetServiceConfiguration.

Thanks,
Steve
Feb 21, 2014 at 1:35 AM
Thanks for your help. I've figured out the netsh syntax for making a http reservation. I'm documenting here and on StackOverflow.

You must have Administrator privileges to add or delete. You must have a trailing / at end of url parameter.

To add a reservation:
netsh http add urlacl url=http://127.0.0.1:3901/ user=%USERNAME%

To show reservations:
netsh http show urlacl

To delete a reservation:
netsh http delete urlacl http://127.0.0.1:3901/

Anyone know how to get wildcarding of ip:port addresses to work. NONE of the following seem to work: url=http://+:3901/ or url=http://0.0.0.0:3901/ or url=http://*:3901/
May 21, 2014 at 12:45 PM
Anyone know how to get wildcarding of ip:port addresses to work. NONE of the following seem to work: url=http://+:3901/ or url=http://0.0.0.0:3901/ or url=http://*:3901/
May 23, 2014 at 6:31 AM
http://0.0.0.0::3901/ worked for me on RHEL. earlier firewall was blocking.

To know whether any firewall setting on HTTP do the following command

iptables -L | grep -i http

if any entry is there, then you make sure your iptables service is up and running.

to view the current status
service iptables status

to start the service
service iptables start