Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:35:59+00:00 2026-05-16T04:35:59+00:00

So, for a bit of background : This class is created to accept and

  • 0

So, for a bit of background :
This class is created to accept and respond to calls made remotely in an HTTP format.

The problem is when the method of the request is POST, sometimes the request is processed correctly, but most of the times the class just ends up being irresponsive.
Also, the line “Debug1” and “Debug2” are never written to the console, even when the request is processed correctly.
The line “Debug3” appears only when the request is processed correctly.

I know this will probably look messy, C# is only a hobby for me, and I’m learning 🙂
Thanks for spending some time to go through this code!

Here is the code:

class WebServer
{
    private TcpListener myListener;

    public WebServer(int port)
    {
        //Threading the listener
        try
        {
            myListener = new TcpListener(IPAddress.Any, port) ;
            myListener.Start();

            Thread th = new Thread(new ThreadStart(StartListen));
            th.Start() ;
        }
        catch(Exception e)
        {
            Logs.Add("WebServer|An Exception Occurred while Listening :" +e.ToString());
        }
    }
    private void StartListen()
    {
        int iStartPos = 0;
        string sHttpVersion;
        string sResponse = "";
        string sCode = " 200 OK";

        while(true)
        {
            //Accept a new connection
            Socket mySocket = myListener.AcceptSocket();
            if(mySocket.Connected)
            {
                Byte[] bReceive = new Byte[1024];
                int i = mySocket.Receive(bReceive,bReceive.Length,SocketFlags.None);
                string sBuffer = Encoding.ASCII.GetString(bReceive).TrimEnd('\0');

                iStartPos = sBuffer.IndexOf("HTTP",1);
                sHttpVersion = sBuffer.Substring(iStartPos,8); //http version (ex: "HTTP/1.1")

                if (sBuffer.StartsWith("GET / "))
                {
                    Logs.Add("WebServer|Connected:" + mySocket.RemoteEndPoint.ToString());
                    sResponse = ArrayToJson();
                }
                else if (sBuffer.StartsWith("POST"))
                {
                    Console.WriteLine("Debug1");

                    //This is a POST request, so more data is waiting to be retreived...  
                    bReceive = new Byte[2048];
                    i = mySocket.Receive(bReceive,bReceive.Length,SocketFlags.None);
                    sBuffer = Encoding.ASCII.GetString(bReceive).TrimEnd('\0');

                    Console.WriteLine("Debug2");

                    //Parsing the request
                    string[] sParams = sBuffer.Split(',');
                    Console.WriteLine(sParams.Length);
                    Console.WriteLine("Debug3: {0} - {1} - {2} - {3} - {4}", sParams[0], sParams[1], sParams[2], sParams[3], sParams[4]);

                    //I do what needs to be done here

                    Logs.Add("WebServer|BotStartRequest:" + mySocket.RemoteEndPoint.ToString());
                    sResponse = "Accepted";
                }

                //Sending response and closing socket
                SendHeader(sHttpVersion, "text/html", sResponse.Length, sCode, ref mySocket);
                SendToBrowser(sResponse, ref mySocket);
                mySocket.Close();
            }
        }
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:35:59+00:00Added an answer on May 16, 2026 at 4:35 am

    Implementing HTTP/1.1 is not a simple task. The basic protocol looks quite simple, but it’s really hard to get even a minimal server implementation right: You have at least to think about persistent connections, in the case of POST of the Expect: 100-continue header, correctly parsing the header, and much more.

    I strongly recommend you have a look at existing libraries/code. For example, the HttpListener class is built into the .NET Framework and probably already provides all you’ll ever need.

    If you really want to implement a server from scratch, have a look at Microsoft Cassini, a simple HTTP server written in C# licensed under Ms-PL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, there's a bit of background to this issue available on my blog:
Let me start off with a bit of background. This morning one of our
A bit of background first: I am using base code from a remote SVN
First a bit of background. I have been working on the MS platform for
Bit of an obscure one this. My setup is all running on my local
A bit of a neophyte haskell question, but I came across this example in
This is a bit of a long shot, but if anyone can figure it
I've got this link <a href=/Admin/Product/Brands class=newButton></a> Without the class attribute and a little
Bit of background, I'm a total lisp noob, only started a few weeks ago,
Are (seemingly) shady things ever acceptable for practical reasons? First, a bit of background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.