S
semedao
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.Connected 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.com/group/micr...e+if+socket+connected&rnum=5#76e2563d28f1b256"/>
/// <seealso cref="http://windowssdk.msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx"/>
/// <returns>Bool , True if connected , False if not</returns>
public static bool IsConnected(Socket checkSocket)
{
try
{
//if (checkSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1)[0].Equals(1))
// return checkSocket.Connected;
if (checkSocket.Connected == false)
return false;
//checkSocket.BeginSend(new byte[0], 0, 0, SocketFlags.None, null, null);
bool bSelectRead = checkSocket.Poll(1, SelectMode.SelectRead);
bool bSelectWrite = checkSocket.Poll(1, SelectMode.SelectWrite);
int available = checkSocket.Available;
//if (bSelectWrite && bSelectRead && available > 0)
if (bSelectWrite && bSelectRead)
{
//return true;
//checkSocket.BeginReceive(new byte[1], 0, 1, SocketFlags.Peek, null, null);
checkSocket.Receive(new byte[0], 0, 0, SocketFlags.Peek);
checkSocket.Send(new byte[0], 0, 0, SocketFlags.None);
return checkSocket.Connected;
}
else
return false;
}
catch (SocketException)
{
return false;
}
catch (ObjectDisposedException)
{
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.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}
Console.WriteLine("Connected: {0}", client.Connected);
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.Connected 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.com/group/micr...e+if+socket+connected&rnum=5#76e2563d28f1b256"/>
/// <seealso cref="http://windowssdk.msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx"/>
/// <returns>Bool , True if connected , False if not</returns>
public static bool IsConnected(Socket checkSocket)
{
try
{
//if (checkSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1)[0].Equals(1))
// return checkSocket.Connected;
if (checkSocket.Connected == false)
return false;
//checkSocket.BeginSend(new byte[0], 0, 0, SocketFlags.None, null, null);
bool bSelectRead = checkSocket.Poll(1, SelectMode.SelectRead);
bool bSelectWrite = checkSocket.Poll(1, SelectMode.SelectWrite);
int available = checkSocket.Available;
//if (bSelectWrite && bSelectRead && available > 0)
if (bSelectWrite && bSelectRead)
{
//return true;
//checkSocket.BeginReceive(new byte[1], 0, 1, SocketFlags.Peek, null, null);
checkSocket.Receive(new byte[0], 0, 0, SocketFlags.Peek);
checkSocket.Send(new byte[0], 0, 0, SocketFlags.None);
return checkSocket.Connected;
}
else
return false;
}
catch (SocketException)
{
return false;
}
catch (ObjectDisposedException)
{
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.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}
Console.WriteLine("Connected: {0}", client.Connected);