Question

I created a httplistener and it works only with localhost as a prefix. It show an error if I change it to a remote server ip.

Here's the code:

 HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://*:8080/");  // I need localhost replaced with remote ip
        listener.Start();
        while (true)
        {
            // Wait for a client request:
            HttpListenerContext context = listener.GetContext();

            // Respond to the request:
            string msg = "You asked for: " + context.Request.RawUrl;
            context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);
            context.Response.StatusCode = (int)HttpStatusCode.OK;

            using (Stream s = context.Response.OutputStream)
            using (StreamWriter writer = new StreamWriter(s))
                writer.Write(msg);
        }
        listener.Stop();

THis is the web client:

   using (WebClient wc = new WebClient())        // Make a client request.
                    wc.DownloadString
                      ("http://"my public ip"(on  my router):8080/lg.txt"); //port forwarding is set
                MessageBox.Show("received");

It works only if I change "my public ip" to my local ip Any ideas?

Was it helpful?

Solution

Is your Windows Firewall on? Try disabling it and see if that helps. You may need to actually disable the Windows Firewall service (in your local services list).

OTHER TIPS

Firstly, check the firewall. Perhaps more likely, though, is http.sys, which is used by HttpListener - you must make the prefix available to the account via "netsh". You can check this by running as an elevated admin account briefly - If it works it is probably a http.sys permissions issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top