C# http listener not listening on localhost, only works with FQDN
<p><em>(Please remove the close votes. IMHO this is a valid question, all necessary information is provided and yet tried to brief with all necessary information that's required to identify the possible source of the problem).</em></p>

<p>I'm new to C# coming from the Java world. I try to get a simple http server to listen on a port (in my case 8080) on localhost.</p>

<p>I followed this blog post <a href="https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx" rel="nofollow noreferrer">https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx</a>.</p>

<p>When I run the program, it says </p>

<p><a href="https://i.stack.imgur.com/SwtLN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SwtLN.png" alt="enter image description here"></a></p>

<p>but I cannot reach <a href="http://localhost:8080/test/" rel="nofollow noreferrer">http://localhost:8080/test/</a> via browser and <code>netstat</code> does not show port 8888 either.
The port is also not blocked by firewall.</p>

<p>Generally, I can run other servers, such as Tomcat, on that port, without a problem. The port is not blocked or used by other applications when I run the C# http server program.</p>

<p>The code:</p>

<p><strong>Program.cs:</strong></p>

<pre><code>static void Main(string[] args)
{
    WebServer ws = new WebServer();
    ws.Run();
    Console.WriteLine("A simple webserver. Press a key to quit.");
    Console.ReadKey();
    ws.Stop();  
}
</code></pre>

<p><strong>WebServer.cs:</strong></p>

<pre><code>namespace WebServerTest
{
    public class WebServer
    {
        private readonly HttpListener _listener = new HttpListener();
        private readonly Func&lt;HttpListenerRequest, string&gt; _responderMethod;

        public WebServer()
        {
            if (!HttpListener.IsSupported)
                throw new NotSupportedException(
                    "Needs Windows XP SP2, Server 2003 or later.");

            _listener.Prefixes.Add("http://localhost:8080/test/");
            _responderMethod = SendResponse;
            _listener.Start();
        }

        public static string SendResponse(HttpListenerRequest request)
        {
            return string.Format("&lt;HTML&gt;&lt;BODY&gt;My web page.&lt;br&gt;{0}&lt;/BODY&gt;&lt;/HTML&gt;", DateTime.Now);
        }

        public void Run()
        {
            ThreadPool.QueueUserWorkItem((o) =&gt;
            {
                Console.WriteLine("Webserver running...");

                    while (_listener.IsListening)
                    {
                        ThreadPool.QueueUserWorkItem((c) =&gt;
                        {
                            var ctx = c as HttpListenerContext;

                            string rstr = _responderMethod(ctx.Request);
                            byte[] buf = Encoding.UTF8.GetBytes(rstr);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);


                        }, _listener.GetContext());
                    }


            });
        }

        public void Stop()
        {
            _listener.Stop();
            _listener.Close();
        }
    }
}
</code></pre>

<p><strong><em>=========== Update ===========</em></strong></p>

<ul>
<li>I changed the port to 8080 </li>
<li>all try-catch-blocks in the code are
removed </li>
<li>I made sure that the port is not an issue: I tested to run
Tomcat servlet container on port 8080 and it works fine (of course,
now it's stopped again) </li>
<li>Port 8080 is not blocked by any other
application nor Firewall</li>
</ul>

<p>This is what happens when I start the application:</p>

<p><strong>netstat -na | find "8080"</strong></p>

<pre><code>C:\&gt;netstat -na | find "8080"
  TCP    10.10.1.177:8080       0.0.0.0:0              LISTENING
  TCP    192.168.56.1:8080      0.0.0.0:0              LISTENING
  TCP    192.168.99.1:8080      0.0.0.0:0              LISTENING
</code></pre>

<p><strong>I don't understand why nothing is listening at 0.0.0.0:8080 (as it is the case for example when I run Tomcat on port 8080), but on those other IPs.</strong></p>

<p>However, I cannot access the http server via localhost...</p>

<p><a href="https://i.stack.imgur.com/vZeK2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vZeK2.png" alt="enter image description here"></a></p>

<p>..or when I try to access via those IP addresses, it's return a "Bad Request" response but not </p>

<p><a href="https://i.stack.imgur.com/DvTum.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DvTum.png" alt="enter image description here"></a></p>

<p>I also tried other ports such as 7777 or higher ones 10888. </p>

<p><strong>ipconfig:</strong></p>

<pre><code>C:\&gt;ipconfig

Windows-IP-Konfiguration

Ethernet-Adapter Ethernet:

   Verbindungsspezifisches DNS-Suffix: mycompany.ch
   Verbindungslokale IPv6-Adresse  . : fe80::5511:3eb5:408a:2562%4
   IPv4-Adresse  . . . . . . . . . . : 10.10.1.177
   Subnetzmaske  . . . . . . . . . . : 255.255.254.0
   Standardgateway . . . . . . . . . : 10.10.0.1

Drahtlos-LAN-Adapter WiFi:

   Medienstatus. . . . . . . . . . . : Medium getrennt
   Verbindungsspezifisches DNS-Suffix: mycompany.ch

Ethernet-Adapter VirtualBox Host-Only Network:

   Verbindungsspezifisches DNS-Suffix:
   Verbindungslokale IPv6-Adresse  . : fe80::b4b0:6fc5:d0fc:1c77%24
   IPv4-Adresse  . . . . . . . . . . : 192.168.56.1
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . :

Ethernet-Adapter VirtualBox Host-Only Network #2:

   Verbindungsspezifisches DNS-Suffix:
   Verbindungslokale IPv6-Adresse  . : fe80::b0e6:32c8:fc0a:af52%25
   IPv4-Adresse  . . . . . . . . . . : 192.168.99.1
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . :
</code></pre>

<p>VirtualBox Adapters should not be a problem for that program, right?</p>

<p>============= </p>

<p>Just to compare it - running a Tomcat on port 8080 on my PC works fine, just like this:</p>

<pre><code>C:\&gt;netstat -na | find "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING
  TCP    [::]:8080              [::]:0                 LISTENING
</code></pre>

<p><a href="https://i.stack.imgur.com/E2Aw4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E2Aw4.png" alt="enter image description here"></a></p>

<p><strong><em>=========== Update 2 ===========</em></strong></p>

<p>I changed the hostname from <code>localhost</code> to the FQDN:</p>

<pre><code>_listener.Prefixes.Add("http://devml-nb01-81.mycompany.ch:8080/test/");
</code></pre>

<p>This way it works. </p>

<p><a href="https://i.stack.imgur.com/FlBvc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FlBvc.png" alt="enter image description here"></a></p>

<p>But why would it not work via <code>localhost</code> (such as Tomcat?) </p>

<pre><code>C:\&gt;ping localhost

Ping wird ausgeführt für devml-nb01-81.mycompany.ch [::1] mit 32 Bytes Daten:
Antwort von ::1: Zeit&lt;1ms
Antwort von ::1: Zeit&lt;1ms
Antwort von ::1: Zeit&lt;1ms
Antwort von ::1: Zeit&lt;1ms

Ping-Statistik für ::1:
    Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 0ms, Maximum = 0ms, Mittelwert = 0ms
</code></pre>