I have written server code which receives the data from servers continuously using multithreading (asynchronous) from multiple clients (more than 500). It reads data from all the clients and works properly in terms multithreading and receives all the message sent by clients.
What is the problem ?
The problem is when I keep on reading data from client after 1 and 2 hours my server gets slow. By that, I mean it keeps on increasing CPU usage (may be be because of unfreeing the memory…because here we leave every thing on garbage collector). When I see in CPU the memory rises from 14% to suddenly 80-100% after some time. But it still receives data from client (but some there is leak of data) but it still receives. And I am not able to determine the cause of the problem.
Could someone please let me know what could be the basic cause of it?
The important part of code is:
public void StartServer()
{
try
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
server.Bind(ipep);
server.Listen(4);
AppendIncomingData(txtMsg, "Server Started..");
btnStart.Enabled = false;
lblStartDate.Text = DateTime.Now.ToString();
server.BeginAccept(new AsyncCallback(AcceptConn), server);
}
catch (Exception ex)
{
AppendIncomingData(txtError, ex.Message);
}
}
void AcceptConn(IAsyncResult ia)
{
Socket client;
try
{
Socket oldserver = (Socket) ia.AsyncState;
client = oldserver.EndAccept(ia);
StateObject state = new StateObject();
state.workSocket = client;
client.ReceiveTimeout = 1000;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveData), state);
server.BeginAccept(new AsyncCallback(AcceptConn), server);
}
catch (Exception ex)
{
AppendIncomingData(txtError, "AcceptConn Exception : " + ex.Message);
}
}
/// <summary>
/// StateObject Class to read data from Client
/// </summary>
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 512;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
public Int64 DeviceId = 0;
}
void ReceiveData(IAsyncResult ia)
{
StateObject state = (StateObject)ia.AsyncState;
Socket client = state.workSocket;
string sql = "";
string stringData = "";
try
{
recv = client.EndReceive(ia);
if (recv == 0)
{
client.Close();
server.BeginAccept(new AsyncCallback(AcceptConn), server);
return;
}
Array.Resize(ref state.buffer, StateObject.BufferSize);
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveData), state);
} //End Try
catch (Exception ex)
{
AppendIncomingData(txtError, "ReceiveData Error : " + stringData + ": " + ex.Message);
client.Close();
server.BeginAccept(new AsyncCallback(AcceptConn), server);
}
}
public void AppendIncomingData(object dataControl, string str)
{
if (btnMsg.Text.ToUpper().Contains("START") == true)
return;
TextBox lst = (TextBox)dataControl;
if (lst.InvokeRequired)
{
AppendIncomingData_Delegate method = new AppendIncomingData_Delegate(AppendIncomingData);
lst.Invoke(method, new object[] { lst, str });
}
else
{
msglines += 1;
if (msglines > 500)
{
msglines = 0;
//lst.Clear();
txtError.Clear();
txtMsg.Clear();
}
lst.AppendText(Environment.NewLine + DateTime.Now.ToString() + " " + str);
}
}
What could be the problem?