27303

How to simulate a blocking send() using WSASend() and IOCP?

Question:

<strong><em>Edit:</em></strong> This question is not necessary, as the WSASend() function can inherently be used in a blocking mode, even if the socket has the overlapped attribute and is associated with a completion port. To use it in a blocking mode: <strong>just don't supply an overlapped structure or a completion routine when calling WSASend()</strong>.

<hr />

I want to make sure that when I send data, the function will only return when this data has been placed in the send buffer. So this is what I come up with (pseudo code):

void WSASend_Block(char *arr, int length) { OVERLAPPED overlapped; overlapped.hEvent = someEvent; int result = WSASend(arr, length, &overlapped); while(true) { if (result == 0) // IO operation has been scheduled { wait(overlapped.hEvent, INFINITE); // block until data is placed into send buffer break; } else { result = WSASend(arr, length, &overlapped); } } }

Answer1:

Why would you want to do this?

What do you think it achieves?

From the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms742203(v=vs.85).aspx" rel="nofollow">MSDN documentation</a> for WSASend

<blockquote>

For sockets with the overlapped attribute, WSASend uses overlapped I/O unless both the lpOverlapped and lpCompletionRoutine parameters are NULL. In that case, the socket is treated as a non-overlapped socket.

</blockquote>

So, simply don't supply a completion routine OR an OVERLAPPED structure and the call will become a "non overlapped" call. Which is what you want, a call which blocks until the data is copied into the network stack's send buffer...

Also, I hope the code in question is NOT using an IOCP as it will crash horribly if it is, maybe not now, but eventually. You have a race condition. If you're using IOCP then the OVERLAPPED structure MUST exist until after the completion occurs and is processed by a thread calling GetQueuedCompletionStatus(). You could use overlapped I/O this way, just do NOT associate the socket with an IOCP. The fact that you wait for the event to be signalled does not mean that one of your threads blocking on GetQueuedCompletionStatus() will have retrieved the completion and dealt with it and finished touching the OVERLAPPED structure that you have created on the stack before the call waiting on the event completes and that function returns.

Recommend

  • Use event arguments with other arguments?
  • Local eventEmitter listening to global eventEmitter
  • ReadFile only reading one buffer character?
  • Sockets using GetQueuedCompletionStatus and ERROR_MORE_DATA
  • How to tell openmp not to synchronize an array
  • How to tell which value triggered RegNotifyChangeKeyValue?
  • BoundingBoxes for Collision Detection overlapping and causing issues
  • std::system Exception when instantiating a singleton object
  • Load 24 bit TGA
  • Get the App path without the app name at the end of the app
  • Partial specialization of a class template in derived class affects base class
  • Arduino making decision according to a packet received from serial port
  • Invert string in Rust
  • Changing a global variable in C
  • Inversing an interpolation of rotation
  • How to unpack 32bit integer packed in a QByteArray?
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • How to read piped content in C?
  • Overlapping controls in Windows XP
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • How does this usort cmp function actually work?
  • Appending Character to Character Array In C
  • AES padding and writing the ciphertext to a disk file
  • Convert array of 8 bytes to signed long in C++
  • Rearranging Cells in UITableView Bug & Saving Changes
  • retrieve vertices with no linked edge in arangodb
  • Linker errors when using intrinsic function via function pointer
  • Acquiring multiple attributes from .xml file in c#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?