Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I just created a server using the following code.It just printing the message.Now i need to run a html page in this server.How to do that?My codes are shown below.
C#
static void Main(string[] args)
       {
           HttpListener listner = null;
           try
           {
               listner= new HttpListener();
               listner.Prefixes.Add("http://localhost:1300/simpleserver/");
               listner.Start();
               while(true)
               {
                Console.WriteLine("Waiting....");
                   HttpListenerContext context =listner.GetContext();
                   string msg = "Welcome to the server";
                   context.Response.ContentLength64=Encoding.UTF8.GetByteCount(msg);
                   context.Response.StatusCode=(int)HttpStatusCode.OK;
                   using(Stream stream=context.Response.OutputStream)
                   {
                       using(StreamWriter writer =new StreamWriter(stream))
                       {
                           writer.Write(msg);

                       }
                   }
                   Console.WriteLine("Message Sent.....");
               }
           }
           catch(WebException e)
           {
               Console.WriteLine(e.Status);
           }
       }


Thanks
Posted

HTML files are not seen/rendered in server, they are rendered in a Web browser or similar application reading and interpreting HTML. You can choose form a set of major browsers available free of charge. :-)

You know, your code really relevant to some server, I can see it by HttpListener, but as your question creates a doubt that you understand what happens of server and client sides, I hardly can believe that you really created an HTTP server… :-)

—SA
 
Share this answer
 
You have to parse the incoming request and respond by sending the html contents of the associated file.

Take a look at the following links :

A Simple HTTP Server
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900