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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:04:54+00:00 2026-06-10T18:04:54+00:00

I have a tcp server application which accepts tcp clients in more number and

  • 0

I have a tcp server application which accepts tcp clients in more number and receives continuous messages from all the connected clients.

public void KeepListening()
    {
        while (IsRunning)
        {
                try
                {
                        Thread.Sleep(20);
                        if (tcpListener.Pending())
                        {
                            //Get the socket obtained from the tcpListener
                            TcpClient _Client = tcpListener.AcceptTcpClient();
                            NetworkStream ns = _Client.GetStream();
                            Thread.Sleep(20);
                            if (Stop_Status == false)
                            {
                                if (_Client.Connected)
                                {
                                    int DataBytes = _Client.Available;
                                    byte[] data = new byte[DataBytes];
                                    if (ns.DataAvailable == true)
                                    {
                                        ns.Read(data, 0, DataBytes);
                                        string Key = ASCIIEncoding.ASCII.GetString(data);
                                        string[] KeyCodes = Key.Split(',');
                                        if (Key != "" && ((KeyCodes[0].StartsWith("i"))))
                                            ProcessClient(_Client, KeyCodes[0], Key);
                                    }
                                }
                            }
                            else
                            {
                                ns.Flush();
                            }
                        }
                }
                catch
                {

                }
        }
    }


    void ProcessClient(TcpClient _Client, string Key,string Msg)
    {
        try
        {
            if (hashTable_Users.Count > 0)
            {
                if (hashTable_Users.Contains(Key))
                {
                    hashTable_Users.Remove(Key);
                }
            }
            hashTable_Users.Add(Key, _Client);
            hashusers = hashTable_Users.Count;
            this.MessageFromClient(Key, serverPortNo.ToString(), Msg);

            //StreamWriter sw = new StreamWriter(_Client.GetStream());
            //sw.WriteLine("1");
            //sw.Flush();

            object[] objarr = new object[5];
            objarr[0] = _Client;
            objarr[1] = Key;
            Thread ReceiveFromClient = new Thread(new ParameterizedThreadStart(ReceiveFromClients));
            ReceiveFromClient.Start(objarr);
        }
        catch
        {

        }
    }
   public void ReceiveFromClients(object obj)
    {
            object[] objarr = (object[])obj;
            TcpClient _Client = (TcpClient)objarr[0];
            NetworkStream ns = _Client.GetStream();
            while (IsRunning)
            {
                if (Stop_Status == false)
                {
                    if (_Client.Connected)
                    {
                        if (ns.DataAvailable)
                        {
                            try
                            {
                                int numBytesRead = _Client.Available;
                                byte[] data = new byte[numBytesRead];
                                ns.Read(data, 0, numBytesRead);
                                string MsgReceived = ASCIIEncoding.ASCII.GetString(data);
                                string clientKey = objarr[1].ToString();
                                this.MessageFromClient(clientKey, serverPortNo.ToString(), MsgReceived);
                            }
                            catch
                            {

                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    ns.Flush();
                }
            }
    }

KeepListening() is used to accept the clients which send the request. ProcessClient() and ReceiveFromClients() are used to receive the messages from the clients.

This application is using 100% cpu.

Please suggest me what to do to decease the cpu utilization?

  • 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-10T18:04:56+00:00Added an answer on June 10, 2026 at 6:04 pm

    By looking into your code, ReceiveFromClients is consuming most of CPU, because of while (IsRunning) and in that you are reading if only data is available.

    To solve this, dont check IsAvailabele, call ReadMethod which blocks the thread until ReadTimeOut occurs or some data has been read. Based on the number of bytes received, process your data.

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

Sidebar

Related Questions

I have a server application that receives some special TCP packet from a client
I have created a TCP server-client application in java which exchange periodic messages between
I have a TCP server application in which on receiving an incoming connection, I
I am working on a server application which receives data over a TCP socket
We have a .NET application which, on a certain server, keeps making various TCP
I have written a TCP server implementation using which I created an application which
I currently have a TCP server application written in .Net that receives and submits
I have a client-server application which communicates over TCP/IP. I use System.Net.Sockets.Socket type object
I'm developing an application that accepts connections from clients over the internet. All these
I have a little web server application which reads X bytes from a file

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.