K
Ken Foskey
I have a application that uses sockets to connect to a server and sends
the strings, if I close the server application then the button on the
primary application must go red so that fault tracing starts
immediately. My application will then attempt to reconnect regularly.
I have implemented a timer that will check the connect every 5 seconds
and the actual connected testing code was lifted directly from MSDN:
http://msdn.microsoft.com/en-us/library/ych8bz3x.aspx
The problem is that it does not detect the disconnect properly. If I
close the server application then this routine returns true until I
actually attempt to write to the socket.
How do I detect that the server application has stopped communicating.
(I have no control on the server application, it is third party)
Ta
Ken
/// <summary>
/// Whether this socket is connected and still connected
/// </summary>
public bool Connected
{
get
{
if (socketClient == null)
return false;
// This is how you can determine a socket is still connected.
bool blockingState = socketClient.Blocking;
bool rv = false;
try
{
byte[] tmp = new byte[1];
socketClient.Blocking = false;
socketClient.NoDelay = true;
socketClient.Send(tmp, 0, 0);
rv = true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
rv = true;
else
{
rv = false;
}
}
finally
{
socketClient.NoDelay = false;
socketClient.Blocking = blockingState;
}
return rv;
}
}
the strings, if I close the server application then the button on the
primary application must go red so that fault tracing starts
immediately. My application will then attempt to reconnect regularly.
I have implemented a timer that will check the connect every 5 seconds
and the actual connected testing code was lifted directly from MSDN:
http://msdn.microsoft.com/en-us/library/ych8bz3x.aspx
The problem is that it does not detect the disconnect properly. If I
close the server application then this routine returns true until I
actually attempt to write to the socket.
How do I detect that the server application has stopped communicating.
(I have no control on the server application, it is third party)
Ta
Ken
/// <summary>
/// Whether this socket is connected and still connected
/// </summary>
public bool Connected
{
get
{
if (socketClient == null)
return false;
// This is how you can determine a socket is still connected.
bool blockingState = socketClient.Blocking;
bool rv = false;
try
{
byte[] tmp = new byte[1];
socketClient.Blocking = false;
socketClient.NoDelay = true;
socketClient.Send(tmp, 0, 0);
rv = true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
rv = true;
else
{
rv = false;
}
}
finally
{
socketClient.NoDelay = false;
socketClient.Blocking = blockingState;
}
return rv;
}
}