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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:52:51+00:00 2026-05-12T22:52:51+00:00

On my multithreaded server I am experiencincg troubles with connections that are not coming

  • 0

On my multithreaded server I am experiencincg troubles with connections that are not coming from the proper Client and so hang unathorized. I did not want to create new thread only for checking if clients are connected for some time without authorization. Instead of this, I have add this checking to RecieveData thread, shown on the code below. Do you see some performance issue or this is acceptable? The main point is that everytime client is connected (and Class client is instantionized) it starts stopwatch. And so I add to this thread condition – if the time is greater than 1 and the client is still not authorized, its added on the list of clients determinated for disconnection. Thanks

EDIT: This While(true) is RecieveData thread. I am using async. operations – from tcplistener.BeginAccept to threadpooling. I have updated the code to let you see more.

 protected void ReceiveData()
    {
        List<Client> ClientsToDisconnect = new List<Client>();
        List<System.Net.Sockets.Socket> sockets = new List<System.Net.Sockets.Socket>();
        bool noClients = false;
        while (true)
        {
            sockets.Clear();
            this.mClientsSynchronization.TryEnterReadLock(-1);
            try
            {
                for (int i = 0; i < this.mClientsValues.Count; i++)
                {
                    Client c = this.mClientsValues[i];
                    if (!c.IsDisconnected && !c.ReadingInProgress)
                    {
                        sockets.Add(c.Socket);
                    }
                    //clients connected more than 1 second without recieved name are suspect and should be disconnected
                     if (c.State == ClientState.NameNotReceived && c.watch.Elapsed.TotalSeconds > 1)
                         ClientsToDisconnect.Add(c);
                }
                if (sockets.Count == 0)
                {
                    continue;
                }
            }
            finally
            {

                this.mClientsSynchronization.ExitReadLock();
            }
            try
            {
                System.Net.Sockets.Socket.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                foreach (System.Net.Sockets.Socket s in sockets)
                {
                    Client c = this.mClients[s];
                    if (!c.SetReadingInProgress())
                    {
                        System.Threading.ThreadPool.QueueUserWorkItem(c.ReadData);

                    } 
                }
                //remove clients in ClientsToDisconnect
                foreach (Client c in ClientsToDisconnect)
                {
                    this.RemoveClient(c,true);
                }
            }
            catch (Exception e)
            {
                //this.OnExceptionCaught(this, new ExceptionCaughtEventArgs(e, "Exception when reading data."));
            }
        }
    }
  • 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-12T22:52:51+00:00Added an answer on May 12, 2026 at 10:52 pm

    I think I see what you are trying to do and I think a better way would be to store new connections in a holding area until they have properly connected.

    I’m not positive but it looks like your code could drop a valid connection. If a new connection is made after the checking section and the second section takes more than a second all the timers would time out before you could verify the connections. This would put the new connections in both the socket pool AND the ClientsToDisconnect pool. Not good. You would drop a currently active connection and chaos would ensue.

    To avoid this, I would make the verification of a connection a seperate thread from the using of the connection. That way you won’t get bogged down in timing issues (well…you still will but that is what happens when we work with threads and sockets) and you are sure that all the sockets you are using won’t get closed by you.

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

Sidebar

Related Questions

I'm creating an app that is a sort of client- multithreaded server. The problem
I have a server that communicates with a client. The server is multithreaded, and
I'm trying to make a multithreaded server/client app with java ! this code is
I am writing a multithreaded server application, in C (linux), that must listen to
I am writing a multithreaded client that uses an IO Completion Port. I create
I'm trying to build a simple multithreaded tcp server. The client connects and sends
We have a multithreaded RPC server that parses input strings. We've run into an
I have an android client and a multithreaded Java server. The server was originally
...for use in a multithreaded network server. I want to pass data around between
I have a simple multithreaded C# server and client. When just one client is

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.