Discussion:
CSocket connect, CAsyncSocket does not
(too old to reply)
afmiga
2006-03-05 09:34:26 UTC
Permalink
I am trying to use CSocket::OnReceive() but have seen lots of postings that
the MFC under VC++ 6.0 (I have SP6) may have stability problems with this
class and suggestions that the CAsyncSocket should be used instead. The
problem is that Connect() did work fine with my CSocket but it returns error
10035 with CAsyncSocket. I have traced the issue to differences in
ConnectHelper() which is called from Connect(). The ConnectHelper() is
overridden in CSocket and it does some message manipulation before calling
CAsyncSocket::ConnectHelper() which then succeeds.
At this point I am unable to discover what the issue is that the
CAsyncSocket::Connect() would fail, but CSocket::Connect() works.
No other code changes, only "CSocket socket;" to "CAsyncSocket socket;"
causes the different behavior.
Thanks in advance for ideas or hints.
--
Andy Miga
unknown
2006-03-05 09:49:03 UTC
Permalink
Error 10035 is not in fact an error, but information. It is telling you that
the function would block if it were to wait for completion of the Connect.
The connect is still going on in the background after the function returns.
You will get a call to OnConnect() when it completes. In order to get that
call, you must derive a new class from CAsyncSocket and override OnConnect
as a virtual function.
Post by afmiga
I am trying to use CSocket::OnReceive() but have seen lots of postings that
the MFC under VC++ 6.0 (I have SP6) may have stability problems with this
class and suggestions that the CAsyncSocket should be used instead. The
problem is that Connect() did work fine with my CSocket but it returns error
10035 with CAsyncSocket. I have traced the issue to differences in
ConnectHelper() which is called from Connect(). The ConnectHelper() is
overridden in CSocket and it does some message manipulation before calling
CAsyncSocket::ConnectHelper() which then succeeds.
At this point I am unable to discover what the issue is that the
CAsyncSocket::Connect() would fail, but CSocket::Connect() works.
No other code changes, only "CSocket socket;" to "CAsyncSocket socket;"
causes the different behavior.
Thanks in advance for ideas or hints.
--
Andy Miga
afmiga
2006-03-05 19:47:04 UTC
Permalink
I did it as suggested using class wizard. But my callbacks are never called.
I do the following:
AfxSocketInit();
MySocket socket; // derived from CAsyncSocket{}
socket.Create();
socket.AsyncSelect(); // optional, makes no difference
socket.Connect();
Sleep (1000);
socket.Send(); // send authentication
Sleep (1000);
socket.Receive(); // receive confirmation
// not shown the OnConnect(), OnReceive() handlers.
The Sleep()s should be enough to give the callbacks a chance to be called.
But they never are. The Send() and Receive() do work as expected and I get
the desired response from the server.
I can send you the code. The server is available over the Internet. I have a
dummy temp account, so it will be ok for me to give the authentication info
for test.
My address is ***@comcast.net. Let me know if you will be able to help.
--
Andy Miga
Post by unknown
Error 10035 is not in fact an error, but information. It is telling you that
the function would block if it were to wait for completion of the Connect.
The connect is still going on in the background after the function returns.
You will get a call to OnConnect() when it completes. In order to get that
call, you must derive a new class from CAsyncSocket and override OnConnect
as a virtual function.
Scott McPhillips [MVP]
2006-03-05 21:59:49 UTC
Permalink
Post by afmiga
The Sleep()s should be enough to give the callbacks a chance to be called.
But they never are.
The CAsyncSocket notifications do not work the way you are assuming.
OnConnect, OnReceive, OnSend are not callbacks. They are message
handlers that are called by the MFC message pump. So they can only be
called after you have returned to the MFC message pump.

Your Sleep() calls simply freeze your thread and its message pump so
absolutely nothing can happen.

Build an event-driven program instead of a procedural program. If the
Connect() call does not immediately succeed then return. The OnConnect
call should then come in later. Meanwhile, your program can pump other
messages to keep the GUI alive.

After you send the authentication, return. The OnReceive call should
come in later, when the response has arrived. Meanwhile, your program
can pump other messages....
--
Scott McPhillips [VC++ MVP]
Joseph M. Newcomer
2006-03-08 01:35:45 UTC
Permalink
I've never seen this problem. Note that 10035 is WSA_EWOULDBLOCK, an *expected* error
return; it means the connection process is underway, and your OnConnect handler will be
called when the conneciton is made, or when it fails (look at the parameter passed into
OnConnect to see if the connection actually was made). The differences you see are rather
important.

If you are doing this in a thread, you have to do it in a UI thread, because sockets
require a functioning message pump to handle the dispatching of the callbacks.
joe
Post by afmiga
I am trying to use CSocket::OnReceive() but have seen lots of postings that
the MFC under VC++ 6.0 (I have SP6) may have stability problems with this
class and suggestions that the CAsyncSocket should be used instead. The
problem is that Connect() did work fine with my CSocket but it returns error
10035 with CAsyncSocket. I have traced the issue to differences in
ConnectHelper() which is called from Connect(). The ConnectHelper() is
overridden in CSocket and it does some message manipulation before calling
CAsyncSocket::ConnectHelper() which then succeeds.
At this point I am unable to discover what the issue is that the
CAsyncSocket::Connect() would fail, but CSocket::Connect() works.
No other code changes, only "CSocket socket;" to "CAsyncSocket socket;"
causes the different behavior.
Thanks in advance for ideas or hints.
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Loading...