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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:53+00:00 2026-05-27T06:59:53+00:00

I have the following code in my server side: // Bind to a specific

  • 0

I have the following code in my server side:

// Bind to a specific local port number (SERVER_PORT) and any local IP address.
m_tlServer = new TcpListener(IPAddress.Any, SERVER_PORT);

// Start listening for connection attempts.
m_tlServer.Start();

// Start accepting new clients.
// Sleep until a new client(s) will try to connect to the server.
while (!m_bStopThread)
{
    if (!m_tlServer.Pending())
    {
        // Sleep and try again.
        Thread.Sleep(LISTENING_SLEEP_TIME);
        continue;
    }
    else
    {
        // Accept a new client in a new thread. 
        Thread newthread = new Thread(new ThreadStart(HandleNewConnection)); 
        newthread.Start();
    }
}

My problem is that when a client tries to connect to the server, the method Pending returns true several times (usually 4 times) and several thread are created.
I tried to replace that while loop by a loop that uses the AcceptTcpClient method (without knowing if there are any connection attempts) and it’s works fine. So, I think the problem is caused by Pending method.
Can anyone help me? Thanks, Ofer.

  • 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-27T06:59:54+00:00Added an answer on May 27, 2026 at 6:59 am

    Use AcceptTcpClient not Pending and it will work.

    Why what you’re doing is producing theses steps :

    1. Listener thread see Pending == true
    2. Start accept thread
    3. Listener thread see Pending == true
    4. Start accept thread
    5. Listener thread see Pending == true
    6. Start accept thread
    7. First accept thread begin to run
    8. Listener thread see Pending == false

    In a nutshell that’s not because you start a thread that some random instruction inside is executed immediately. The point of the thread is exactly that : It will execute later.

    If you want to have a way to stop the listening process use WaitHandles :

    // In your class somewhere stopEvent is a ManualResetEvent
    
    while(true)
    {
    
        var asyncResult = listener.BeginAcceptTcpClient(null, null);
    
        var waitHandles = new [] { stopEvent, asyncResult.AsyncWaitHandle };
    
        var waitResult = WaitHandle.WaitAny(waitHandles);
    
        if (waitResult == 0) return;
    
        var client = EndAcceptTcpClient(asyncResult);
        // Create thread to speak with this client
    
    }
    

    Your method that want to stop the thread will just need to stopEvent.Set()

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

Sidebar

Related Questions

I have a Server side variable by the following code Dim mgps As New
Visual Studio C++ 2008 I have the following code in my server.h file. #if
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following code on the client side for retrieving data from the
I have following code inside my ListView template. <asp:DropDownList runat=server ID=myDropDown Width=60px SelectedValue='<%# Eval(SelectedValue)
I have the following code on client side: <script src=http://code.jquery.com/jquery-1.5.js></script> <script> $(document).ready(function() { $(a).click(function()
I have the following json, I'm sending through AJAX, but in the server side
I have the following code that creates a serverside object of the xmlhttp class.
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section

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.