Hi all,
I view many posts about this issue , the connected property does not tell us the current status of the socket.
based on couple of suggestions of msdn , and some article here , I try to write an helper method that will tell if the socket is connected or not , but it's not working good
continue to tell me that the socket is connectedeven if the other party already call shutdown(both) + close , or , even if the other party close the app itslef.
also I determine different behaviour when my app running behind NAT
when I run behind NAT , calling to socket.Receive( ..) throw exception which was good forme to determine that it's already closed the connection , it doesn't throw nothing when I run the same code without NAT
Here is my code:
/// <summary>
/// Determine if the givven socket is connected or not
/// </summary>
/// <remarks>
/// There is a problem to based the Socket.Connecte d property since it's show the last operaion status: (from msdn)
/// The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.
/// The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
/// </remarks>
/// <seealso cref="http://groups.google.c om/group/microsoft.publi c.dotnet.langua ges.csharp/browse_thread/thread/3cf03e064173165 9/76e2563d28f1b25 6?lnk=st&q=c%23 +determine+if+s ocket+connected &rnum=5#76e2563 d28f1b256"/>
/// <seealso cref="http://windowssdk.msdn .microsoft.com/en-us/library/system.net.sock ets.socket.conn ected.aspx"/>
/// <returns>Bool , True if connected , False if not</returns>
public static bool IsConnected(Soc ket checkSocket)
{
try
{
//if (checkSocket.Ge tSocketOption(S ocketOptionLeve l.Socket, SocketOptionNam e.KeepAlive, 1)[0].Equals(1))
// return checkSocket.Con nected;
if (checkSocket.Co nnected == false)
return false;
//checkSocket.Beg inSend(new byte[0], 0, 0, SocketFlags.Non e, null, null);
bool bSelectRead = checkSocket.Pol l(1, SelectMode.Sele ctRead);
bool bSelectWrite = checkSocket.Pol l(1, SelectMode.Sele ctWrite);
int available = checkSocket.Ava ilable;
//if (bSelectWrite && bSelectRead && available 0)
if (bSelectWrite && bSelectRead)
{
//return true;
//checkSocket.Beg inReceive(new byte[1], 0, 1, SocketFlags.Pee k, null, null);
checkSocket.Rec eive(new byte[0], 0, 0, SocketFlags.Pee k);
checkSocket.Sen d(new byte[0], 0, 0, SocketFlags.Non e);
return checkSocket.Con nected;
}
else
return false;
}
catch (SocketExceptio n)
{
return false;
}
catch (ObjectDisposed Exception)
{
return false;
}
}
here is MS code , that also don't work - tell that the socket is connected even if the other party already close...
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking ;
try
{
byte [] tmp = new byte[1];
client.Blocking = false;
client.Send(tmp , 0, 0);
Console.WriteLi ne("Connected!" );
}
catch (SocketExceptio n e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorC ode.Equals(1003 5))
Console.WriteLi ne("Still Connected, but the Send would block");
else
{
Console.WriteLi ne("Disconnecte d: error code {0}!", e.NativeErrorCo de);
}
}
finally
{
client.Blocking = blockingState;
}
Console.WriteLi ne("Connected: {0}", client.Connecte d);
I view many posts about this issue , the connected property does not tell us the current status of the socket.
based on couple of suggestions of msdn , and some article here , I try to write an helper method that will tell if the socket is connected or not , but it's not working good
continue to tell me that the socket is connectedeven if the other party already call shutdown(both) + close , or , even if the other party close the app itslef.
also I determine different behaviour when my app running behind NAT
when I run behind NAT , calling to socket.Receive( ..) throw exception which was good forme to determine that it's already closed the connection , it doesn't throw nothing when I run the same code without NAT
Here is my code:
/// <summary>
/// Determine if the givven socket is connected or not
/// </summary>
/// <remarks>
/// There is a problem to based the Socket.Connecte d property since it's show the last operaion status: (from msdn)
/// The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.
/// The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
/// </remarks>
/// <seealso cref="http://groups.google.c om/group/microsoft.publi c.dotnet.langua ges.csharp/browse_thread/thread/3cf03e064173165 9/76e2563d28f1b25 6?lnk=st&q=c%23 +determine+if+s ocket+connected &rnum=5#76e2563 d28f1b256"/>
/// <seealso cref="http://windowssdk.msdn .microsoft.com/en-us/library/system.net.sock ets.socket.conn ected.aspx"/>
/// <returns>Bool , True if connected , False if not</returns>
public static bool IsConnected(Soc ket checkSocket)
{
try
{
//if (checkSocket.Ge tSocketOption(S ocketOptionLeve l.Socket, SocketOptionNam e.KeepAlive, 1)[0].Equals(1))
// return checkSocket.Con nected;
if (checkSocket.Co nnected == false)
return false;
//checkSocket.Beg inSend(new byte[0], 0, 0, SocketFlags.Non e, null, null);
bool bSelectRead = checkSocket.Pol l(1, SelectMode.Sele ctRead);
bool bSelectWrite = checkSocket.Pol l(1, SelectMode.Sele ctWrite);
int available = checkSocket.Ava ilable;
//if (bSelectWrite && bSelectRead && available 0)
if (bSelectWrite && bSelectRead)
{
//return true;
//checkSocket.Beg inReceive(new byte[1], 0, 1, SocketFlags.Pee k, null, null);
checkSocket.Rec eive(new byte[0], 0, 0, SocketFlags.Pee k);
checkSocket.Sen d(new byte[0], 0, 0, SocketFlags.Non e);
return checkSocket.Con nected;
}
else
return false;
}
catch (SocketExceptio n)
{
return false;
}
catch (ObjectDisposed Exception)
{
return false;
}
}
here is MS code , that also don't work - tell that the socket is connected even if the other party already close...
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking ;
try
{
byte [] tmp = new byte[1];
client.Blocking = false;
client.Send(tmp , 0, 0);
Console.WriteLi ne("Connected!" );
}
catch (SocketExceptio n e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorC ode.Equals(1003 5))
Console.WriteLi ne("Still Connected, but the Send would block");
else
{
Console.WriteLi ne("Disconnecte d: error code {0}!", e.NativeErrorCo de);
}
}
finally
{
client.Blocking = blockingState;
}
Console.WriteLi ne("Connected: {0}", client.Connecte d);
Comment