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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:05:52+00:00 2026-05-28T06:05:52+00:00

I need to write some code that uses TcpListener . The object that using

  • 0

I need to write some code that uses TcpListener. The object that using the TcpListener has 3 possibilities: Listen, Stop Listen, Send message. This is how my code looks:

public class someClass
{
    public bool listenerEnable {get; set; }

    public void StartListen()
    {
        .
        .
        .

        while (listenerEnable)
        {
            // Block until client connected
            TcpClient client = server.AcceptTcpClient();       

            // code that doing something with the client message ... 
        }
    }
}

Now, I’m not sure I know how I can keep the thread safe when I want to stop the listener state (change the listenerEnable to be false).

Also I’m not sure I know how to keep this thread safe when I want to send some message to the client (in some separated method) without stopping the server listener state.

Thanks for any help.

  • 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-28T06:05:53+00:00Added an answer on May 28, 2026 at 6:05 am

    I would suggest waiting on an event rather than waiting on a variable. In your current scheme, consider the case when your loop test for whether listener variable is true. If it is it enters the loop and waits for a incoming connection, and blocks. So now even if another thread sets the listener variable to be false, your loop will not exit till someone connects to it and it processes that connection.

    One way of working around the problem above will be to have code like:

    WaitHandle[] waitHandles = new WaitHandle[] {endlLoopEvent};
    while(true)
    {
       int ret = WaitHandle.WaitAny(waitHandles, 100);
       if(WaitTimeout == ret) continue;
       else if(0 == ret) break; //our event has been signaled.
       else
        {
             if(!server.Pending()) continue; //Go back to the loop if no connection pending
             TcpClient client = server.AcceptTcpClient();  
             // You can spawn a thread to service the request.
        }
    }
    

    Sending would not affect the loop above, provided it is not in response to a request for connection above.

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

Sidebar

Related Questions

I need to write some code that would loop though all rows of a
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write some code to trigger internet connectivity on a computer. By
I need to write some code to insert around 3 million rows of data.
I need to write some sql that will allow me to query all objects
I need to write some C functions that will be called by a java
I am trying to write some code that does AES decryption for an SSL
I have an application that uses html5 to let the user listen to some
I'm writing code that deals with a file that uses hashes. I need to
Ok, I need to write a code that calls a script, and if the

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.