Back to Top
WJunction

Register Now

Welcome Guest!  Register  
Go Back   WJunction - Webmaster Forum > Coding and Graphics > Development Area

Post Reply
 
Thread Tools Display Modes

Old 23rd Jun 2010, 06:11 AM   #1
Member
 
  • My Statistics
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 is offline   Quote
Liked by:
Old 23rd Jun 2010, 12:10 PM   #2
Member

jayfella's Avatar
 
Website(s):
extremecoderz.com
  • My Statistics
+ awesome multi-thread scaling
jayfella is offline   Quote
Old 23rd Jun 2010, 12:56 PM   #3
Banned
 
  • My Statistics
Send a message via MSN to BadLuckGuy
hmm Interesting :d
BadLuckGuy is offline   Quote
Old 23rd Jun 2010, 02:15 PM   #4
Member
 
  • My Statistics
Quote:
Originally Posted by jayfella View Post
+ awesome multi-thread scaling
This example is single threaded .
Hyperz is offline   Quote
Old 23rd Jun 2010, 02:46 PM   #5
Member

gfxguru's Avatar
 
Website(s):
GFXWebHosting.com WarezJobs.com
  • My Statistics
thats is so cool Hyperz...
gfxguru is offline   Quote
Old 23rd Jun 2010, 02:47 PM   #6
Member

jayfella's Avatar
 
Website(s):
extremecoderz.com
  • My Statistics
Quote:
Originally Posted by Hyperz View Post
This example is single threaded .
Yeah i know, but not if you change like 3 lines.
jayfella is offline   Quote
Old 24th Jun 2010, 01:19 PM   #7
Member
 
  • My Statistics
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.
Hyperz is offline   Reply
Post Reply

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz Help To Add A Php Snippet Into My DLE Index ! JoomlaZ Development Area 0 7th Jul 2011 01:18 PM
Facebook May Make Tiny Town a Data Center Mecca Daniel News & Current Events 3 30th May 2011 06:20 PM
Image Upload in php. Code snippet #2 SplitIce Tutorials and Guides 5 31st Oct 2009 07:40 AM
See real OOP (Snippet from Litewarez V2) Webmasters CP litewarez Tutorials and Guides 21 19th Sep 2009 03:59 PM
A Snippet from my latest project litewarez Tutorials and Guides 19 21st Jun 2009 05:17 PM


All times are GMT. The time now is 01:04 AM.