一个用C#实现的简单http 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 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;
  }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值