Link to home
Create AccountLog in
Avatar of sachinjain_aspnet
sachinjain_aspnet

asked on

CPU Usage increases C# Socket

I am establishing a connection through between different applications through TCP in c#. Everything is working fine, but my issue is that when i connect it , it increase my cpu usage from 5% to 50% within 2-3 minute and it increases continuously.  Below is my code or see attchment ; if you may provide me another set of code, this would be great help.

using System.Threading;
using System.Net.Sockets;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RemoteConnection obj = new RemoteConnection();  
        }

    }
    class RemoteConnection
    {
        public RemoteConnection()
        {
            Thread serverConnect = new Thread(ConnectServer);
            serverConnect = new Thread(ConnectServer);
            ThreadPool.SetMaxThreads(1, 1);
            serverConnect.Start();
        }
        public RemoteConnection(int i)
        {

        }
        TcpListener serverSocket;
        TcpClient clientSocket;

            private void ConnectServer()
            {
                serverSocket = new TcpListener(51111);
                clientSocket = default(TcpClient);
                int counter = 0;
                serverSocket.Start();
                counter = 0;
                if (counter > 0)
                {
                    serverSocket.Stop();
                    clientSocket.Close();
                    clientSocket = serverSocket.AcceptTcpClient();
                    counter = 0;
                }
                while (true)
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    clientSocket.NoDelay = true;
                    clientSocket.Client.NoDelay = true;
                    handleClinet client = new handleClinet();
                    client.startClient(clientSocket, Convert.ToString(counter));
                }

            }


    }
    public class handleClinet
    {

        TcpClient clientSocket;

        string clNo;
        string ipadid = null;
        string ipadname = null;
        Thread ctThread = null;
        public void startClient(TcpClient inClientSocket, string clineNo)
        {

            this.clientSocket = inClientSocket;
            this.clNo = clineNo;
            ctThread = new Thread(Server);
            ctThread.Start();
           
        }
        private void Server()
        {

            byte[] bytesFrom = new byte[1050];
            NetworkStream networkStream = null;
            StringBuilder strServerStr = new StringBuilder();
            string serverString = string.Empty;
            while (true)
            {

                if (clientSocket.Connected)
                {
                    if (networkStream == null)
                        networkStream = clientSocket.GetStream();
                    clientSocket.NoDelay = true;
                    clientSocket.Client.NoDelay = true;
                    if (serverString != string.Empty)
                        serverString = string.Empty;
                    if (strServerStr.Length > 1)
                        strServerStr.Remove(0, strServerStr.Length);

                    try
                    {


                        while (networkStream.DataAvailable && networkStream.CanRead)
                        {
                            int recv = networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                            serverString = System.Text.Encoding.ASCII.GetString(bytesFrom, 0, recv);
                            strServerStr.Append(serverString);
                        }
                        if (!string.IsNullOrEmpty(serverString))
                        {
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
    }
}
Program.cs
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of sachinjain_aspnet
sachinjain_aspnet

ASKER

I used below lines :

  IPAddress ipAddress = System.Net.Dns.GetHostByName(Convert.ToString(System.Net.Dns.GetHostName())).AddressList[0];

                serverSocket = new TcpListener(ipAddress, 51111);

But no any affect in CPU Usage
There's nothing wrong with that code...at least with respect to CPU usage.  The problem must be somewhere else.  You don't show any clients actually connecting...