첫 번째 질문은 유감입니다. 두 번째 시도. C# 및 System.Net 라이브러리를 사용하여 웹 서버 (또는 응답자)를 만들었습니다. 다음은 서버 공용 변수 : 내 듣는 방법에서웹 서버에서 하위 디렉토리를 찾을 수 없습니다.
#region "Variables"
private TcpListener _TcpListener;
private Thread _ListenThread;
private string _ServerDataPath = "";
private string _Log = "[XMS LOG] Date : " + DateTime.Now.ToString("r") + "\r\n";
public List<string> Defaults;
public Dictionary<string, string> Mimes;
#endregion
private int _SendBufferSize = 2048;
private int _ReceiveBufferSize = 2048;
#region "Properties"
public int SendBufferSize
{
get { return _SendBufferSize; }
set
{
if (value <= 0)
{
return;
}
_SendBufferSize = value;
}
}
public int ReceiveBufferSize
{
get { return _ReceiveBufferSize; }
set
{
if (value <= 0)
{
return;
}
_ReceiveBufferSize = value;
}
}
public TcpListener Listener
{
get { return _TcpListener; }
}
public Thread ListenThread
{
get { return _ListenThread; }
}
public String Path
{
get { return _ServerDataPath; }
}
#endregion
여기
입니다 코드 :
private void Listen()
{
Socket cur = null;
try
{
// Infinite loop
while(true)
{
// Accept incoming socket
cur = _TcpListener.AcceptSocket();
// Limit socket buffers
cur.SendBufferSize = SendBufferSize; cur.ReceiveBufferSize = ReceiveBufferSize;
// Get request
byte[] Request = new byte[ReceiveBufferSize];
int RequestSize = cur.Receive(Request);
string RequestStr = Encoding.Default.GetString(Request);
// Clients send empty requests filled with nulls
// To prevent lag if request is empty then directly close stream
if (string.IsNullOrWhiteSpace(RequestStr) || string.IsNullOrEmpty(RequestStr))
{
cur.Close();
}
else
{
// Process request
Process(cur, RequestStr);
cur.Close();
}
}
}
catch (Exception ex)
{
SendError(cur, "TCPClient Listening Error", "500", "Runtime Exception", ex);
}
}
이 방법은 스레드에서 실행됩니다. 다음은 HTTP 요청을 처리하는 내 가공 방법은 다음과 같습니다
private void Process(Socket skt, string Request)
{
try
{
// Split all the request from line terminators
string[] RequestSplit = Request.Split(new string[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
// Get Request at top of this split array
string GetRequest = RequestSplit[0];
// Trim
GetRequest = GetRequest.Trim();
// Is it a get request?
if (!GetRequest.StartsWith("GET"))
{
// Send error and return
SendError(skt, "Bad Request : " + GetRequest, "400", "Bad Request");
return;
}
// Split Get Request
string[] GetRequestSplit = GetRequest.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
// Is Request Legal?
// Classical GET requests generally has 3 parts:
// GET {FileName} HTTP/1.1
// If we get length smaller than 3 then send error
if (GetRequestSplit.Length < 3)
{
SendError(skt, "Bad Request : " + GetRequest, "400", "Bad Request");
return;
}
Log(GetRequest);
// As usual middle one is file
string File = GetRequestSplit[1];
// We patch server path directory to this file string
File = _ServerDataPath + File;
// Control if it is a directory
// If it is a directory then control default files
bool IsIndex = false;
if (System.IO.Directory.Exists(File))
{
// This must be an index file
IsIndex = true;
}
// Not index file? No problem
// I just control that if there
// Is a file called like that
if (!IsIndex)
{
// Oops accidents happen.
// Cannot find the file that you requested.
if (!System.IO.File.Exists(File))
{
SendError(skt, "Cannot find selected file", "404", "Not Found");
return;
}
// Ok we a legal file
// Go out and send it!
}
// But if file is an index?
// Simple, loop over every default file
else
{
// No defaults defined by user?
// Sorry, we do not serve index files.
if (Defaults.Count == 0)
{
SendError(skt, "Default files are not allowed", "404", "Not Found");
return;
}
for (int i = 0; i < Defaults.Count; i++)
{
if (System.IO.File.Exists(File + "\\" + Defaults[i]))
{
// Get the index file. Patch it.
File += "\\" + Defaults[i];
goto send;
}
}
// Does not contain any default?
// Send error again.
SendError(skt, "Cannot find default file in requested directory", "404", "Not Fount");
return;
}
send:
// Here we are, sending data...
// Byte buffer for sending
byte[] Buffer = System.IO.File.ReadAllBytes(File);
// Mime?
string Mime = GetMime(File);
// Directly send while it is hot already!
SendMessage(skt, Buffer, true, "200", "OK", Mime);
}
catch (Exception ex)
{
SendError(skt, "Unknown exception", "500", "Internal Exception");
}
}
내 메시지 전송 방법 : 나는 그래서 내가 그들을 넣지 마십시오 대해 sendError, GetMime 또는 StrIsFile 방법에는 문제가 없다고 생각
public void SendMessage(Socket skt, byte[] message, bool includeHeader = false, string statusCode = "200", string statusMessage = "OK", string mime = "text/plain")
{
if (skt == null) { return; }
string header = "";
if (includeHeader)
{
header = "HTTP/1.1 " + statusCode + " " + statusMessage + "\r\n";
header += "Server: XMServer Module\r\n";
header += "Date: " + DateTime.Now.ToString("r") + "\r\n";
header += "Content-Type: " + mime + "; charset=utf-8\r\n";
header += "Connection: Closed";
header += "\r\n\r\n";
}
List<byte> buffer = Encoding.Default.GetBytes(header).ToList();
buffer.AddRange(message);
skt.Send(buffer.ToArray());
}
이리. 이것은 XMServer라는 클래스입니다.
XMServer server = new XMServer(8080, "..\\web\\", 4096, 1024);
server.Mimes = MIMES;
server.Defaults = DEFAULTS;
server.Start();
문제는 서버 디렉토리는 내가 거기에 index.html 파일을 넣어 \ .. \ 웹으로 정의하고 브라우저에 127.0.0.1:8080를 입력하고 서버가 index.html을을 보냅니다이다 : 여기 내 시작 코드 페이지. 이것은 훌륭하고 구현하려는 것입니다. "web"폴더에 "docs"폴더를 만들고 "docs"폴더에 "images"폴더를 넣습니다. index.html 파일을 "docs"폴더에 넣으십시오. index.html 콘텐츠 :
<html>
<head>
<title> Documentation </title>
<meta charset="utf-8"/>
</head>
<body>
<!-- This is where error pops out -->
<img src="images/logo.png"/>
</body>
</html>
서버가 index.html 파일을 올바르게 보냅니다. 그러나 페이지는 "GET /images/logo.png HTTP/1.1"과 같은 서버에 요청을 보냅니다. (예를 들어, 요청이이 메시지와 완벽하게 일치하는지 모르겠습니다.) 서버는 ".. \ web \ docs \ images \ logo.png"가 아닌 ".. \ web \ images \ logo.png"를 보내고 파일에 오류를 기록합니다 (이 작업을 수행하는 방법을 만들었습니다). 웹 폴더의 하위 idrector에서 다른 html 파일에 대한 링크를 제공하려고 할 때도 같은 일이 발생합니다. 어떻게 이길 수 있니? 그리고 내 코드가 비효율적이라고 확신하여 실수를 저에게 보여주십시오. 어떤 도움을 주시면 감사하겠습니다.
게시하기 전에 좋은 질문을 작성하는 방법에 대한 지침을 읽으십시오. –
Welcome to Stack Overflow! 나는 당신의 문제가 무엇인지 아주 명확하게 알지 못합니다 ... 명확하고 철저해야 함을 명심하고, (가능한 경우) 적절한 예를 포함하십시오. 도움을받을 때 계속 진행할 수 있도록 몇 가지 코드를 알려주십시오. - 신입 회원을 위해 읽을만한 좋은 점은 [질문하는 방법] (http://stackoverflow.com/help/how-to-ask)과 [둘러보기] (http://stackoverflow.com/tour)입니다. –