C# using Overlapped IO even when I don't ask it to?

2

I have a System.Net.Sockets.Socket that is constructed like so:

ClientSocket = new Socket(AddressFamily.InterNetwork,
                          SocketType.Stream,
                          ProtocolType.Tcp);

Later on, I read from it in what I believe to be a synchronous manner:

bytesReceived = ClientSocket.Receive(aBuffer, aOffset, aSize, SocketFlags.None);

This will work great for many thousands of calls. Then, at some point for reasons I do not understand, I will get a SocketException with the following details:

SocketErrorCode - IOPending ErrorCode - 996 hresult: -2147467259 (0x80004005) Message: Overlapped I/O operation is in progress

In trying to learn more about Overlapped I/O I see it referenced mainly with async network operations. I believe I'm using a synchronous/blocking version of Receive so it's unclear to me why this exception would reference Overlapped IO.

Any advice? This exception happens very infrequently so gathering new data each time it happens can take days or weeks. I'm hoping there's something I'm missing or misunderstanding that can lead to a more correct approach.

As it is now, I'm working to detect this unexpected error & retry the Receive when this happens.

To answer some questions below:

  1. All calls to Receive are handled on the same thread, but it's a different thread from the thread that created the Socket. The socket is created in the constructor and then a long running Task is spawned to repeatedly call Receive.
  2. I don't have the sizes that were used when this failure happened.
c#
sockets
network-programming
asked on Stack Overflow Nov 7, 2018 by Dave • edited Nov 7, 2018 by Dave

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0