On full .Net Framework I use the following code:
socket.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, readTimeout);
socket.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.SendTimeout, writeTimeout);
However, Windows Mobile does not support this and throws exceptions.
I am currently in the middle of testing this solution for implementing timeouts.
Does anyone know a better way? I'd like to avoid spawning multiple threads if possible, this is an embedded device after all.
Best Solution
This code works, raising timeouts when expected (it is a modified version of the example I linked in the question):
The crucial element missing from the example I found is
Socket.Select(IList checkRead, IList checkWrite, IList checkError, int microSeconds)
. Bear in mind that this method may modify the list that is passed to it (that's why my code creates a new one each time) and measures time in microseconds instead of milliseconds. And remember to useEnvironment.TickCount
(which is a monotonic time source) instead ofDateTime.Now
for measuring time.