0

MSDN says that AcceptEx() may return TRUE, but I was never able to reproduce this.

If AcceptEx() returns TRUE, will hEvent be set? is it safe to call GetOverlappedResult() after AcceptEx() returns TRUE?

Is it the same for other functions like ReadFile()?

basin
  • 3,949
  • 2
  • 27
  • 63
  • This is technically possible on *all* overlapped I/O operations. Unlikely for AcceptEx() and difficult to test. Quite likely for Readfile(). No, don't use GetOverlappedResult(), it was already completed without using an overlapped operation. – Hans Passant Aug 21 '13 at 12:31
  • How to reproduce it with ReadFile? – basin Aug 21 '13 at 12:49
  • Even without FILE_FLAG_OVERLAPPED, ReadFile updates the overlapped structure and sets the event. I think it's safe to call GetOverlappedResult() in any case – basin Aug 21 '13 at 12:58
  • It is not unlikely that Microsoft added this to help programmers that ignore advice from Q+A sites. Whether that still works on an old version of Windows is something you'll find out, don't forget to let us know. – Hans Passant Aug 21 '13 at 13:06
  • 2
    `AcceptEx` will return `TRUE` if there's already a connection waiting to be accepted and you haven't passed it a receive buffer. Consider the `backlog` argument of `listen()`. It's not too difficult to test. – Cory Nelson Aug 21 '13 at 14:30

2 Answers2

1

At least for ReadFile(socket) it's like that:
If ReadFile() succeeds or fails with ERROR_IO_PENDING, the event is set.
If the connection is closed before calling ReadFile(), it fails, and the event is not set.

basin
  • 3,949
  • 2
  • 27
  • 63
0

From what I can tell from the MSDN pages on AcceptEx and the OVERLAPPED structure, when AcceptEx completes it should set the OVERLAPPED::hEvent handle to signalled.

From MSDN page of OVERLAPPED

A handle to the event that will be set to a signaled state by the system when the operation has completed. The user must initialize this member either to zero or a valid event handle using the CreateEvent function before passing this structure to any overlapped functions.

Its talking fairly broadly here, which I would say is safe to assume it applies to all functions taking an OVERLAPPED structure.

If your AcceptEx never returns true, it may be that you have a bug in your code. Unless you post actual code, will be hard to tell what that might be.

In the same page on OVERLAPPED it says this about ReadFile

Functions such as ReadFile and WriteFile set this handle to the nonsignaled state before they begin an I/O operation. When the operation has completed, the handle is set to the signaled state.

With regards to calling GetOverlappedResult it also states specifically what to do:

Functions such as GetOverlappedResult and the synchronization wait functions reset auto-reset events to the nonsignaled state. Therefore, you should use a manual reset event; if you use an auto-reset event, your application can stop responding if you wait for the operation to complete and then call GetOverlappedResult with the bWait parameter set to TRUE.

Like @HansPassant said in the comments, don't use it.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415