Thursday, July 31, 2014

StreamSocket Disconnection handling in Universal app (Client)

Can anyone help with how to detect disconnections on StreamSocket in C# Universal App I am developing for WP8 and Store.


I have already gone though various resources online and I do understand that in case of conditions like going out of WiFi range, switching off WiFi or other kinds of abrupt disconnections, I cannot rely on the read operation returning with a 0 length read.


What I cannot understand is the fact that even when I try to write to StreamSocket during such a disconnection situation, I am not getting any exception.


I am using the InputStream and OutputStream properties of StreamSocket directly for read and write. My write code is as follows



socket.OutputStream.WriteAsync(dataBuffer);
socket.OutputStream.FlushAsync();

Neither of these lines is throwing an exception. I also put handlers on the async operations and both operations have status Completed.

So how do I detect disconnection. My custom binary messaging protocol includes a health check message which gets sent every 5 sec and the connection is reset when there is no response for 15 sec. What I expected is that there would be some sort of detectable failure when the write is done, but its not happening.


As my client is sensitive to time, I need to be able to detect disconnections as and when it happens and initiate reconnection if required.





No comments:

Post a Comment