المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How do i Create a simple HTTP web server in C# ?



C# Programming
04-02-2009, 01:12 PM
Hi guys i need to solving this problem to create a simple HTTP web server in C#

here is the code

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

//************************************************************************//
// Thread to actually handle an HTTP connection. //
//************************************************************************//

//************************************************************************//
// This project makes an extremely simple HTTP server. //
// //
// //
//************************************************************************//

namespace TCP_Socket
{
class ConnectionThread
{


//******************************************************************//
// Constructor. //
//******************************************************************//

public ConnectionThread(Socket socketToHandleConnection)
{
connection = socketToHandleConnection;
}


//******************************************************************//
// Class (instance) variables. //
//******************************************************************//

Socket connection = null; //TCP/IP socket to handle the actual connection
NetworkStream connectionStream = null;
BinaryReader inStream = null;
BinaryWriter outStream = null;
String userName = null;




//******************************************************************//
//There are Threads all over the place here, probably un necessarily//
//Here is yet another Thread to handle the connection. //
//******************************************************************//

public void run()
{

//***********************************************************//
//We have now accepted a connection from a web browser. //
// //
//There are several ways to do this next bit. Here I make a//
//network stream and use it to create two other streams, an //
//input and an output stream. Life gets easier at that //
//point. //
//***********************************************************//
connectionStream = new NetworkStream(connection);

inStream = new BinaryReader(connectionStream);
outStream = new BinaryWriter(connectionStream);

userName = Environment.UserName;


byte b = 0;
String s = ""; //This will contain all the stuff the browser transmitted,
//but "all in one go".
try
{
while (connectionStream.DataAvailable)
{
b = (byte)inStream.ReadSByte();
Console.Out.Write((char)b);
s += (char)b;
}

String[] items = s.Split();//This will contain all the stuff the browser transmitted,
//but nicely split up.

}
catch (EndOfStreamException eos)
{
Console.Out.WriteLine("Unexpected end of stream.");
Console.Out.WriteLine("Error caused by " + eos.Message);
}
Console.Out.WriteLine("End of stream.");


//***********************************************************//
// Finally, write the output to the web browser. The HTTP //
// dialog seems wo want each line terminated with both //
// Carriage Return (CR or the "\r") and LF (Line Feed, or "\n//
// for new line), so that is the significance if all the //
// "\r\n" that you see. The output is written as chars, as//
// C# strings are slightly different. //
//***********************************************************//

String stringOut = "HTTP/ 1.1 200 OK\r\n";
outStream.Write(stringOut.ToCharArray());

outStream.Write(stringOut.ToCharArray());
stringOut = "Content-Type: text/html\r\n";

outStream.Write(stringOut.ToCharArray());

stringOut = "\r\n"; //Blank lines instead of//
//writing date and //
outStream.Write(stringOut.ToCharArray()); //content length. That//
stringOut = "\r\n"; //is for you... //

stringOut = "\r\n";
outStream.Write(stringOut.ToCharArray());

stringOut = "\r\n";
outStream.Write(stringOut.ToCharArray());

stringOut = "\r\n";
outStream.Write(stringOut.ToCharArray());

stringOut = "Welcome to " + userName + "'s primative HTTP server";
outStream.Write(stringOut.ToCharArray());

stringOut = "\r\n";
outStream.Write(stringOut.ToCharArray());



inStream.Close();
outStream.Flush();
outStream.Close();
connectionStream.Close();
connection.Close();

Console.Out.WriteLine("Done; Connection closed.");
}

}
}
This is where i need help, The loop just reads whatever the web browser sends to the server and writes it to the console ******** I need help converting the bytes to strings, looking for “\r” and / or “\n” (CR or Carriage Return, LF of Line Feed) to be at the end of a line and therefore terminate the strings.

byte b = 0;
String s = ""; //This will contain all the stuff the browser transmitted,
//but "all in one go".


Then i need look for the “get” below, to end up with a nice array of strings in items[] which represents what the browser transmitted, but split up into individual strings. Then i need to Stop your origram there by inserting a breakpoint and looking at the contents of items[].

I've been stuck on this for days, can anyone help, i am a beginner doing this for my first year, so please keep answer easy for me to understand.

Thanks in Advance