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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:05:06+00:00 2026-06-12T17:05:06+00:00

I’ve written some code to send and read text from a listener. This runs

  • 0

I’ve written some code to send and read text from a listener. This runs fine on the 1st and 2nd exchange, but on the 3rd send there’s a long delay between calling GetRequestStream() and the actual writing of the data.

I’ve disposed the outstream on the send side, as well as the stream reader, and the input stream on the read side as recommended here: Does anyone know why I receive an HttpWebRequest Timeout?

And it still hangs on the 3rd attempt to send info. It definitely seems to be hanging at GetRequestStrean() in SendMessage():

public void SendMessage(string message)
{
    HttpWebRequest request;
    string sendUrl;
    
    sendUrl = "http://" + termIPAddress + ":" + sendPort + "/";
    
    Uri uri = new Uri(sendUrl);
    
    Console.WriteLine("http://" + termIPAddress + ":" + sendPort + "/");
    
    ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
    
    servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
    servicePoint.ConnectionLeaseTimeout = 300;
    
    
    request = (HttpWebRequest)WebRequest.Create(sendUrl);
    request.KeepAlive = false;
    request.Method = "POST";
    request.ProtocolVersion = HttpVersion.Version11;
    request.ContentType = "application/x-www-form-urlencoded";
    request.Headers.Add("SourceIP", localIPAddress);
    request.Headers.Add("MachineName", localName);
    requestStarted = true;
    
    
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
    request.ContentLength = buffer.Length;
        
    try
    {
        using (Stream output = request.GetRequestStream())
        {
            output.Write(buffer, 0, buffer.Length);
            output.Close();
            request = null;
        }
    }
    catch(WebException wE)
    {
        Console.WriteLine(wE.Message);
    }
}

And this is the read portion :

public string GetMessage()
{
    Console.WriteLine("Entering actual listener");
    string s;
    string sourceIP;
    
    NameValueCollection headerList;
    
    HttpListenerContext context = terminalListener.GetContext();
    HttpListenerRequest request = context.Request;
        
    headerList = request.Headers;
    sourceIP = headerList.GetValues("SourceIP")[0];
    termName = headerList.GetValues("MachineName")[0];
    termIPAddress = sourceIP;
    using (System.IO.Stream body = request.InputStream)
    {
        System.Text.Encoding encoding = request.ContentEncoding;
        using (System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding))
        {
            s = reader.ReadToEnd();
            body.Close();
            reader.Close();
        }
    }
    
    return termName + " : " + s;    
}

I also tried to add an IP End Point bind but have to be honest, I don’t fully understand this piece of the code:

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    int portNumber = Convert.ToInt32(sendPort);
    IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(localIPAddress), 0); 
    
    Console.WriteLine(Convert.ToString(IEP));
    return IEP;  
}
  • 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-06-12T17:05:07+00:00Added an answer on June 12, 2026 at 5:05 pm

    You just forgot to call HttpWebRequest.GetResponse and therefore run out of connection limit.

    So, you should change your code as follows:

    try
    {
        using (Stream output = request.GetRequestStream())
            output.Write(buffer, 0, buffer.Length);
    
        var response = request.GetResponse() as HttpWebResponse;
        //TODO: check response.StatusCode, etc.
    }
    catch(WebException wE)
    {
        Console.WriteLine(wE.Message);
    }
    

    Also, in some cases you might want to adjust default connection limit:
    ServicePointManager.DefaultConnectionLimit

    Or use persistent connections:
    HttpWebRequest.KeepAlive

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.