问题描述:
我在建立网络时候创建一线程侦听,侦听后将连接套接子传给接受线程。假设该连接套接子为m_Socket,我另想建立一CSocket,我如何将m_Socket关联到CSocket?除了Attach()还需要怎么做。

解决方案 »

  1.   

    class CSockThread : public CWinThread
    {
    // ... Other function and member declarations
    protected:
      CSocket m_sConnected;
    };SOCKET hConnected;BOOL CSockThread::InitInstance()
    {
       // Attach the socket object to the socket handle
       // in the context of this thread.
       m_sConnected.Attach(hConnected);   return TRUE;
    }// This listening socket has been constructed
    // in the primary thread.
    void CListeningSocket::OnAccept(int nErrorCode)
    {
       // This CSocket object is used just temporarily
       // to accept the incoming connection.
       CSocket sConnected;
       Accept(sConnected);   // Detach the newly accepted socket and save
       // the SOCKET handle.
       hConnected = sConnected.Detach();   // After detaching it, it should no longer be
       // used in the context of this thread.   // Start the other thread.
       AfxBeginThread(RUNTIME_CLASS(CSockThread));
    }