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;
}
}