?
Ich hab mich nochmal ran gesetzt und den von mir gezeigten Code überarbeitet. Das ganze sollte nun anständig laufen. Ich habs mit Firefox und Internet Explorer getestet.
Die Geschwindigkeit ist eigentlich ganz okay. Der Verbindungsaufbau dauert allerdings einen kleinen Moment.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace HTTP
{
class Program
{
static string HttpRoot = "E:/htdocs/";
static string HttpServer = "My Server";
static int HttpPort = 81;
static Dictionary<string, string> types = new Dictionary<string, string>();
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, HttpPort);
listener.Start();
types[".html"] = "text/html";
types[".htm"] = "text/html";
types[".jpg"] = "image/jpeg";
types[".jpeg"] = "image/jpeg";
while (true)
{
TcpClient client = null;
try
{
Console.Write("Awaiting connection... ");
client = listener.AcceptTcpClient();
Console.WriteLine("Done!");
if (client.Connected && client.Available > 0)
{
Console.Write("Receiving request... ");
byte[] request = new byte[client.Available];
client.GetStream().Read(request, 0, request.Length);
string[] header = System.Text.ASCIIEncoding.ASCII.GetString(request).Split(new char[] { '\n' });
Console.Write("[" + header[0].Trim() + "] ");
Console.WriteLine("Done!");
if (!client.Connected)
{
continue;
}
string[] requestElements = header[0].Split(new char[] { ' ' });
if (requestElements[0].ToLower() == "get")
{
Console.Write("Sending response... ");
string file = requestElements[1].Substring(1);
if (requestElements[1] == "/")
{
file = "index.html";
}
Console.Write("[Sending file \"" + file + "\"... ");
if (File.Exists(HttpRoot + file))
{
SendFile(client, "200 OK", HttpRoot + file);
Console.Write("Done!] ");
}
else
{
SendFile(client, "404 Not Found", HttpRoot + "404.html");
Console.Write("Not Found!] ");
}
Console.WriteLine("Done!");
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.Write("Closing connection... ");
if (client != null)
{
client.Close();
}
Console.WriteLine("Done!\n");
}
}
}
static void SendHttpHeader(TcpClient client, string code, string type, long length)
{
SendHttpString(client, String.Format("HTTP/1.0 {0}\r\nServer: {1}\r\nPragma: no-cache\r\nContent-Type: {2}\r\nContent-Length: {3}\r\n\r\n", code, HttpServer, type, length));
}
static void SendHttpString(TcpClient client, string str)
{
byte[] response = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
client.GetStream().Write(response, 0, response.Length);
}
static void SendFile(TcpClient client, string code, string path)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Open))
{
string type = "text/plain";
string extension = Path.GetExtension(path);
if (types.ContainsKey(extension))
{
type = types[extension];
}
SendHttpHeader(client, code, type, fs.Length);
byte[] buf = new byte[2048];
long total = 0;
long size = fs.Length;
long left = size;
while (total < size)
{
int rc = fs.Read(buf, 0, buf.Length);
client.GetStream().Write(buf, 0, rc);
total += rc;
left -= rc;
}
Console.Write(String.Format("(Sent {0} of {1} bytes: {2}) ", total, size, path));
}
}
catch
{
string msg = "500 - Internal Server Error";
SendHttpHeader(client, "500 Internal Server Error", "text/html", msg.Length);
SendHttpString(client, msg);
}
}
}
}
Die Beschränkung der Dateigröße ist auch nicht mehr vorhanden. Es können also nun auch größere Dateien versendet werden. Angepasst werden muss der Pfad zum Verzeichnis, wo alle Dateien liegen. Die Startseite muss index.html heißen.