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.AcceptTcpClie nt();
counter = 0;
}
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClie nt();
clientSocket.NoDelay = true;
clientSocket.Client.NoDela y = true;
handleClinet client = new handleClinet();
client.startClient(clientS ocket, 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.NoDela y = true;
if (serverString != string.Empty)
serverString = string.Empty;
if (strServerStr.Length > 1)
strServerStr.Remove(0, strServerStr.Length);
try
{
while (networkStream.DataAvailab le && networkStream.CanRead)
{
int recv = networkStream.Read(bytesFr om, 0, bytesFrom.Length);
serverString = System.Text.Encoding.ASCII .GetString (bytesFrom , 0, recv);
strServerStr.Append(server String);
}
if (!string.IsNullOrEmpty(ser verString) )
{
}
}
catch (Exception ex)
{
}
}
}
}
}
}
Program.cs
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
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.AcceptTcpClie
counter = 0;
}
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClie
clientSocket.NoDelay = true;
clientSocket.Client.NoDela
handleClinet client = new handleClinet();
client.startClient(clientS
}
}
}
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.NoDela
if (serverString != string.Empty)
serverString = string.Empty;
if (strServerStr.Length > 1)
strServerStr.Remove(0, strServerStr.Length);
try
{
while (networkStream.DataAvailab
{
int recv = networkStream.Read(bytesFr
serverString = System.Text.Encoding.ASCII
strServerStr.Append(server
}
if (!string.IsNullOrEmpty(ser
{
}
}
catch (Exception ex)
{
}
}
}
}
}
}
Program.cs
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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...
ASKER
IPAddress ipAddress = System.Net.Dns.GetHostByNa
serverSocket = new TcpListener(ipAddress, 51111);
But no any affect in CPU Usage