Click here to Skip to main content
15,925,399 members
Home / Discussions / C#
   

C#

 
GeneralRe: Thread.Sleep() Pin
yesu prakash2-Apr-09 19:19
yesu prakash2-Apr-09 19:19 
Question[Message Deleted] Pin
Rajdeep.NET is BACK1-Apr-09 23:55
Rajdeep.NET is BACK1-Apr-09 23:55 
AnswerRe: Help needed again....MessageBox pops up Twice! Pin
12Code1-Apr-09 23:59
12Code1-Apr-09 23:59 
GeneralRe: Help needed again....MessageBox pops up Twice! Pin
Rajdeep.NET is BACK2-Apr-09 0:09
Rajdeep.NET is BACK2-Apr-09 0:09 
GeneralRe: Help needed again....MessageBox pops up Twice! Pin
12Code2-Apr-09 0:17
12Code2-Apr-09 0:17 
GeneralRe: Help needed again....MessageBox pops up Twice! PinPopular
J4amieC2-Apr-09 0:11
J4amieC2-Apr-09 0:11 
AnswerRe: Help needed again....MessageBox pops up Twice! Pin
King Julien2-Apr-09 0:06
King Julien2-Apr-09 0:06 
QuestionHow do i Create a simple HTTP web server in C# ? Pin
sportsonpc1-Apr-09 23:02
sportsonpc1-Apr-09 23:02 
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 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n";
            outStream.Write(stringOut.ToCharArray());

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

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

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

            stringOut = "</body></html>\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 window. 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
AnswerRe: How do i Create a simple HTTP web server in C# ? Pin
sportsonpc1-Apr-09 23:24
sportsonpc1-Apr-09 23:24 
AnswerRe: How do i Create a simple HTTP web server in C# ? Pin
#realJSOP2-Apr-09 0:16
professional#realJSOP2-Apr-09 0:16 
QuestionDeleting Folder...... Please Help! Pin
Rajdeep.NET is BACK1-Apr-09 23:00
Rajdeep.NET is BACK1-Apr-09 23:00 
AnswerRe: Deleting Folder...... Please Help! Pin
musefan1-Apr-09 23:01
musefan1-Apr-09 23:01 
GeneralRe: Deleting Folder...... Please Help! Pin
Rajdeep.NET is BACK1-Apr-09 23:14
Rajdeep.NET is BACK1-Apr-09 23:14 
GeneralRe: Deleting Folder...... Please Help! Pin
musefan1-Apr-09 23:20
musefan1-Apr-09 23:20 
AnswerRe: Deleting Folder...... Please Help! Pin
MumbleB1-Apr-09 23:03
MumbleB1-Apr-09 23:03 
GeneralRe: Deleting Folder...... Please Help! Pin
Rajdeep.NET is BACK1-Apr-09 23:21
Rajdeep.NET is BACK1-Apr-09 23:21 
GeneralRe: Deleting Folder...... Please Help! Pin
MumbleB2-Apr-09 0:04
MumbleB2-Apr-09 0:04 
GeneralRe: Deleting Folder...... Please Help! Pin
Pete O'Hanlon2-Apr-09 1:32
mvePete O'Hanlon2-Apr-09 1:32 
AnswerRe: Deleting Folder...... Please Help! Pin
0x3c02-Apr-09 6:03
0x3c02-Apr-09 6:03 
Questionfiles created by process Pin
shefa' isied1-Apr-09 22:41
shefa' isied1-Apr-09 22:41 
AnswerRe: files created by process Pin
musefan1-Apr-09 22:58
musefan1-Apr-09 22:58 
GeneralRe: files created by process Pin
shefa' isied1-Apr-09 23:08
shefa' isied1-Apr-09 23:08 
GeneralRe: files created by process Pin
musefan1-Apr-09 23:19
musefan1-Apr-09 23:19 
GeneralRe: files created by process Pin
Eddy Vluggen1-Apr-09 23:38
professionalEddy Vluggen1-Apr-09 23:38 
GeneralRe: files created by process Pin
musefan1-Apr-09 23:55
musefan1-Apr-09 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.