1. Hyperz

    Hyperz Well-Known Member Respected

    Feb 8, 2009
    2,183
    This is one of those examples that really gives you an idea just how much dev time the .NET platform saves you. This is a extremely basic web server.

    PHP:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    using c System.Console;


    namespace 
    Hyperz.BasicWebServer
    {
        public class 
    Program
        
    {
            private static 
    String address;
            private static 
    Thread listenThread;
            private static 
    HttpListener listener;

            public static 
    void Main(string[] args)
            {
                
    c.WriteLine("[{0:HH:mm}] Initializing"DateTime.Now);

                
    // the address we want to listen on
                
    address "http://127.0.0.1:80/";

                
    // setup thread
                
    listenThread = new Thread(Worker);
                
    listenThread.IsBackground true;
                
    listenThread.Priority ThreadPriority.Normal;

                
    // setup listener
                
    listener = new HttpListener();
                
    listener.Prefixes.Add(address);

                
    // Gogogo
                
    listenThread.Start(null);

                
    // prevent the console window from closing
                
    while (truec.ReadKey(true);
            }

            private static 
    void Worker(object state)
            {
                
    // start listening
                
    listener.Start();

                
    c.WriteLine("[{0:HH:mm}] Running"DateTime.Now);

                
    // request -> response loop
                
    while (true)
                {
                    
    HttpListenerContext context listener.GetContext();
                    
    HttpListenerRequest request context.Request;
                    
                    
    c.WriteLine(
                        
    "[{0:HH:mm}] Request received from {1}",
                        
    DateTime.Now,
                        
    request.LocalEndPoint.Address
                    
    );

                    
    /* respond to the request.
                     * in this case it'll show "Server appears to be working".
                     * regardless of what file/path was requested.
                     */
                    
    using (HttpListenerResponse response context.Response)
                    {
                        
    string html "<b>Server appears to be working!</b>";
                        
    byte[] data Encoding.UTF8.GetBytes(html);

                        
    response.ContentType "text/html";
                        
    response.ContentLength64 data.Length;

                        
    using (Stream output response.OutputStream)
                        {
                            
    output.Write(data0data.Length);
                        }
                    }

                    
    c.WriteLine(
                        
    "[{0:HH:mm}] Handled request for {1}",
                        
    DateTime.Now,
                        
    request.LocalEndPoint.Address
                    
    );
                }
            }
        }
    }
    [slide]http://www.cubeupload.com/files/8a200csws.png[/slide]

    All that in 90 lines of code. C# <3.
     
  2. jayfella

    jayfella Well-Known Member Member

    Mar 25, 2009
    1,592
    + awesome multi-thread scaling <3
     
  3. BadLuckGuy

    BadLuckGuy Banned Banned

    Jun 17, 2010
    69
    hmm Interesting :d
     
  4. Hyperz

    Hyperz Well-Known Member Respected

    Feb 8, 2009
    2,183
    This example is single threaded :unsure:.
     
  5. gfxguru

    gfxguru New Member Member

    Jul 9, 2009
    951
    thats is so cool Hyperz...
     
  6. jayfella

    jayfella Well-Known Member Member

    Mar 25, 2009
    1,592
    Yeah i know, but not if you change like 3 lines.
     
  7. Hyperz

    Hyperz Well-Known Member Respected

    Feb 8, 2009
    2,183
    It would be a bit more than 3 lines lol. But if some1 is genuinely interested in this I can write an improved version which also handles file requests.
     

Share This Page