|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
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++编译器在上面直接运行。 |
|