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 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;
}
}
C# 两种方法实现HTTP协议迷你服务器
本文以两种稍微有差别的方式用C#语言实现HTTP协议的服务器类,之所以写这些,也是为了自己能更深刻了解HTTP底层运作。
要完成高性能的Web服务功能,通常都是需要写入到服务,如IIS,Apache Tomcat,但是众所周知的Web服务器配置的复杂性,如果我们只是需要一些简单的功能,安装这些组件看起来就没多大必要。我们需要的是一个简单的HTTP类,可以很容易地嵌入到简单的Web请求的服务,加到自己的程序里。
实现方法一:
.net框架下有一个简单但很强大的类HttpListener。这个类几行代码就能完成一个简单的服务器功能。虽然以下内容在实际运行中几乎毫无价值,但是也不失为理解HTTP请求过程的细节原理的好途径。
HttpListener httpListener = new HttpListener(); httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; httpListener.Prefixes.Add("http://localhost:8080/"); httpListener.Start(); new Thread(new ThreadStart(delegate { while (true) { HttpListenerContext httpListenerContext = httpListener.GetContext(); httpListenerContext.Response.StatusCode = 200; using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream)) { writer.WriteLine("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>测试服务器</title></head><body>"); writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello</p></div>"); writer.WriteLine("<ul>"); writer.WriteLine("</ul>"); writer.WriteLine("</body></html>"); } } })).Start();
如果你运用的好,举一反三的话,这样一个简单的类可能会收到意想不到的效果,但是由于HttpListener这个类对底层的完美封装,导致对协议的控制失去灵活性,因此我想大型服务器程序里肯定不会用这个类去实现。因此如果必要的话,自己用Tcp协议再去封装一个类,以便更好的控制服务运行状态。
实现方法二:
这个方法是一个老外分享的,虽然文件内容看起来很乱,其实逻辑很强很有条理。让我们来分析一下实现过程:
通过子类化HttpServer和两个抽象方法handlegetrequest和handlepostrequest提供实现…
public class MyHttpServer : HttpServer { public MyHttpServer(int port) : base(port) { } public override void handleGETRequest(HttpProcessor p) { Console.WriteLine("request: {0}", p.http_url); p.writeSuccess(); p.outputStream.WriteLine("<html><body><h1>test server</h1>"); p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString()); p.outputStream.WriteLine("url : {0}", p.http_url); p.outputStream.WriteLine("<form method=post action=/form>"); p.outputStream.WriteLine("<input type=text name=foo value=foovalue>"); p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>"); p.outputStream.WriteLine("</form>"); } public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) { Console.WriteLine("POST request: {0}", p.http_url); string data = inputData.ReadToEnd(); p.outputStream.WriteLine("<html><body><h1>test server</h1>"); p.outputStream.WriteLine("<a href=/test>return</a><p>"); p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data); } }
如果你能够顺利编译和运行附件中的项目,就你应该能够用Web浏览器输入Http://localhost:8080/,打开就可以看上面的简单的HTML页面渲染。让我们看看怎么具体是怎么实现的吧~!
这个简单的Web服务器可以分解为两个部分。HttpServer类开启了一个指定输入端口的TcpListener实例,使用accepttcpclient()用于循环处理传入的TCP连接请求。这是处理传入的TCP连接的第一步。当传入的请求到达已知的指定端口,这个接受过程会创建一个新的服务器与客户端端口配对,用于服务器语言客户端的连接通信。
public abstract class HttpServer { protected int port; TcpListener listener; bool is_active = true; public HttpServer(int port) { this.port = port; } public void listen() { listener = new TcpListener(port); listener.Start(); while (is_active) { TcpClient s = listener.AcceptTcpClient(); HttpProcessor processor = new HttpProcessor(s, this); Thread thread = new Thread(new ThreadStart(processor.process)); thread.Start(); Thread.Sleep(1); } } public abstract void handleGETRequest(HttpProcessor p); public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData); }
这样一些介绍方式可能会让人产生一头雾水的感觉,或许直观地看代码或调试示例源代码程序可能会更容易理解一些。下面就把源码贴上来弓大家参考,希望能对大家有所帮助!