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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:52:15+00:00 2026-06-04T07:52:15+00:00

I have a Visual Studio 2008 C# .NET 3.5 application running on Windows XP

  • 0

I have a Visual Studio 2008 C# .NET 3.5 application running on Windows XP SP3 x86. In my application, I have an event handler OnSendTask that can be called by multiple threads simultaneously. It opens a TCP connection to a remote host and sends/receives data.

For example:

/// <summary>
/// prevent us from exceeding the maximum number of half-open TCP 
/// connections in Windows XP.
/// </summary>
private System.Threading.Semaphore tcp_connection_lock_ = 
    new System.Threading.Semaphore(10, 10);

public event EventHandler<SendTaskEventArgs> SendTask;

private void OnSendTask(object sender, SendTaskEventArgs args)
{
    try
    {
        tcp_connection_lock_.WaitOne();
        using (TcpClient recipient = new TcpClient())
        {
            // error here!
            recipient.Connect(args.IPAddress, args.Port);
            using (NetworkStream stream = recipient.GetStream())
            {
                // read/write data
            }
    }
    catch
    {
        // write exceptions to the logfile
    }
    finally
    {
        tcp_connection_lock_.Release();
    }
}

void SendTasks(int tasks_to_send)
{
    using (ManualResetEvent done_event = new ManualResetEvent(false))
    {
        int countdown = tasks_to_send;
        for (int i = 0; i < tasks_to_send; ++i)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                SendTaskEventArgs args = new SendTaskEventArgs(/*...*/);

                EventHandler<SendTaskEventArgs> evt = SendTask;
                if (evt != null)
                    evt(this, e);

                if (Interlocked.Decrement(ref countdown) == 0)
                    done_event.Set();
            }, i);
        }
        done_event.WaitOne();
    }
}

Unfortunately, I occasionally see this error:

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.0.16:59596
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)

Some points of information:

  • If I send a task to 40 remotes, I will see this response from around 6.
  • A Wireshark trace shows no attempt to even initiate a TCP connection from the PC to the remote.
  • I can ping the remote from the PC and get consistent good responses.
  • The remotes are all on the same switch and subnet as the PC running this application. There is no fancy networking in the way.

Can anybody suggest what may be causing this error or how I can fix it?

Thanks

  • 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-04T07:52:16+00:00Added an answer on June 4, 2026 at 7:52 am

    I am not sure of all of the details behind the max half-open TCP connections but, I believe it is NOT specific to application connections, but rather system wide. Are you sure that when this error occurs there are no other applications on the system that are creating TCP connections?

    I’d setup a retry whenever an error occurs. Something like:

    private const int MaxRetries = 10;
    private void OnSendTask(object sender, SendTaskEventArgs args)
    {
        bool retry = false;
        try
        {
            tcp_connection_lock_.WaitOne();
            using (TcpClient recipient = new TcpClient())
            {
                // error here!
                recipient.Connect(args.IPAddress, args.Port);
                using (NetworkStream stream = recipient.GetStream())
                {
                    // read/write data
                }
            }
        }
        catch (SocketException ex)
        {
            if(args.RetryCount < MaxRetries)
            {
                retry = true;
                args.RetryCount++;
            }
            else
            {
                // write exceptions to the logfile
            }
        }
        finally
        {
            tcp_connection_lock_.Release();
        }
    
        if(retry)
        {
            Thread.Sleep(1);
            OnSendTask(sender, args);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Visual Studio 2008, c#, .net 2.0. I have a Windows Forms client application
JI have written a .NET C# Windows Form app in Visual Studio 2008 that
I have my application in Visual Studio 2008, .net 3.5 running under IE7. It's
I have a Windows Forms application that was created in Visual Studio 2008 and
We have a ASP.Net web application running in Visual Studio 2010 that is targeting
I have a Visual Studio 2008 C# .NET 3.5 project that I am implementing
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I have made a small windows application with visual studio 2008 ,its working properly
Scenario : I have an ASP.NET MVC application developed in Visual Studio 2008. There
I have an asp.net application that i am modifying. I setup visual studio 2010

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.