CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006
    Posts
    53

    [RESOLVED] Multithreaded Socket

    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.

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Multithreaded Socket

    For CAsyncSocket, your thread must be a UI thread. See this post: http://www.codeguru.com/forum/showth...34#post1397934

    Mike

  3. #3
    Join Date
    May 2006
    Posts
    53

    Re: Multithreaded Socket

    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.

  4. #4
    Join Date
    May 2006
    Posts
    53

    Re: Multithreaded Socket

    Nevermind.... I figured it out... I just needed an additional Socket class as a listener.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured