Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use CAsyncSocket::OnReceive in a DLL ?

177 views
Skip to first unread message

Stéphane Hurtrel

unread,
Jan 28, 2002, 10:19:30 AM1/28/02
to
Hello,
I am using the CAsyncSocket class in a DLL, which is called by an
other DLL.
I have overridden OnReceive to use the event notification.

How it works:
- first, a window MFC program loaded a dll A, next this dll A loaded
my dll B(CAsyncSocket).
- I send data with Send.
- The receive notification comes when the window MFC program recover
the hand.

How can I do to receive event notification in the dll process ??

Kramer

unread,
Jan 28, 2002, 3:12:03 PM1/28/02
to
Hi Stéphane,

I am writing a similar application and am having a similar
problem. What I do know about event notifications is that
they will only be received by the thread that actually
creates the CAsyncSocket that has the appropriate socket
attached to it. If your writing a single threaded
application you should not have problems receiving the
notifications as long as you call AsyncSelect with the
correct bit mask, typically: AsyncSelect(FD_READ|FD_CLOSE).

If you want the notifications to be sent to a different
thread that recives the accept notification then you must
first detach the socket handle within OnAccept and call
Attach within the thread that then reads data from the
socket. Have a quick look at my posting
entitled "CAsyncSocket in a Multithreaded application".
Hopefully, someone will know the answer to my problem
which should also fix yours or vice-versa.

By the way, I think these notifications were designed to
work within a process boundary. The reason I mention this
is, you talk about receiving an event notificion in
the "dll process" which implies your writing a multi-
process application as opposed to a multi-threaded
application.

>.
>

Kramer

unread,
Jan 29, 2002, 7:31:08 AM1/29/02
to
Hi again,

after much head scratching I have figured out what my
problem was and hopefully it may also apply to yours. (I'm
assuming you've read my posting "CAsysnSocket in a
multithreaded application"). The reason why I was not
receiving notifications was because I was detaching the
wrong socket:

void CListenSocket::OnAccept(int nErrorCode)
{
socket hSocket;

CAsyncSocket *pRequest = new CAsyncSocket();
if (Accept(*pRequest))
{
hSocket = Detach();
}
...
}

The Detach() here is called on the wrong async socket,
which now explains why my server application only worked
for the first client connect attempt. (In the first client
connect, the listening socket is detached from the
listening thread, so after this point the listen thread no
longer receives notifications from the listen socket).

The code I should have written is:

hSocket = pRequest->Detach();

What I do from here, is to send the socket handle to
another thread using PostThreadMessage, and when the new
thread receives the message, I retrieve the socket handle,
create a new async socket and attach the socket handle to
the socket:

hSocket = (extract from the thread message)

CAsyncSocket *pSocket = new CAsyncSocket();

if (pSocket)
{
pSocket->Attach(hSocket);
pSocket->AsyncSelect(FD_READ|FD_CLOSE);
}


Notification process between different threads now works
as required.

>.
>

Joseph M. Newcomer

unread,
Jan 29, 2002, 6:26:41 PM1/29/02
to
There is no concept of "the dll process". This is a semantically empty phrase. There is
exactly one process. Once you load the DLL, the code is all part of the same process and
is essentially all mooshed together. To a first approximation, the kernel neither knows
nor cares where the call came from, DLL or EXE.

CAsyncSocket requires a message pump. If your message pump is not running, no
notifications will be processed. If you do a Send, then go off and compute for an hour
before returning to the GUI thread, no notifications will be seen for an hour.

One way around this is to extract the raw socket from the CAsyncSocket object using
Detach, create a UI thread and pass the raw socket into the UI thread and reattach it to a
CAsyncSocket within the thread (this is important because the socket-to-object map is
thread-local). Then the message pump is independent of the main GUI thread. I did this
when we were having problems because users were doing things that caused messages to be
lost.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

0 new messages