Jump to content
  • Advertisement
Sign in to follow this  
Volte6

.NET socket mystery

This topic is 4419 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.alive || !clientList.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;
      clientList.RemoveAt(i);

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

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

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

Share this post


Link to post
Share on other sites
Advertisement
If somebody can help me come to an exact solution ( ha, big request i know! ) I'll gladly send over $20 via paypal.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!