用C# Builder实现Web服务器
p;FileStream(sPhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader reader = new BinaryReader(fs);
byte[] bytes = new byte[fs.Length];
int read;
while((read = reader.Read(bytes, 0, bytes.Length)) != 0)
{
sResponse = sResponse + Encoding.ASCII.GetString(bytes,0,read);
iTotBytes = iTotBytes + read;
}
reader.Close();
fs.Close();
SendHeader(sHttpVersion, sMimeType, iTotBytes, " 200 OK", ref mySocket);
SendToBrowser(bytes, ref mySocket);
}
mySocket.Close();
}
}
}
}
}
将文件编译成EXE文件,就实现了简单的WEB服务器功能!设定一个虚拟目录,进行测试!
在C:\Inetpub\wwwroot目录中新一HTML文件index.html,如下:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb2312">
<title>测试</title>
</head>
<body>
<h1>C#Builder开发的WebServer</h1>
<p>测试!!!!!</p>
</body>
</html>按F9运行程序,如果您的端口808没有占用的话,就会看到下面的窗口
screen.width-430)this.width=screen.width-430" align=center border=0>
(图一)
打开浏览器,输入http://localhost:808
screen.width-430)this.width=screen.width-430" align=center border=0>
(图二)
四、结束语
到这里,一个简单的Web服务器就做好了,如有兴趣,自己再改改增强一下它的功能吧。在C# Builder 1.0 个人测试版+WIN2000环境调试通过。源码可以到我的网站下载 http://yousoft.hi.com.cn
上一页 [1] [2] [3] [4]
进入问吧