Click to See Complete Forum and Search --> : [RESOLVED] Multithreaded Socket


DrPowers
May 30th, 2006, 07:47 AM
I'm having some problems moving my socket to a new thread so it can run independently to the rest of my application.

I'm using AsyncSocket and WinThread.

So far I've

instantiated the socket (with connection)
created the new thread (in suspended mode)
copied the socket to the thread
and started the thread...


As far as I can tell its working fine but I cant seem to figure out how to catch the onReceive calls within the new thread.

When the socket was created within the original (Main Thread) it was created from an overloaded socket class (CMySocket) which I defined the behavior for onReceive calls. But it was saved as a CAsyncSocket within the new thread.

So... how do I define the onReceive behaviors etc.


Thanks in advance.

MikeAThon
May 30th, 2006, 01:08 PM
For CAsyncSocket, your thread must be a UI thread. See this post: http://www.codeguru.com/forum/showthread.php?p=1397934#post1397934

Mike

DrPowers
May 30th, 2006, 01:19 PM
I've gotten that far already.

My Derived Socket class:
CMySocket public : CAsyncSocket{
CSocketThread *thread;

}

My Derived Thread class
CSocketThread : CWinThread{

CAsyncSocket m_pMySocket;
SOCKET m_socket;


}

This is my problem... I attach the socket to my new thread:

CAsyncSocket m_pMySocket;
SOCKET m_socket;

m_pMySocket.Attach(m_socket);

I'm trying to figure out how to define the behaviors of the onReceive() onSend() etc functions for a data member that is the base Class type.

Should I somehow instantiate m_pMySocket to be CMySocket rather than CAsyncSocket?

This threw me errors since CMySocket contains a CSockThread and CSockThread would contain a CMySocket

Or should I just create a dummy class to create and accept the connection then pass it to the thread and have it assigned to a CMySocket type?

Please help.... I dont understand this very well.

DrPowers
May 31st, 2006, 12:48 PM
Nevermind.... I figured it out... I just needed an additional Socket class as a listener.