您好,登錄后才能下訂單哦!
這篇文章主要講解了“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();
}
}
}
}
}
感謝各位的閱讀,以上就是“C#怎么實現Web服務器功能”的內容了,經過本文的學習后,相信大家對C#怎么實現Web服務器功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。