How do I reference a CAsyncSocket class (or how do I copy this class);

I have a class that brings up a window and realises a connection with another computer using CAsyncSocket... After connection I want to destroy that window(implicitly the class) and to copy the connected socket to another class so I can work with it.
For example I use a dialog for simplicity in this example:

---implemantation of some class---
--some function--
CMyDialog dlg;
if(dlg.DoModal()==IDOK)
{
    //how do I do this here:
    //socket=dlg.socketdlg;
    //where socket: is declared as CMyAsyncSocket in this class
    //and socketdlg: is declared as CMyAsyncSocket in CMyDialog
}

---in other function of this class---
socket.Send....
lbalan79Asked:
Who is Participating?
 
williamcampbellConnect With a Mentor Commented:

 class CDlg1
 {
    CAsyncSocket m_sock1;

    CopySocket ( SOCKET& sock )
    {
       m_sock1.Attach ( sock );
    }
 };

 class CDlg2
 {
    CAsyncSocket m_sock2;

    SOCKET GetSocket () { m_sock2.Detach (); }
 };

 
 CDlg1 dlg1;
 CDlg2 dlg2;

 dlg1.CopySocket ( dlg2.GetSocket () );

 Try That
 
0
 
AlexFMCommented:
I think it's better to declare CMyAsyncSocket somewhere outside of dialog class. Keep pointer to this socket in the dialog:

class CMyClass
{
    CMyAsyncSocket m_socket;
};

void CMyClass::ShowConnectionDlg()
{
    CMyDialog dlg;
    dlg.m_pSocket = &m_socket;
    if(dlg.DoModal()==IDOK)
    {
        // continue to work with m_socket
    }
}

class CMyDialog : public CDialog
{
public:
    CMyAsyncSocket* m_pSocket;  // filled by caller before DoModal
};
0
All Courses

From novice to tech pro — start learning today.