I'm wondering whether there is a way to poll a socket in c# when only one of the conditions (data is available for reading) is satisfied, I'm aware of the socket.Poll method but this can return true if any number of the 3 conditions specified returns true as specified here: MSDN: Socket.Poll
According to the MSDN documentation there are three causes that return true for
Poll(microSeconds, SelectMode.SelectRead);
Listen()
has been called and a connection is pendingLet's see if we can discriminate them:
Listen()
has been called before, so you don't need to consider that cause if you have not.Poll()
returned.Conclusion:
needs not to be considered
and 3. can be handled by checking the socket state each time true is returned.
So I would go for (untested):
if (s.Poll(microSeconds, SelectMode.SelectRead)))
{
if (!s.Connected)
// Something bad has happened, shut down
else
// There is data waiting to be read"
}
You could use Socket property Available. It returns how much data is available to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With