CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Posts
    10

    multithreaded application using MFC

    Hi,

    I am working in a visual c++ voice chat application (dialog based application). I am using the CAsychsocket Class for the TCP/IP connection. i have a full duplex sound card but i cann't send and receive at the same time. If i send from one PC to anther i received the voice well but the application is hanging so that i can just send in one direction. I need a simple code example to till me how to make the sending and receiving in different threads. I read a lot of articles in that issue but i cann't understand. Could you please help me.

    Thank you very much.

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: multithreaded application using MFC

    According to your problem you need 2 threads for your socket communication.
    One thead to process your incomming data and second to send .
    Create a class derived from CWinThread and make 2 functions in it 1: Send Data 2: ReceiveData.

    When u create a connected socket or accepted socket then create 2 objects of this thread class and set their pointer in socket class .

    When u Receive any data then post a message to one of your thread class to receive and process data (ReceiveData function).

    ReceiverThread->PostThreadMessage(RECV_MSG,(WPARAM)this,0);

    and when u want to send data then post messag to other thread class object to send data ( SendData function).

    SenderThread->PostThreadMessage(SEND_MSG,(WPARAM)this,(LPARAM)Msg);

    hope this will help u
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  3. #3
    Join Date
    May 2004
    Posts
    10

    Re: multithreaded application using MFC

    Hello,

    Thank you very much for your help. that was realy great.
    I create a class derived from CWinThread called it CMyData then i make two member functions of that class (SendData() and ReceiveData()) the two functions are void and public. Then i create two member variables of the CMyData class called them m_pRecord, and m_pPlay . I already have a class derived from CAsyncSocket which is CMySocket. Is that OK!!!
    I just need to know how can i "create 2 objects of this thread class and set their pointer in socket class".

    best regards,

    asharf
    Last edited by ashraf_diaa; September 21st, 2004 at 02:37 PM.

  4. #4
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: multithreaded application using MFC

    Make 2 variables of your CMyData class in your CMySocket lets suppose.

    CMyData m_pSDataThread; //pointer of send data thread
    CMyData m_pRDataThread; //pointer of receive data thread

    when u create socket then create both threads

    m_pSDataThread=(CMyData *)AfxBeginThread(RUNTIME_CLASS(CMyData));
    m_pRDataThread=(CMyData *)AfxBeginThread(RUNTIME_CLASS(CMyData));

    now your have 2 threads one for receiving data and one for sending data.
    at this point you should have your socket class pointer in both of threads for accessing socket .

    make a variable in your CMyData class
    CASyncSocket *m_pMySocket;
    and after creating your threads assign your socket class pointer in m_pMySocket pointer.

    // assigning socket class pointer in thread class
    m_pSDataThread->m_pMySocket=this;
    m_pRDataThread->m_pMySocket=this;

    hope this will help u
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  5. #5
    Join Date
    May 2004
    Posts
    10

    Re: multithreaded application using MFC

    Hello sir,

    Thank you very much for your help. I am now have three classes which are CSockDlg, CMySocket, CMyData. I am sorry but i realy confused. I don't know how to connect these three sockets correctly. I always have problems with pointers.
    Could you please advice me what is the easiest way to make these three classes working correctly. when they are just two i can make them connected correctly but now when they are three the situation get more complex. (if you don't mind and have time to help me i can send you the source code of my program, it is already working but just sending from on side to another)

    Thank you very much.

    Best Regards,

    Ashraf
    Last edited by ashraf_diaa; September 23rd, 2004 at 07:49 AM.

  6. #6
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: multithreaded application using MFC

    post me ur code
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  7. #7
    Join Date
    May 2004
    Posts
    10

    Re: multithreaded application using MFC

    Hello sir,

    Thank you for your help. I post the source code for you in your E-Mail (abdulnauman@yahoo.com ) because it is too big to send here. Please help me as soon as you can.

    best regards,

    ashraf

+ Reply to Thread

Bookmarks

Posting Permissions

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



HTML5 Development Center

Click Here to Expand Forum to Full Width