Activity Stream
48,167 MEMBERS
62832 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 7 of 7
  1.     
    #1
    Respected Developer

    Lightbulb [C#] Tiny Web Server (snippet)

    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 Code: 
    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
                    
    );
                }
            }
        }



    All that in 90 lines of code. C# .
    Hyperz Reviewed by Hyperz on . [C#] Tiny Web Server (snippet) 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. 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; Rating: 5

  2.   Sponsored Links

  3.     
    #2
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    + awesome multi-thread scaling

  4.     
    #3
    Banned
    hmm Interesting

  5.     
    #4
    Respected Developer
    Quote Originally Posted by jayfella View Post
    + awesome multi-thread scaling
    This example is single threaded .

  6.     
    #5
    Member
    Website's:
    GFXWebHosting.com WarezJobs.com
    thats is so cool Hyperz...

  7.     
    #6
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    Quote Originally Posted by Hyperz View Post
    This example is single threaded .
    Yeah i know, but not if you change like 3 lines.

  8.     
    #7
    Respected Developer
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Snippet of the Day
    By SplitIce in forum Web Development Area
    Replies: 41
    Last Post: 26th Aug 2012, 06:09 PM
  2. Amazing Art in tiny bottles!
    By glcon in forum General Discussion
    Replies: 6
    Last Post: 15th Dec 2011, 10:51 AM
  3. All Hail The Tiny $25 PC! Coming In November
    By doni in forum News & Current Events
    Replies: 0
    Last Post: 2nd Sep 2011, 05:07 PM
  4. where I can get Tiny paste or safelinking like script
    By MiTU? in forum Webmaster Discussion
    Replies: 3
    Last Post: 26th Jul 2011, 09:42 AM
  5. Plz Help To Add A Php Snippet Into My DLE Index !
    By JoomlaZ in forum Web Development Area
    Replies: 0
    Last Post: 7th Jul 2011, 01:18 PM

Tags for this Thread

BE SOCIAL