.Net socket client data receiving issue
I am implementing Master/Slave architecture in my application.
Master polls the jobs from the database and then distributes jobs among the slaves. I am using socket connection to maintain the connection among the master and slaves and to send the job to the client.
As soon as the master initiates the process, one thread starts creating the job queue and another thread starts to get job from the queue and feed them to all the connected slaves one by one. Once the slave finishes the job, it will send some acknowledgement to the master that it is free and master again feeds the job to that client. If queue is empty master just sends some acknowledgement to the client after which client again sends some acknowledgement to the master and master again checks the queue and send the job if available. This process keeps on going until the master decides that all the jobs are done.
Acknowledgement is nothing but some constant value. Whenever client receives this constant value client sends the same value back to the master and if master receives this constant values it sends the job if available in the queue else send the same value back to the client. This way master and slaves keep on co开发者_运维百科mmunicating until the jobs are all done.
I am working with c# windows server using asynchronous sockets.
My problem is while handling data being received on client side, some time client is getting overlapping values. It is happening in the following event of client socket:
public void OnDataReceived(IAsyncResult asyn)
{
SocketPacket theSockId = null;
try
{
theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
if (OnDataRecievedFromMaster != null)
{
OnDataRecievedFromMaster(Convert.ToInt32(szData), theSockId.hostName);
}
//richTextRxMessage.Text = richTextRxMessage.Text + szData;
WaitForData(theSockId.hostName);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
if (se.ErrorCode == 10054) // Error code for Connection reset by peer
{
//string msg = "Client " + socketData.hostName + " Disconnected" + "\n";
//AppendToRichEditControl(msg);
// Remove the reference to the worker socket of the closed client
// so that this object will get garbage collected
}
}
finally
{
}
}
on this event, the value of szdata is not the original value what master really sent. Most of the time the value is repeated. I am only sending integer values to client or server, no strings.
Any kind of input on why I am getting this will really be appreciated.
How are you scheduling the two threads - master and slave, if you let both of them run without any synchronization there may be a issue. Suppose the slave keeps on checking for the data without the master giving it, in that case you could end up with multiple repeated values on the client end. The fact that you are getting this error only sometimes and not always points to a concurrency bug.
精彩评论