I have a server-client using WCF.
The server has a list of clients.
To check for timeouts, every minute it sends a bit of data (ping) to each client. All sending is done asynchronously - so the line looks like this:
for (int i = 0; i < MaxUsers; i++)
{
if (_clients[i] != null)
{
_clients[i].Client.BeginSendToClient("PING", new AsyncCallback(this.SendToClientCallback), _clients[i].Client);
Now the SentToClientCallback method starts off like this:
void SendToClientCallback(IAsyncResult asyncResult)
{
IClientAsync callback = null;
try
{
callback = (IClientAsync)asyncResult.AsyncState;
callback.EndSendToClient(asyncResult);
}
catch (TimeoutException e)
The timeout exception has to convert the result we sent back to get the index of the client in our _client array... and it does so using a method I wrote:
int myClientIndex = GetClientIndexFromCallback(CurrentCallback);
where
private int GetClientIndexFromCallback(IClientAsync callback)
{
for (int i = 0; i < MaxUsers; i++)
{
if (_clients[i] != null && _clients[i].Client.Equals(callback))
{
// found
return i;
Now this code works fine if I connect, then disconnect. It sends the PING, goes to the callback method. It generates a timeout exception. It gets the id number (which is 0) and removes _client[0] and all is good.
However, if I connect and disconnect twice in a row, before the first connect timed out, only one of the connections (the second one, or _client[1]) disconnects. The other one remains, and constantly gets sent PING.
The callback method keeps running, and keeps generating a timeout exception... but when I call my method to get the id of it, it cannot find it. For some reason, _clients[i].Client.Equals(callback)
is not returning true.
How is this possible? Every PING command sends _clients[0].Client
, but when I go to compare it in my method, it returns false.