仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 825|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET编程:一个用C#完成的复杂http server(转)

[复制链接]
简单生活 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:50:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
对于new隐藏成员的作用,往往是出于使用了一个第三方类库,而你又无法获得这个类库的源代码,当你继承这个类库的某个类时,你需要重新实现其中的一个方法,而又需要与父类中的函数使用同样的函数,这是就需要在自定义的子类中把那个同名函数(或成员)加上new标记,从而隐藏父类中同名的成员。http.cs
----------------------------
usingSystem;
usingSystem.Collections;
usingSystem.IO;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Threading;

classHttpProcessor{

privateSockets;
privateBufferedStreambs;
privateStreamReadersr;
privateStreamWritersw;
privateStringmethod;
privateStringurl;
privateStringprotocol;
privateHashtablehashTable;

publicHttpProcessor(Sockets){
this.s=s;
hashTable=newHashtable();
}

publicvoidprocess(){
NetworkStreamns=newNetworkStream(s,FileAccess.ReadWrite);
bs=newBufferedStream(ns);
sr=newStreamReader(bs);
sw=newStreamWriter(bs);
parseRequest();
readHeaders();
writeURL();
s.Shutdown(SocketShutdown.SdBoth);
ns.Close();
}

publicvoidparseRequest(){
Stringrequest=sr.ReadLine();
string[]tokens=request.Split(newchar[]{});
method=tokens[0];
url=tokens[1];
protocol=tokens[2];
}

publicvoidreadHeaders(){
Stringline;
while((line=sr.ReadLine())!=null&&line!=""){
string[]tokens=line.Split(newchar[]{:});
Stringname=tokens[0];
Stringvalue="";
for(inti=1;i<tokens.Length;i++){
value+=tokens;
if(i<tokens.Length-1)tokens+=":";
}
hashTable[name]=value;
}
}

publicvoidwriteURL(){
try{
FileStreamfs=newFileStream(url.Substring(1),FileMode.Open,FileAccess.Read);
writeSuccess();
BufferedStreambs2=newBufferedStream(fs);
byte[]bytes=newbyte[4096];
intread;
while((read=bs2.Read(bytes,0,bytes.Length))!=0){
bs.Write(bytes,0,read);
}
bs2.Close();
}catch(FileNotFoundException){
writeFailure();
sw.WriteLine("Filenotfound:"+url);
}
sw.Flush();
}

publicvoidwriteSuccess(){
sw.WriteLine("HTTP/1.0200OK");
sw.WriteLine("Connection:close");
sw.WriteLine();
}

publicvoidwriteFailure(){
sw.WriteLine("HTTP/1.0404Filenotfound");
sw.WriteLine("Connection:close");
sw.WriteLine();
}
}

publicclassHttpServer{

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

protectedintport;

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

publicHttpServer():this(80){
}

publicHttpServer(intport){
this.port=port;
}

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

publicvoidlisten(){
Socketlistener=newSocket(0,SocketType.SockStream,ProtocolType.ProtTCP);
IPAddressipaddress=newIPAddress("127.0.0.1");
IPEndPointendpoint=newIPEndPoint(ipaddress,port);
listener.Bind(endpoint);
listener.Blocking=true;
listener.Listen(-1);
while(true){
Sockets=listener.Accept();
HttpProcessorprocessor=newHttpProcessor(s);
Threadthread=newThread(newThreadStart(processor.process));
thread.Start();
}
}

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

publicstaticintMain(String[]args){
HttpServerhttpServer;
if(args.GetLength(0)>0){
httpServer=newHttpServer(args[0].ToUInt16());
}else{
httpServer=newHttpServer();
}
Threadthread=newThread(newThreadStart(httpServer.listen));
thread.Start();
return0;
}
}



[img=1border=0style=,1src=]http://www.ckuyun.com/[/img]

捆绑编译器。用户不需要受制于厂家,自己就能将程序在新平台上编译运行。除了牛B轰轰的linux,估计也没有系统捆绑c/c++的编译器,而且许多新平台都无法支持复杂的c/c++编译器在上面直接运行。
小女巫 该用户已被删除
沙发
发表于 2015-1-20 05:30:27 | 只看该作者
最强的技术支持WebService,而且有.NET的所有library做后盾。而且ASP.NET在.NET3.5中还有微软专门为AJAX开发的功能--ASP.NETAJAX。
老尸 该用户已被删除
板凳
发表于 2015-1-28 18:51:28 | 只看该作者
asp.net空间的支持有:ASP.NET1.1/虚拟目录/MicrosoftFrontPage2000扩展/CDONTS,同时他的网站上也提供了Asp.net的使用详解和程序源代码,相信对使用ASP.NET编程的程序员来说会非常有用哦!
因胸联盟 该用户已被删除
地板
发表于 2015-2-5 21:39:33 | 只看该作者
JSP/Servlet虽然在国内目前的应用并不广泛,但是其前途不可限量。
深爱那片海 该用户已被删除
5#
发表于 2015-2-13 17:14:09 | 只看该作者
ASP.net的速度是ASP不能比拟的。ASP.net是编译语言,所以,当第一次加载的时候,它会把所有的程序进行编译(其中包括worker进程,还有对语法进行编译,形成一个程序集),当程序编译后,执行速度几乎为0。
小魔女 该用户已被删除
6#
发表于 2015-3-4 00:10:23 | 只看该作者
ASP.Net和ASP的最大区别在于编程思维的转换,而不仅仅在于功能的增强。ASP使用VBS/JS这样的脚本语言混合html来编程,而那些脚本语言属于弱类型、面向结构的编程语言,而非面向对象。
变相怪杰 该用户已被删除
7#
发表于 2015-3-11 14:50:42 | 只看该作者
如今主流的Web服务器软件主要由IIS或Apache组成。IIS支持ASP且只能运行在Windows平台下,Apache支持PHP,CGI,JSP且可运行于多种平台,虽然Apache是世界使用排名第一的Web服务器平台。
飘飘悠悠 该用户已被删除
8#
发表于 2015-3-18 20:50:12 | 只看该作者
我觉得什么语言,精通就好,你要做的就是比其他80%的人都厉害,你就能得到只有20%的人才能得到的高薪。
简单生活 该用户已被删除
9#
 楼主| 发表于 2015-3-26 14:01:36 | 只看该作者
在asp.net虚拟主机的服务提供商中,目前首推的是CNNIC的其中一家域名注册机构---时代互联(www.now.net.cn),他们早在2001年微软刚推出Asp.net时就推出了对应的Asp.net虚拟主机了,经笔者的使用测试,他提供的Asp.net性能非常的稳定,版本也会定期的更新,目前他的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-4 22:42

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表