Simple Web Server: The format of the specified network name is invalid
Asked Answered
C

4

8

I'm having a problem with a simple web server that I am writing. I need to be able to connect to the server through localhost and IP. However, I am having problems connecting through IP. Here is my code:

private void start_button_Click(object sender, EventArgs e)
    {
        start_button.Text = "Listening...";

        HttpListener server = new HttpListener();

        server.Prefixes.Add("http://201.0.0.10:69/");
        server.Prefixes.Add("http://localhost:69/");

        server.Start();

        while (true)
        {
            HttpListenerContext context = server.GetContext();
            HttpListenerResponse response = context.Response;

            string page = Directory.GetCurrentDirectory() +
                context.Request.Url.LocalPath;

            if (page == string.Empty)
                page = page + "index.html";

            TextReader tr = new StreamReader(page);
            string msg = tr.ReadToEnd();


            byte[] buffer = Encoding.UTF8.GetBytes(msg);

            response.ContentLength64 = buffer.Length;
            Stream st = response.OutputStream;
            st.Write(buffer, 0, buffer.Length);

            context.Response.Close();
        }
    }

I keep getting this error: The format of the specified network name is invalid.

I know my problem lies in this bit:

server.Prefixes.Add("http://201.0.0.10:69/");

I can connect through localhost if I comment out this line.

Does anyone know what I could be doing wrong?


Okay, I got the IP adress working, but now I'm having a problem with this line:

if (page == string.Empty)
            page = page + "index.html";

For some reason, it's not adding index.html to the end.

Camouflage answered 30/9, 2013 at 13:40 Comment(4)
This error happens if you try to add prefix that does not correspond to any of your host's addresses. Does your machine actually have 201.0.0.10 address?Breaker
What I mean is: make sure you only use actual IP addresses assigned to your host. You can obtain them using System.Net.Dns.GetHostEntry("").AddressList.Breaker
Thanks! That worked! I was trying to use my public IP instead of one assigned to my computer. Now I'm having another problem though. For some reason, the if statement to add index.html to the end of the URL isn't working.Camouflage
Just before the if, you set page to CurentDirectory + LocalPath. How do you expect it to be empty? You could change it to something like if (context.Request.Url.LocalPath == "/") (browser adds the forward slash if no path is entered).Breaker
A
2

As well as setting the bindings in the application.config file you may need to set your system to listen for http from certain IP addresses by running this command:

netsh http add iplisten 201.0.0.10

You may also need to add localhost:

netsh http add iplisten 127.0.0.1

And as mentioned in other answers add these to the bindings file:

 <binding protocol="http" bindingInformation="*:69:201.0.0.10" /> 
 <binding protocol="http" bindingInformation="*:69:localhost" />
Aptitude answered 12/4, 2016 at 9:22 Comment(0)
J
1

The solution that worked for me was to add a binding in the applicationhost.config file.

This answer gives an example of where the binding information is located and how you can manually edit it.

In your case, the following binding info may fix your problem:

<bindings>
 <binding protocol="http" bindingInformation="*:69:localhost" />
 <binding protocol="http" bindingInformation="*:69:201.0.0.10" /> 
</bindings>
Jacqulynjactation answered 1/7, 2015 at 16:59 Comment(0)
I
0

The primary occurs when you have multiple IP Addresses or a defined system registry has one IP Address Bound to it.

It can be solved with some command in a command prompt (Admin rights) as seen here using:

netsh http add iplisten ipaddress=::

In the Registry Path, it is bound to one IP Address here.

Insubordinate answered 7/9, 2022 at 6:3 Comment(0)
P
0

I tried all other proposed solutions, but it didn't work for me. But this solution worked:

  1. Run command prompt (Admin rights)

  2. netsh http show iplisten

the list is displayed

  1. For each IP address displayed run

netsh http delete iplisten 192.168.2.3

netsh http delete iplisten 127.0.0.1

  1. netsh http add iplisten 0.0.0.0

  2. Execute appwiz.cpl

  3. locate IIS Express in the programs list and uninstall it

  4. Delete folder named IISExpress located in C:\Users[your_profile_name]\Documents

  5. restart your computer

  6. Download and install IIS Express

Pimiento answered 18/9, 2022 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.