I put this tiny snippet of code into a thread
that I'm starting with AfxBeginThread, and suddenly
the thread crashes the program.
CSocket sock;
if (!sock.Create (0, SOCK_STREAM, NULL)) {
return 1;
}
It bombs inside the call to Create. I believe the error
is illegal access to the wrong part of memory.
Does anybody know what I'm doing wrong?
Thanks.
I may be mistaken but I'm sure if read somewhere that AfxSocketInit()
has to be called in each thread that will use the socket libraries. You
may already be doing this but if you're not...
--
Fenster
CMapPtrToPtr::GetValueAt(void * 0x00000754) line 179 + 3 bytes
CAsyncSocket::LookupHandle(unsigned int 1876, int 0) line 385 + 15
bytes
CAsyncSocket::AttachHandle(unsigned int 1876, CAsyncSocket * 0x00de7d84
{CAsyncSocket}, int 0) line 407 + 13 bytes
CAsyncSocket::Socket(int 1, long 0, int 0, int 2) line 557
CAsyncSocket::Create(unsigned int 0, int 1, long 0, const char *
0x00000000) line 105 + 20 bytes
MyThread (void * 0x0011df80) line 543 + 19 bytes
_AfxThreadEntry(void * 0x0011debc) line 112 + 13 bytes
_threadstartex(void * 0x00aa10a0) line 212 + 13 bytes
KERNEL32! 7c80b50b()
I'm creating a multithreaded program where each
thread will be accessing services on the Internet.
My understanding is that I need to use a "UI thread" instead
of a worker thread for this, because those have message
pumps and for some reason that is needed for winsock.
However so far, I can't create a UI thread.
UI threads clearly require that I create a CWinThread-derived
class because the AfxBeginThread class requires that I pass it
a runtime class information struct.
However, when I try to derive anything from CWinThread,
coding by hand (no classwizard involved), it doesn't link.
Thanks for any help-
Well, you need to work on "it doesn't link". The MFC socket classes
definitely need the "UI" thread and the version of AfxBeginThread that
requires a runtime class. The reason is that socket notifications, such
as OnReceive and OnSend, are generated by messages that winsock posts to
the thread's message queue. Only a "UI" thread provides this message queue.
--
Scott McPhillips [VC++ MVP]
> The MFC socket classes
> definitely need the "UI" thread and the version of AfxBeginThread
that
> requires a runtime class.
I got that working, but here's a funny question: Why does MS
provide the method CreateThread if it doesn't work.
> The reason is that socket notifications, such
> as OnReceive and OnSend, are generated by messages that winsock posts
to
> the thread's message queue. Only a "UI" thread provides this message
queue.
Well, now I've got the thread running, however when it reaches
the socket creation is bombs, just as it did in the non-UI thread.
I'm still using :
CSocket sock;
if (!sock.Create (0, SOCK_STREAM, NULL)) {
return 1;
}
However I've tried CAsyncSocket; that doesn't work, and I've
tried adding the call to AfxSocketInit in the beginning of
the thread. That doesn't help either.
I'm amazed that something so simple could be so complicated.
Any help appreciated-
It does work. It is just not appropriate in most cases. Your program
is using two libraries (C runtime and MFC) that need to make provisions
for multithreading. There are rules established by the libraries.
AfxBeginThread takes care of all the rules.
> Well, now I've got the thread running, however when it reaches
> the socket creation is bombs, just as it did in the non-UI thread.
> I'm still using :
>
> CSocket sock;
> if (!sock.Create (0, SOCK_STREAM, NULL)) {
> return 1;
> }
>
> However I've tried CAsyncSocket; that doesn't work, and I've
> tried adding the call to AfxSocketInit in the beginning of
> the thread. That doesn't help either.
>
> I'm amazed that something so simple could be so complicated.
>
> Any help appreciated-
Well, clearly the problem is elsewhere than in the above code. Study
the Microsoft sample code in the link below. Part of your problem may
be that you think running a message-driven socket in a multithreaded MFC
app should be simple. It takes study and great care. To analyze the
problem you need to understand how the MFC socket classes and thread
classes work, and the requirements they impose. Based on my experience
with both I strongly recommend you use CAsyncSocket and not CSocket.
http://download.microsoft.com/download/vc60pro/Sample4/1/WIN98/EN-US/MultiSoc.exe
Thanks for this info. You mentioned that I'm using a message-
driven socket. Just curious but is there such a thing as a
non-message driven socket in Windows?
Thanks.
Also, looking at the example you pointed out, I see that
the connection is formed is the parent app, at which point
the socket id is detached and passed to the thread.
Is there a way to create the thread and form the connection
from inside the thread?
Thanks.
Yes, there are several ways. They are described in the winsock section
of the MSDN documentation.
Yes, there's nothing different about creating a CAsyncSocket or CSocket
inside a UI thread. They work the same way there that they work in the
main thread.
That sounds nice, but I'm seeing evidence to the contrary.
I wish I had a decent example to look at but everything
I've found so far has involved creating the CSocket in the
main app, connecting or listening in the main app, and then
handing a socket id to the thread.
> Yes, there are several ways. They are described in the winsock
section
> of the MSDN documentation.
It took me maybe 10 minutes to write some socket code in the
traditional Unix fashion i.e. using Windows' SOCKET datatype
and connect(), which has compiled and run without problems.
"Yef" <e9...@yahoo.com> wrote in message
news:1110663752.0...@g14g2000cwa.googlegroups.com...