一個用C#實現的簡單http server (轉)
http.cs
----------------------------
using System;
using System.Collections;
using System.IO;
using System;
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.Soth);
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 200 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;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-989465/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#實現一個最簡單的HTTP伺服器C#HTTP伺服器
- 用java實現一個簡單的房屋管理程式。 (轉)Java
- 用 go 實現一個簡單的 mvcGoMVC
- 用java實現一個簡單的序列化的例子(轉)Java
- 用java實現一個簡單的序列化的例子 (轉)Java
- 一個簡單的選單按鈕的實現 (轉)
- 用原生http 實現簡單 Express 框架HTTPExpress框架
- Thrift使用入門(2) - 用Thrift實現一個簡單的Server/Client應用程式Serverclient
- React 實現一個簡單實用的 Form 元件ReactORM元件
- 在 Golang 中實現一個簡單的Http中介軟體GolangHTTP
- 用canvas實現一個簡單的畫板Canvas
- 用 Swift 實現一個簡單版 ReactSwiftReact
- 用java實現一個簡單的區塊鏈Java區塊鏈
- 用java實現一個簡單的計算器Java
- 簡單實用的《http小書》HTTP
- 試著用Proxy 實現一個簡單mvvmMVVM
- 自己用 Netty 實現一個簡單的 RPCNettyRPC
- php實現一個簡單的socketPHP
- 實現一個簡單的TomcatTomcat
- 實現一個簡單的 RESTful APIRESTAPI
- 實現一個簡單的模板引擎
- 實現一個簡單的 dd 庫
- 自己實現一個簡單的 PromisePromise
- php實現一個簡單的堆PHP
- Java實現一個簡單的BitArrayJava
- 簡單的實現一個原型鏈原型
- 開發一個簡單的 HTTP 伺服器應用HTTP伺服器
- 用node實現一個簡單的聊天室——websocket實踐Web
- 一個簡單實用的 vb 加密/解密演算法 (轉)加密解密演算法
- 實現一個簡單的 jQuery 的 APIjQueryAPI
- 實現自己的http serverHTTPServer
- 基於 Hyperf+ SQL Server 實現的一個簡單資料庫 curdSQLServer資料庫
- tcp 實現簡單http 問題TCPHTTP
- 實現一個簡單的MVVM(Compile)MVVMCompile
- 實現一個簡單的虛擬DOM
- JavaScript之實現一個簡單的VueJavaScriptVue
- 如何實現一個簡單的以太坊?
- 動手實現一個簡單的promisePromise