Simple web server: format of the specified network name is not valid

I have 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 via 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 not valid.

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 on this line.

Does anyone know what I can do wrong?


Ok, I got the IP address, but now I have a problem with this line:

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

For some reason, it does not add index.html to the end.

+7
source share
2 answers

, , applicationhost.config.

, .

:

<bindings>
 <binding protocol="http" bindingInformation="*:69:localhost" />
 <binding protocol="http" bindingInformation="*:69:201.0.0.10" /> 
</bindings>
0

application.config, http IP-, :

netsh http add iplisten 201.0.0.10

localhost:

netsh http add iplisten 127.0.0.1

, :

 <binding protocol="http" bindingInformation="*:69:201.0.0.10" /> 
 <binding protocol="http" bindingInformation="*:69:localhost" />
0

All Articles