Sign in to follow this  

.NET socket mystery

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a socket server I've been creating... kind of learning as I go. All seemed great until i moved it over to my Linux box ( with mono ), and when i lost connection... BEHOLD! It didn't detect a disconnect! I've tried everything in my code to detect this, and searched the object up and down... but still no luck. Does anyone know why if I cut my net connection, or if I disconnect/reconnect fast enough over and over, it doesn't detect the disconnection? Does anyone know of a cure? Here is a watered down version of my loop: If I add polling, or check some properties... nothing seems to indicate that i've disconnected... but I have!
if (clientListMut.WaitOne())
{
  for (int i = clientList.Count - 1; i >= 0; i--)
  {
    if ( !clientList[i].alive || !clientList[i].socket.Connected )
    {
      // The socket has died. Lets remove that client.
      // Create a temporary reference so we can access it right after removing it from the array.
      NetworkClient tmpObj = clientList[i];
      clientList.RemoveAt(i);

      System.Console.WriteLine("Connection Disconnected: " + tmpObj.remoteEndPoint.ToString());

      if (userOnDisconnect != null)
      {
        userOnDisconnect(tmpObj);
      }

      tmpObj.destroy();
    }
    else if ( clientList[i].socket.Poll(1, SelectMode.SelectRead) )
    {
      clientList[i].read();
    }
  }
  clientListMut.ReleaseMutex();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi

.NET Only updates the isConnected property after a send/receive , thus the only way you can detect a disconnect is to use the following code before you try to send/receive on the connection :

/* pvConnection is a Socket Object
*/


if(pvConnection.Poll(10000,System.Net.Sockets.SelectMode.SelectRead))
{
if(pvConnection.Available==0)
{
//The socket is disconnected/gone -> Reconnect or do whatever ....
}
}


Hope this helps

Thanks
Le Roi

Share this post


Link to post
Share on other sites
Thanks! Your suprisingly simple explanation has shed some light on my problem. I seem to have corrected it...

Can you post or PM me your paypal account id and I will send you $20?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi

No need for payment , your thanks was enough ;)

in any case I also struggled with this before finding the solution ,
so the least I can do is share it.

Thanks
Le Roi

Share this post


Link to post
Share on other sites

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this