基于C#動(dòng)手實(shí)現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server
前言
最近在學(xué)習(xí)網(wǎng)絡(luò)原理,突然萌發(fā)出自己實(shí)現(xiàn)一個(gè)網(wǎng)絡(luò)服務(wù)器的想法,并且由于第三代小白機(jī)器人的開(kāi)發(fā)需要,我把之前使用python、PHP寫(xiě)的那部分代碼都遷移到了C#(別問(wèn)我為什么這么喜歡C#),之前使用PHP就是用來(lái)處理網(wǎng)絡(luò)請(qǐng)求的,現(xiàn)在遷移到C#了,而Linux系統(tǒng)上并沒(méi)有IIS服務(wù)器,自然不能使用ASP.Net,所以這個(gè)時(shí)候自己實(shí)現(xiàn)一個(gè)功能簡(jiǎn)單的網(wǎng)絡(luò)服務(wù)器就恰到好處地解決這些問(wèn)題了。
基本原理
Web Server在一個(gè)B/S架構(gòu)系統(tǒng)中起到的作用不僅多而且相當(dāng)重要,Web開(kāi)發(fā)者大部分時(shí)候并不需要了解它的詳細(xì)工作機(jī)制。雖然不同的Web Server可能功能并不完全一樣,但是以下三個(gè)功能幾乎是所有Web Server必須具備的:
接收來(lái)自瀏覽器端的HTTP請(qǐng)求
將請(qǐng)求轉(zhuǎn)發(fā)給指定Web站點(diǎn)程序(后者由Web開(kāi)發(fā)者編寫(xiě),負(fù)責(zé)處理請(qǐng)求)
向?yàn)g覽器發(fā)送請(qǐng)求處理結(jié)果
下圖顯示W(wǎng)eb Server在整個(gè)Web架構(gòu)系統(tǒng)中所處的重要位置:
如上圖,Web Server起到了一個(gè)“承上啟下”的作用(雖然并沒(méi)有“上下”之分),它負(fù)責(zé)連接用戶和Web站點(diǎn)。
每個(gè)網(wǎng)站就像一個(gè)個(gè)“插件”,只要網(wǎng)站開(kāi)發(fā)過(guò)程中遵循了Web Server提出的規(guī)則,那么該網(wǎng)站就可以“插”在Web Server上,我們便可以通過(guò)瀏覽器訪問(wèn)網(wǎng)站。
太長(zhǎng)不看版原理
瀏覽器想要拿到哪個(gè)文件(html、css、js、image)就和服務(wù)器發(fā)請(qǐng)求信息說(shuō)我要這個(gè)文件,然后服務(wù)器檢查請(qǐng)求合不合法,如果合法就把文件數(shù)據(jù)傳回給瀏覽器,這樣瀏覽器就可以把網(wǎng)站顯示出來(lái)了。(一個(gè)網(wǎng)站一般會(huì)包含n多個(gè)文件)
話不多說(shuō),直接上代碼
在C#中有兩種方法可以簡(jiǎn)單實(shí)現(xiàn)Web服務(wù)器,分別是直接使用Socket和使用封裝好的HttpListener。
因?yàn)楹笳弑容^方便一些,所以我選擇使用后者。
這是最簡(jiǎn)單的實(shí)現(xiàn)一個(gè)網(wǎng)絡(luò)服務(wù)器,可以處理瀏覽器發(fā)過(guò)來(lái)的請(qǐng)求,然后將指定的字符串內(nèi)容返回。
class Program { static void Main(string[] args) { string port = "8080"; HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add(string.Format("http://+:{0}/", port)); httpListener.Start(); httpListener.BeginGetContext(new AsyncCallback(GetContext), httpListener); //開(kāi)始異步接收request請(qǐng)求 Console.WriteLine("監(jiān)聽(tīng)端口:" + port); Console.Read(); } static void GetContext(IAsyncResult ar) { HttpListener httpListener = ar.AsyncState as HttpListener; HttpListenerContext context = httpListener.EndGetContext(ar); //接收到的請(qǐng)求context(一個(gè)環(huán)境封裝體) httpListener.BeginGetContext(new AsyncCallback(GetContext), httpListener); //開(kāi)始 第二次 異步接收request請(qǐng)求 HttpListenerRequest request = context.Request; //接收的request數(shù)據(jù) HttpListenerResponse response = context.Response; //用來(lái)向客戶端發(fā)送回復(fù) response.ContentType = "html"; response.ContentEncoding = Encoding.UTF8; using (Stream output = response.OutputStream) //發(fā)送回復(fù) { byte[] buffer = Encoding.UTF8.GetBytes("要返回的內(nèi)容"); output.Write(buffer, 0, buffer.Length); } } }
這個(gè)簡(jiǎn)單的代碼已經(jīng)可以實(shí)現(xiàn)用于小白機(jī)器人的網(wǎng)絡(luò)請(qǐng)求處理了,因?yàn)榇笾轮挥玫紾ET和POST兩種HTTP方法,只需要在GetContext方法里判斷GET、POST方法,然后分別給出響應(yīng)就可以了。
但是我們的目的是開(kāi)發(fā)一個(gè)真正的網(wǎng)絡(luò)服務(wù)器,當(dāng)然不能只滿足于這樣一個(gè)專用的服務(wù)器,我們要的是可以提供網(wǎng)頁(yè)服務(wù)的服務(wù)器。
那就繼續(xù)吧。
根據(jù)我的研究,提供網(wǎng)頁(yè)訪問(wèn)服務(wù)的服務(wù)器做起來(lái)確實(shí)有一點(diǎn)麻煩,因?yàn)樾枰幚淼臇|西很多。需要根據(jù)瀏覽器請(qǐng)求的不同文件給出不同響應(yīng),處理Cookies,還要處理編碼,還有各種出錯(cuò)的處理。
首先我們要確定一下我們的服務(wù)器要提供哪些文件的訪問(wèn)服務(wù)。
這里我用一個(gè)字典結(jié)構(gòu)來(lái)保存。
/// <summary> /// MIME類型 /// </summary> public Dictionary<string, string> MIME_Type = new Dictionary<string, string>() { { "htm", "text/html" }, { "html", "text/html" }, { "php", "text/html" }, { "xml", "text/xml" }, { "json", "application/json" }, { "txt", "text/plain" }, { "js", "application/x-javascript" }, { "css", "text/css" }, { "bmp", "image/bmp" }, { "ico", "image/ico" }, { "png", "image/png" }, { "gif", "image/gif" }, { "jpg", "image/jpeg" }, { "jpeg", "image/jpeg" }, { "webp", "image/webp" }, { "zip", "application/zip"}, { "*", "*/*" } };
劇透一下:其中有PHP類型是我們后面要使用CGI接入的方式使我們的服務(wù)器支持PHP。
我在QFramework中封裝了一個(gè)QHttpWebServer模塊,這是其中的啟動(dòng)代碼。
/// <summary> /// 啟動(dòng)本地網(wǎng)頁(yè)服務(wù)器 /// </summary> /// <param name="webroot">網(wǎng)站根目錄</param> /// <returns></returns> public bool Start(string webroot) { //觸發(fā)事件 if (OnServerStart != null) OnServerStart(httpListener); WebRoot = webroot; try { //監(jiān)聽(tīng)端口 httpListener.Prefixes.Add("http://+:" + port.ToString() + "/"); httpListener.Start(); httpListener.BeginGetContext(new AsyncCallback(onWebResponse), httpListener); //開(kāi)始異步接收request請(qǐng)求 } catch (Exception ex) { Qdb.Error(ex.Message, QDebugErrorType.Error, "Start"); return false; } return true; }
現(xiàn)在把網(wǎng)頁(yè)服務(wù)器的核心處理代碼貼出來(lái)。
這個(gè)代碼只是做了基本的處理,對(duì)于網(wǎng)站的主頁(yè)只做了html后綴的識(shí)別。
后來(lái)我在QFramework中封裝的模塊做了更多的細(xì)節(jié)處理。
/// <summary> /// 網(wǎng)頁(yè)服務(wù)器相應(yīng)處理 /// </summary> /// <param name="ar"></param> private void onWebResponse(IAsyncResult ar) { byte[] responseByte = null; //響應(yīng)數(shù)據(jù) HttpListener httpListener = ar.AsyncState as HttpListener; HttpListenerContext context = httpListener.EndGetContext(ar); //接收到的請(qǐng)求context(一個(gè)環(huán)境封裝體) httpListener.BeginGetContext(new AsyncCallback(onWebResponse), httpListener); //開(kāi)始 第二次 異步接收request請(qǐng)求 //觸發(fā)事件 if (OnGetRawContext != null) OnGetRawContext(context); HttpListenerRequest request = context.Request; //接收的request數(shù)據(jù) HttpListenerResponse response = context.Response; //用來(lái)向客戶端發(fā)送回復(fù) //觸發(fā)事件 if (OnGetRequest != null) OnGetRequest(request, response); if (rawUrl == "" || rawUrl == "/") //單純輸入域名或主機(jī)IP地址 fileName = WebRoot + @"\index.html"; else if (rawUrl.IndexOf('.') == -1) //不帶擴(kuò)展名,理解為文件夾 fileName = WebRoot + @"\" + rawUrl.SubString(1) + @"\index.html"; else { int fileNameEnd = rawUrl.IndexOf('?'); if (fileNameEnd > -1) fileName = rawUrl.Substring(1, fileNameEnd - 1); fileName = WebRoot + @"\" + rawUrl.Substring(1); } //處理請(qǐng)求文件名的后綴 string fileExt = Path.GetExtension(fileName).Substring(1); if (!File.Exists(fileName)) { responseByte = Encoding.UTF8.GetBytes("404 Not Found!"); response.StatusCode = (int)HttpStatusCode.NotFound; } else { try { responseByte = File.ReadAllBytes(fileName); response.StatusCode = (int)HttpStatusCode.OK; } catch (Exception ex) { Qdb.Error(ex.Message, QDebugErrorType.Error, "onWebResponse"); response.StatusCode = (int)HttpStatusCode.InternalServerError; } } if (MIME_Type.ContainsKey(fileExt)) response.ContentType = MIME_Type[fileExt]; else response.ContentType = MIME_Type["*"]; response.Cookies = request.Cookies; //處理Cookies response.ContentEncoding = Encoding.UTF8; using (Stream output = response.OutputStream) //發(fā)送回復(fù) { try { output.Write(responseByte, 0, responseByte.Length); } catch (Exception ex) { Qdb.Error(ex.Message, QDebugErrorType.Error, "onWebResponse"); response.StatusCode = (int)HttpStatusCode.InternalServerError; } } }
這樣就可以提供基本的網(wǎng)頁(yè)訪問(wèn)了,經(jīng)過(guò)測(cè)試,使用Bootstrap,Pure等前端框架的網(wǎng)頁(yè)都可以完美訪問(wèn),性能方面一般般。(在QFramework的封裝中我做了一點(diǎn)性能優(yōu)化,有一點(diǎn)提升)我覺(jué)得要在性能方面做提升還是要在多線程處理這方面做優(yōu)化,由于篇幅關(guān)系,就不把多線程版本的代碼貼出來(lái)了。
接下來(lái)我們還要實(shí)現(xiàn)服務(wù)器的PHP支持。
首先定義兩個(gè)字段。
/// <summary> /// 是否開(kāi)啟PHP功能 /// </summary> public bool PHP_CGI_Enabled = true; /// <summary> /// PHP執(zhí)行文件路徑 /// </summary> public string PHP_CGI_Path = "php-cgi"; 接下來(lái)在網(wǎng)頁(yè)服務(wù)的核心代碼里做PHP支持的處理。 //PHP處理 string phpCgiOutput = ""; Action phpProc = new Action(() => { try { string argStr = ""; if (request.HttpMethod == "GET") { if (rawUrl.IndexOf('?') > -1) argStr = rawUrl.Substring(rawUrl.IndexOf('?')); } else if (request.HttpMethod == "POST") { using (StreamReader reader = new StreamReader(request.InputStream)) { argStr = reader.ReadToEnd(); } } Process p = new Process(); p.StartInfo.CreateNoWindow = false; //不顯示窗口 p.StartInfo.RedirectStandardOutput = true; //重定向輸出 p.StartInfo.RedirectStandardInput = false; //重定向輸入 p.StartInfo.UseShellExecute = false; //是否指定操作系統(tǒng)外殼進(jìn)程啟動(dòng)程序 p.StartInfo.FileName = PHP_CGI_Path; p.StartInfo.Arguments = string.Format("-q -f {0} {1}", fileName, argStr); p.Start(); StreamReader sr = p.StandardOutput; while (!sr.EndOfStream) { phpCgiOutput += sr.ReadLine() + Environment.NewLine; } responseByte = sr.CurrentEncoding.GetBytes(phpCgiOutput); } catch (Exception ex) { Qdb.Error(ex.Message, QDebugErrorType.Error, "onWebResponse->phpProc"); response.StatusCode = (int)HttpStatusCode.InternalServerError; } }); if (fileExt == "php" && PHP_CGI_Enabled) { phpProc(); } else { if (!File.Exists(fileName)) { responseByte = Encoding.UTF8.GetBytes("404 Not Found!"); response.StatusCode = (int)HttpStatusCode.NotFound; } else { try { responseByte = File.ReadAllBytes(fileName); response.StatusCode = (int)HttpStatusCode.OK; } catch (Exception ex) { Qdb.Error(ex.Message, QDebugErrorType.Error, "onWebResponse"); response.StatusCode = (int)HttpStatusCode.InternalServerError; } } }
這樣就實(shí)現(xiàn)了基于PHP-CGI的PHP支持了,經(jīng)過(guò)測(cè)試,基本的php頁(yè)面都可以支持,但是需要使用curl和xml這類擴(kuò)展的暫時(shí)還沒(méi)辦法。需要做更多的工作。
接下來(lái)我會(huì)給服務(wù)器做一個(gè)GUI界面,供大家測(cè)試。
同時(shí)也會(huì)把QFramework框架發(fā)布,有興趣的可以使用基于QFramework的服務(wù)器封裝。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#使用SqlServer作為日志數(shù)據(jù)庫(kù)的設(shè)計(jì)與實(shí)現(xiàn)
- SQL Server存儲(chǔ)過(guò)程在C#中調(diào)用的簡(jiǎn)單實(shí)現(xiàn)方法
- C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
- C#使用SqlConnection連接到SQL Server的代碼示例
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例
- C#連接SQL Server的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法
- C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼
- C#實(shí)現(xiàn)的sqlserver操作類實(shí)例
- C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法
- C#創(chuàng)建一個(gè)小型Web Server(Socket實(shí)現(xiàn))
- C#實(shí)現(xiàn) Server-sent Events的步驟
相關(guān)文章
C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?
這篇文章主要介紹了C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開(kāi)介紹,需要的朋友可以參考一下2022-08-08c# volatile 關(guān)鍵字的拾遺補(bǔ)漏
這篇文章主要介紹了c# volatile 關(guān)鍵字的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#的相關(guān)知識(shí),感興趣的朋友可以了解下2020-10-10