Re: CAsyncSocket message handling
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 23 Sep 2009 11:44:00 -0400
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:
Joseph M. Newcomer [MVP]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 ..
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Re: CAsyncSocket message handling
- From: Joseph M . Newcomer
- Re: CAsyncSocket message handling
- From: neilsolent
- Re: CAsyncSocket message handling
- From: Stephen Myers
- Re: CAsyncSocket message handling
- From: neilsolent
- Re: CAsyncSocket message handling
- From: Joseph M . Newcomer
- Re: CAsyncSocket message handling
- From: neilsolent
- Re: CAsyncSocket message handling
- From: Stephen Myers
- Re: CAsyncSocket message handling
- From: neilsolent
- Re: CAsyncSocket message handling
- Prev by Date: Re: CMutex /CEvent (multiple threads)
- Next by Date: Re: CMutex /CEvent (multiple threads)
- Previous by thread: Re: CAsyncSocket message handling
- Next by thread: Re: CAsyncSocket message handling
- Index(es):
Relevant Pages
|