日期:2013-03-15  浏览次数:20398 次

个人珍藏,仅为存档.今天整理磁盘,没处放.扔BLOG.

看不懂,别问我.自己研究.

webserver.cs

namespace cnnbsun.webserver
{
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading ;


class MyWebServer
{

private TcpListener myListener ;
private int port = 8080 ; // 选者任何闲置端口

//开始兼听端口
//同时启动一个兼听进程
public MyWebServer()
{
try
{
//开始兼听端口
myListener = new TcpListener(port) ;
myListener.Start();
Console.WriteLine("Web Server Running... Press ^C to Stop...");
//同时启动一个兼听进程 'StartListen'
Thread th = new Thread(new ThreadStart(StartListen));
th.Start() ;

}
catch(Exception e)
{
Console.WriteLine("兼听端口时发生错误 :" +e.ToString());
}
}
public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
{

String sBuffer = "";

if (sMIMEHeader.Length == 0 )
{
sMIMEHeader = "text/html"; // 默认 text/html
}

sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
sBuffer = sBuffer + "Server: cx1193719-b\r\n";
sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";
sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n";

Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);

SendToBrowser( bSendData, ref mySocket);

Console.WriteLine("Total Bytes : " + iTotBytes.ToString());

}

public void SendToBrowser(String sData, ref Socket mySocket)
{
SendToBrowser (Encoding.ASCII.GetBytes(sData), ref mySocket);
}

public void SendToBrowser(Byte[] bSendData, ref Socket mySocket)
{
int numBytes = 0;

try
{
if (mySocket.Connected)
{
if (( numBytes = mySocket.Send(bSendData, bSendData.Length,0)) == -1)
Console.WriteLine("Socket Error cannot Send Packet");
else
{
Console.WriteLine("No. of bytes send {0}" , numBytes);
}
}
else
Console.WriteLine("连接失败....");
}
catch (Exception e)
{
Console.WriteLine("发生错误 : {0} ", e );

}
}
public static void Main()
{
MyWebServer MWS = new MyWebServer();
}
public void StartListen()
{

int iStartPos = 0;
String sRequest;
String sDirName;
String sRequestedFile;
String sErrorMessage;
String sLocalDir;
/////////////////////////////////////注意设定你自己的虚拟目录/////////////////////////////////////
String sMyWebServerRoot = "E:\\MyWebServerRoot\\"; //设置你的虚拟目录
//////////////////////////////////////////////////////////////////////////////////////////////////
String sPhysicalFilePath = "";
String sFormattedMessage = "";
String sResponse = "";


while(true)
{
//接受新连接
Socket mySocket = myListener.AcceptSocket() ;

Console.WriteLine ("Socket Type " +mySocket.SocketType );
if(mySocket.Connected)
{
Console.WriteLine("\nClient Connected!!\n==================\nCLient IP {0}\n",mySocket.RemoteEndPoint) ;

Byte[] bReceive = new Byte[1024] ;
int i = mySocket.Receive(bReceive,bReceive.Length,0) ;


//转换成字符串类型
string sBuffer = Encoding.ASCII.GetString(bReceive);


//只处理"get"请求类型
if (sBuffer.Substring(0,3) != "GET" )
{
Console.WriteLine("只处理get请求类型..");
mySocket.Close();
return;
}

// 查找 "HTTP" 的位置
iStartPos = sBuffer.IndexOf("HTTP",1);


string sHttpVersion = sBuffer.Substring(iStartPos,8);


// 得到请求类型和文件目录文件名
sRequest = sBuffer.Substring(0,iStartPos