Is there a way to poll a socket in C # only when something is readable?

I am wondering if there is a way to poll a socket in C # when only one of the conditions (data is readable) is satisfied, I know about socket.Poll , but it can return true if any of the three specified conditions returns true, as indicated here : MSDN: Socket.Poll

+3
source share
6 answers

According to the MSDN documentation, there are three reasons that return true for

Poll(microSeconds, SelectMode.SelectRead);

  • if it Listen()was called and the connection is pending
  • If read data is available
  • If the connection was closed, reset or terminated

, :

  • , Listen(), , .
  • , .
  • , Poll() , . - Poll().

:

  • 3. , , true.

, (untested):

if (s.Poll(microSeconds, SelectMode.SelectRead)))
{
  if (!s.Connected)
    // Something bad has happened, shut down
  else
    // There is data waiting to be read"
}
+5

Socket Available. .

+4

- NetworkStream. NetworkStream.DataAvailable true, . , TcpListener TcpClient. , .

Socket NetworkStream. NetworkStream . , .

+1

select() .

+1

Select() Poll(). Socket.Poll ILSpy ( ) .

, Poll() , IntPtr []. Select() .

+1

true if Listen was called and the connection is waiting; -or-true if the data is readable; -or-true if the connection was closed, reset, or terminated; otherwise returns false.

I understand that you want to check if the second option returns true? After checking that the polling response is correct, you can check if the connection is open, which means; not connection closed, reset or completed.

If it is open, then the second parameter returns true.

0
source

Source: https://habr.com/ru/post/1714675/


All Articles