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

  • Home
  • SEARCH
  • 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 3288112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:34:13+00:00 2026-05-17T20:34:13+00:00

I am creating a simple HTTP client/server application on my local machine but I

  • 0

I am creating a simple HTTP client/server application on my local machine but I don’t know why the ListenerCallback is triggered on the server; however, EndGetContext is not completing while throwing ‘Web Exception: Unable to connect to remove server” on the client side. Any ideas? here’s the code

    class Server
{
    static void Main(string[] args)
    {
        NonblockingListener(new string[] {"http://192.168.0.55:5432/"});
    }

    public static void NonblockingListener(string[] prefixes)
    {
        HttpListener listener = new HttpListener();
        foreach (string s in prefixes)
        {
            listener.Prefixes.Add(s);
        }
        listener.Start();
        IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);     
        Console.WriteLine("Waiting for request to be processed asyncronously.");
        result.AsyncWaitHandle.WaitOne();
        Console.WriteLine("Request processed asyncronously.");
        listener.Close();
    }

    public static void ListenerCallback(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        // Call EndGetContext to complete the asynchronous operation.

        HttpListenerContext context = listener.EndGetContext(result);
        HttpListenerRequest request = context.Request;
        Stream reader = request.InputStream;


        HttpListenerResponse response = context.Response;

        string responseString = "<HTML><BODY> Hello World!</BODY></HTML>";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);

        output.Close();
    }


}


   class Client
{
    public static void Main()
    {
        // Create a request using a URL that can receive a post. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.0.55:5432");

        request.UserAgent = "linkToShare - HTTPWebRequest";
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "data data data data.";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;          
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);
        reader.Close();
        dataStream.Close();
        response.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-17T20:34:13+00:00Added an answer on May 17, 2026 at 8:34 pm

    The problem with your code is that in the server you are calling EndGetContext method which will set the WaitHandle and immediately close the server before it had any time to send the response.

    Here’s a slight modification of your code.

    Server:

    class Program
    {
        private static ManualResetEvent _waitHandle = new ManualResetEvent(false);
    
        static void Main()
        {
            NonblockingListener(new string[] { "http://+:5432/" });
        }
    
        public static void NonblockingListener(string[] prefixes)
        {
            using (var listener = new HttpListener())
            {
                foreach (string s in prefixes)
                {
                    listener.Prefixes.Add(s);
                }
                listener.Start();
                var result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
                Console.WriteLine("Waiting for request to be processed asyncronously.");
    
                // Block here until the handle is Set in the callback
                _waitHandle.WaitOne();
                Console.WriteLine("Request processed asyncronously.");
                listener.Close();
            }
        }
    
        public static void ListenerCallback(IAsyncResult result)
        {
            var listener = (HttpListener)result.AsyncState;
            var context = listener.EndGetContext(result);
    
            var response = context.Response;
            string responseString = "<HTML><BODY>Hello World!</BODY></HTML>";
            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            response.OutputStream.Write(buffer, 0, buffer.Length);
            // Finished sending the response, now set the wait handle
            _waitHandle.Set();
        }
    }
    

    Client:

    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.UserAgent] = "linkToShare - HTTPWebRequest";
                var valuesToPost = new NameValueCollection
                {
                    { "param1", "value1" },
                    { "param2", "value2" },
                };
                var result = client.UploadValues("http://127.0.0.1:5432", valuesToPost);
                Console.WriteLine(Encoding.UTF8.GetString(result));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experiencing some performance problems when creating a very simple Python HTTP server.
I'm creating a simple web server in Ruby, which display's the text LOLZ in
I am creating a simple Unix server written in C++ that simply waits for
While creating a simple client for a REST service which I have stubbed out,
I'm creating a simple API that creates typed classes based on JSON data that
I'm creating a simple tag system that allows me to link a tag to
I am using Visual Studio 2008 Express and I tried creating a simple console
One simple method I've used in the past is basically just creating a second
The issue is simple really. Instead of creating folders in Visual Studio, I create
I'm creating a threaded message board and I'm trying to keep it simple. There's

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.