A simple HTTP server implemented in C #

Source: Internet
Author: User
Tags readline socket split thread
Server

Http.cs
----------------------------
Using System;
Using System.Collections;
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;

Class Httpprocessor {

Private Socket S;
Private BufferedStream BS;
Private StreamReader SR;
Private StreamWriter SW;
Private String method;
Private String URL;
Private String Protocol;
Private Hashtable Hashtable;

Public Httpprocessor (Socket s) {
THIS.S = s;
hashTable = new HashTable ();
}

public void process () {
NetworkStream ns = new NetworkStream (s, fileaccess.readwrite);
BS = new BufferedStream (NS);
sr = new StreamReader (BS);
SW = new StreamWriter (BS);
Parserequest ();
Readheaders ();
Writeurl ();
S.shutdown (Socketshutdown.sdboth);
Ns. Close ();
}

public void Parserequest () {
String request = Sr. ReadLine ();
String[] tokens = Request. Split (New char[]{'});
method = Tokens[0];
url = tokens[1];
protocol = tokens[2];
}

  public void Readheaders () {
    String line;
    while (line = Sr. ReadLine ())!= null && line!= ""} {
      string[] tokens = line. Split (New char[]{': '});
      String name = Tokens[0];
      String value = "";
      for (int i = 1; i < tokens. Length; i++) {
        value + = Tokens[i];
        if (I < tokens. LENGTH-1) tokens[i] = = ":";
     }
      Hashtable[name] = value;
   }
 }

  public void Writeurl () {
    try {
       FileStream fs = new FileStream (URL. Substring (1), FileMode.Open, FileAccess.Read);
      writesuccess ();
      BufferedStream BS2 = new BufferedStream (FS);
      byte[] bytes = new byte[4096];
      int read;
      while (read = BS2. Read (bytes, 0, bytes. Length))!= 0) {
        BS. Write (bytes, 0, read);
     }
      BS2. Close ();
   } catch (FileNotFoundException) {
      writefailure ();
      SW. WriteLine ("File not Found:" + URL);
   }
    SW. Flush ();
 }

public void writesuccess () {
Sw. WriteLine ("http/1.0 OK");
Sw. WriteLine ("Connection:close");
Sw. WriteLine ();
}

public void Writefailure () {
Sw. WriteLine ("http/1.0 404 File Not Found");
Sw. WriteLine ("Connection:close");
Sw. WriteLine ();
}
}

public class Httpserver {

// ============================================================
Data

protected int port;

// ============================================================
Constructor

Public Httpserver (): This (80) {
}

public httpserver (int port) {
This.port = port;
}

// ============================================================
Listener

public void Listen () {
Socket listener = new Socket (0, Sockettype.sockstream, protocoltype.prottcp);
IPAddress ipaddress = new IPAddress ("127.0.0.1");
IPEndPoint endpoint = new IPEndPoint (ipaddress, Port);
Listener. Bind (endpoint);
Listener. Blocking = true;
Listener. Listen (-1);
while (true) {
Socket s = listener. Accept ();
Httpprocessor processor = new Httpprocessor (s);
Thread thread = new Thread (new ThreadStart (processor.process));
Thread. Start ();
}
}

// ============================================================
Main

public static int Main (string[] args) {
Httpserver Httpserver;
if (args. GetLength (0) > 0) {
Httpserver = new Httpserver (args[0). ToUInt16 ());
} else {
Httpserver = new Httpserver ();
}
Thread thread = new Thread (new ThreadStart (Httpserver.listen));
Thread. Start ();
return 0;
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.