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 4116238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:41:00+00:00 2026-05-20T22:41:00+00:00

I have to establish an HttpListener that will wait for requests made by our

  • 0

I have to establish an HttpListener that will wait for requests made by our client’s server. I have to receive that request on port 8088 and extract the query string. That is the easy part.

I’m running the HttpListener in a windows service. I cannot get it to fire properly. I build the setup project install the service on our server and it never starts. I suspect there’s an error with my code.

HttpListenerClass:

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

namespace lalalolo
{
    class HttpListenerClass
    {
        bool keepAlive = true;

        public void AddToFile(string contents)
        {
            var fs = new FileStream(@"C:\HttpListenerserv.txt", FileMode.OpenOrCreate, FileAccess.Write);
            var sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(contents);
            sw.Flush();
            sw.Close();
        }

        private HttpListener listener;

        public HttpListenerClass()
        {
            ThreadPool.SetMaxThreads(50, 100);
            ThreadPool.SetMinThreads(50, 50);
            listener = new HttpListener();
            listener.Prefixes.Add("http://*:8088/");
        }

        public void Start()
        {

            listener.Start();
            if(keepalive == true){
            {
                try
                {
                    HttpListenerContext ctx = listener.GetContext();
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessRequest), ctx);
                }
                catch(Exception ex)
                {
                    AddToFile(ex.Message);
                }
              }
            }
        }

        public void Stop()
        {
            listener.Stop();
            keepalive == false;
        }

        public void ProcessRequest(object listenerContext)
        {
            try
            {
                var context = (HttpListenerContext)listenerContext;
                string QS = context.Request.QueryString["ID"];
                AddToFile(QS);
            }

            catch(Exception ex)
            {
                AddToFile(ex.Message);
            }
        }
    }
}

Service1.cs:

using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace lalalolo
{
    public partial class HttpListenerTest1 : ServiceBase
    {
        HttpListenerClass HTTP = new HttpListenerClass();

        public void AddToFile(string contents)
        {
            var fs = new FileStream(@"C:\HttpListenerserv.txt", FileMode.OpenOrCreate, FileAccess.Write);
            var sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(contents);
            sw.Flush();
            sw.Close();
        }

        public HttpListenerTest1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            HTTP.Start();
        }

        protected override void OnStop()
        {
            HTTP.Stop();   
        }
    }
}

What am I doing wrong?

Thank you guys!

  • 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-20T22:41:01+00:00Added an answer on May 20, 2026 at 10:41 pm

    Queuing worker items in a while(true) loop? Are you serious?!

    1. Your OnStart method never returns because of that while loop. However, returning from the OnStart method is essential for the service manager to know, that your service started correctly.
    2. Your service will probably just die with an OutOfMemoryException or something similar, because of that endless loop.

    Suggestion:
    Try adopting this sample. It is in IronPython but that also uses the .NET framework. Hint: The while(true) in that implementation should be changed to be able to break the while loop when your service is being stopped. Additionally, you must call serveforever in your Start method in an async way.
    This should get you going.

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

Sidebar

Related Questions

I have a relatively simple program where I try establish Client Server connection and
When I establish a socket connection to a server, both client and server have
I have a WCF service that provides access to some data. Our client has
I have written a program that will etablish a network connection with a remote
Suppose I have a context that is configured similar to: Establish context = ()
I have a short jsp page that tries to establish the connection to the
I have a homework to build an application which will send and receive simple
I have an application which will establish DB connection with MySQL and execute queries.
I am developing an application that communicates (UDP communication) with our server remotely through
First of all, i'd to establish that i do have the text file in

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.