Re: CAsyncSocket message handling

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



That is correct. The WinSock specification says that you MUST call Shutdown() first to
guarantee that potentially pending data has been flushed. Note that the CAsyncSocket
object cannot be actually deleted until the OnClose notification has been received.

See the discussion "Graceful shutdown, linger options, and socket closure" in the MSDN. It
does say "In fact, it has become a rather common programming practice to rely on [the fact
that the closesocket implicitly causes a shutdown sequence to occur if it has not already
happened] and to use closesocket to both initiate the shutdown sequence and deallocate the
socket handle"

As I read page 130 of Quinn & Shute ("Windows Socket Network Programming") they show a
diagram where a client first does a shutdown(), and waits for the FD_CLOSE notification to
arrive (OnClose), and only then close the handle. Surprisingly (or perhaps not so),
CAsyncSocket seems remarkably silent on when it is safe to actually delete the
CAsyncSocket object. But the diagram suggests that the sequence is to handle CAsyncSocket
by a sequence

Call CAsyncSocket::Shutdown()
In OnClose, call CAsyncSocket::Close()
delete the object.

In reading the code in sockcore.cpp, it appears that after an OnXXX event, the object is
no longer used. There is also a comment about looking up already-closed handles in the
handle map. But it appears that the above sequence is what is expected.

The MSDN "Windows Sockets Sample List" is empty, except for a circular reference to the
Windows Sockets top-level topic.
joe
On Wed, 23 Sep 2009 05:05:19 -0700 (PDT), neilsolent <n@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Does calling the socket's Close() method satisfy your on demand requirement?

Steve

Yes, Close() is the call - but the issue for me is knowing when you
can call it!
I don't believe you can call Close() whenever you like - as there
might be queued up notification messages (OnReceive() callbacks etc)
which will point at a bad handle once Close() has been called, and
hence lead to a crash like the one above. We have got into this
message-pump debate, but I think it is beside the point. It is not
clear how the MFC-style application and built-in message pump does
anything to get around this - you still can't close the socket on
demand (only when a Windows message tells you to).
Unless someone knows different ..
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: shutdown hangs
    ... > On Wed, 14 Dec 2005, Peter T. Breuer wrote: ... It sounds like the shutdown sequence worked fine - though I'm ... Look at pstree to see what is happening. ...
    (comp.os.linux.misc)
  • Re: [opensuse] runlevel daemon startup shutdown sequence
    ... How does sles set the sequence? ... How can i adjust the shutdown or startup priority sles10sp3? ... When it enters the runlevel 3 naviagent starts first then followed by PowerPath. ...
    (SuSE)
  • Re: unmount of /dev failed
    ... > runs through the shut down sequence as usual, ... > rebooting or the message about the shutdown complete comes up, ... To unsubscribe, ...
    (freebsd-current)
  • Re: XP media center is plagued with issues.
    ... The shutdown seems conspicuous. ... sequence by overloading the RPC service. ... by mouse or keyboard input. ... defrage without any actual input may be causing it to trigger a standby ...
    (microsoft.public.windows.mediacenter)