I have written an application for Receiving Stock Market Feed over a 2 Mbps Connection with Exchange with average incoming data of 800kbps.
The program works fine for sometime i.e. for an hour or so but then it throws SocketException Saying "An Operation on socket could not be performed because the system lacked sufficient buffer space ir because a queue was full".
But in my program i am receiving it very fast that on my internal LAN it works fine for 24 hours without any problem.
But in production environment it throws this exception after sometime i.e. 1.5 hours or 30 min or 15 min sometime. even i set buffer size 8k, 16k, 32k, 64k, 128k. still a problem.
Program is written in .Net Framework 1.1 using C#.
My ListenTCP code & Receive Data Code is like:
private void ListenTSTCPFeed()
{
while(doRecieveTDFeed)
{
try
{
if(tdGatewaySocket.Poll(Configurations.POLL_TIMER, SelectMode.SelectRead) && tdGatewaySocket.Connected)
{
byte[] dataBuffer = ReceiveVarDataTweaked();
if(dataBuffer != null)
{
AddToQueue(dataBuffer);
totalMessagesReceived++;
tdFeedProcessor.BeginInvoke(null, null);
}
else
{
break;
}
}
else
{
#region Sleep Thread
Thread.Sleep(new TimeSpan(Net.Tawasol.Config.Configurations.MESSAGE S_PER_SECOND));
#endregion
}
}
catch(NotSupportedException e)
{
//Start Fire Event OnDisConnection
TDConnectionStatusEventArgs args = new TDConnectionStatusEventArgs();
args.Message = "Unable to Connect with Reason:::: >> NotSupportedException >>>"+e.Message;
OnDisConnection(args);
break;
//End Fire Event OnDisConnection
}
catch(SocketException e)
{
//Start Fire Event OnDisConnection
TDConnectionStatusEventArgs args = new TDConnectionStatusEventArgs();
args.Message = "Unable to Connect with Reason:::: >> SocketException >>>"+e.Message;
OnDisConnection(args);
break;
//End Fire Event OnDisConnection
}
catch(ObjectDisposedException e)
{
//Start Fire Event OnDisConnection
TDConnectionStatusEventArgs args = new TDConnectionStatusEventArgs();
args.Message = "Unable to Connect with Reason:::: >> ObjectDisposedException >>>"+e.Message;
OnDisConnection(args);
break;
//End Fire Event OnDisConnection
}
}
Close(false);
}
{
int size;
byte[] datasize = new byte[4];
byte[] datasizeCopy = new byte[4];
byte[] data = new byte[0];
int i = 0;
#region Receive Bytes & Get Length of Data
while( i<4 )
{
try
{
i += tdGatewaySocket.Receive(datasize, i, 4-i, SocketFlags.None);
}
catch(Exception ex)
{
throw ex;
}
}
datasize.CopyTo(datasizeCopy, 0);
if(BitConverter.IsLittleEndian)
{
Array.Reverse(datasize);
}
size = BitConverter.ToInt32(datasize, 0);
try
{
data = new byte[size];
}
catch(Exception ome)
{
throw ome;
}
#endregion
#region Get Data Bytes
i = 0;
while(i < size)
{
try
{
int recv = tdGatewaySocket.Receive(data, i, size-i, SocketFlags.None);
if(recv == 0)
{
return null;
}
i += recv;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
byte[] msg = new byte[data.Length+4];
datasizeCopy.CopyTo(msg, 0);
data.CopyTo(msg, 4);
DataReceived+=msg.Length;
return msg;
}
Can anybody guide me about the problem.
Thanks.