C#实现Web服务器

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

学习C#时,经常会遇到Web服务器功能的程序设计问题,这里将介绍C#实现Web服务器功能的程序设计问题的解决方法。

C#实现Web服务器功能的程序设计

根据HTTP协议的作用原理,实现GET请求的Web服务器程序的方法如下:

创建TcpListener类对象,监听某端口(任意输入闲置端口 如:8080 )。等待、接受客户机连接到该端口,得到与客户机连接的socket;从与socket关联的输入流中读取一行客户机提交的请求信息,请求信息的格式 为:GET 路径/文件名 HTTP/1.0。从请求信息中获取请求类型。如果请求类型是GET,则从请求信息中获取所访问的HTML文件名。没有HTML文件名时,则以 index.html作为文件名;

如果HTML文件存在,则打开HTML文件,把HTTP头信息和HTML文件内容通过socket传回给Web浏览器,然后关闭文件。否则发送错误信息给Web浏览器;关闭与相应Web浏览器连接的socket字。

C#实现Web服务器功能的代码如下:

    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  
    }  
     
    sBuffersBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";  
    sBuffersBuffer = sBuffer + "Server: cx1193719-b\r\n";  
    sBuffersBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";  
    sBuffersBuffer = sBuffer + "Accept-Ranges: bytes\r\n";  
    sBuffersBuffer = 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 - 1);  
     
    sRequest.Replace("\\","/");  
     
    //如果结尾不是文件名也不是以"/"结尾则加"/"  
    if ((sRequest.IndexOf(".") <1) && (!sRequest.EndsWith("/")))  
    {  
    sRequestsRequest = sRequest + "/";  
    }  
     
    //得带请求文件名  
    iStartPos = sRequest.LastIndexOf("/") + 1;  
    sRequestsRequestedFile = sRequest.Substring(iStartPos);  
     
    //得到请求文件目录  
    sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/")-3);  
     
    //获取虚拟目录物理路径  
    sLocalDir = sMyWebServerRoot;  
     
    Console.WriteLine("请求文件目录 : " + sLocalDir);  
     
    if (sLocalDir.Length == 0 )  
    {  
    sErrorMessage = "<H2>Error!! Requested Directory does not exists</H2><Br>";  
    SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket);  
    SendToBrowser(sErrorMessage, ref mySocket);  
    mySocket.Close();  
    continue;  
    }  
     
    if (sRequestedFile.Length == 0 )  
    {  
    // 取得请求文件名  
    sRequestedFile = "index.html";  
    }  
     
     
    String sMimeType = "text/html";  
     
    sPhysicalFilePath = sLocalDir + sRequestedFile;  
    Console.WriteLine("请求文件: " + sPhysicalFilePath);  
     
    if (File.Exists(sPhysicalFilePath) == false)  
    {  
     
    sErrorMessage = "<H2>404 Error! File Does Not Exists...</H2>";  
    SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket);  
    SendToBrowser( sErrorMessage, ref mySocket);  
     
    Console.WriteLine(sFormattedMessage);  
    }  
     
    else  
    {  
    int iTotBytes=0;  
     
    sResponse ="";  
     
    FileStream fs = new 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)  
    {  
    sResponsesResponse = sResponse + Encoding.ASCII.GetString(bytes,0,read);  
     
    iTotBytesiTotBytes = iTotBytes + read;  
     
    }  
    reader.Close();  
    fs.Close();  
     
    SendHeader(sHttpVersion, sMimeType, iTotBytes, " 200 OK", ref mySocket);  
    SendToBrowser(bytes, ref mySocket);  
    //mySocket.Send(bytes, bytes.Length,0);  
    }  
    mySocket.Close();  
    }  
    }  
    }  
    }  
    } 

标签: web服务器 代码 服务器 服务器功能

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:php实现解析网页并下载图片到本地

下一篇: php实现随机点名程序