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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:54:53+00:00 2026-05-15T22:54:53+00:00

I had a problem with too many connections After a few test i deducted

  • 0

I had a problem with too many connections After a few test i deducted the problem is with MY server. The fact that the server listening port was on the left side should have told me.

When running the same client code using a server on a different machine i dont get hundreds of ports opened. When my server on my local machine i get > 200 connects connecting to my listening port. I think i am handling clients wrong. My code is below with all non server and client code removed

{
    TcpListener server = null;
    server = new TcpListener(port);
    server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
    server.Start();

    while (true)
    {
        var client = server.AcceptTcpClient();
        using(var stream = client.GetStream()) {
        ...
        stream.Read(...
        ...
        stream.Write(...
        } //using above should close this.
        client.Close();
    }
    server.Stop();
}

I modified the code to use asynchronous connections and it locks up the 2nd time it does stream.Read just under the while (client.Connected)

    server = new TcpListener(port);
    server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
    server.Start();
    server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
    while (true)
        Thread.Sleep(1000); //infinite loop for testing




public void DoAcceptTcpClientCallback(IAsyncResult ar)
{
    Byte[] bytes = new Byte[1024 * 4];
    TcpListener listener = (TcpListener)ar.AsyncState;
    using (TcpClient client = listener.EndAcceptTcpClient(ar))
    {
        using (var stream = client.GetStream())
        using (var ostream = new MemoryStream())
        {
            while (client.Connected)
            {
                int i;
                while ((i = stream.Read(bytes, 0, bytes.Length)) == bytes.Length)
                    ostream.Write(bytes, 0, i);

                ostream.Write(bytes, 0, i);
                szresults = Func(ostream)
                var obuf = Encoding.UTF8.GetBytes(szresults);
                stream.Write(obuf, 0, obuf.Length);
            }
        }
        client.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-15T22:54:53+00:00Added an answer on May 15, 2026 at 10:54 pm

    You’re dealing with a connection within the same thread. That means you can only actively process a single connection at a time. The simplest approach is to pass the client to a new thread, and deal with it there.

    Now that doesn’t scale hugely well – using fully asynchronous operations everywhere will allow you to serve the same number of clients using fewer threads – but in most cases it will work well enough, IME. It’s certainly where I’d go next.

    Ideally you don’t want to create a new OS thread each time – it’s probably reasonable to use the system thread pool for this, so the thread can be reused after the connection has been completely handled. There are risks around filling up the OS thread pool, but I suspect that’s unlikely to bite you.

    Finally, I’d put the TcpClient into a using statement to make sure you dispose of it even if something else fails. Of course, if you’re handing it over to another thread it should be that thread which immediately uses a using statement.

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

Sidebar

Related Questions

My problem is that I'm apparently using too many tasks (threads?) that call a
Not too long ago, I had a problem which required me to set WinDbg.exe
I have had a few problems with log files growing too big on my
I had a problem that was partially solved. To explain it quickly : I
I had a problem with committing changes after merging two branches of my project
I've had this problem many times before, and I've never had a solution I
Today I had the problem that I couldn't delete a folder because it was
I had recently a problem with oom-killer starting to kill processes after some time.
I am having this problem that I had used Arraylist in my entire program
I recently had a problem during the deployment of a windows service. Four computers

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.