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.
201.0.0.10
address? – BreakerSystem.Net.Dns.GetHostEntry("").AddressList
. – Breakerif
, you setpage
toCurentDirectory
+LocalPath
. How do you expect it to be empty? You could change it to something likeif (context.Request.Url.LocalPath == "/")
(browser adds the forward slash if no path is entered). – Breaker