CSocket::Attach

调用该成员函数的附加 hSocket 句柄 CSocket 对象。

BOOL Attach(
   SOCKET hSocket 
);

参数

  • hSocket
    包含一个句柄套接字。

返回值

非零,如果函数运行成功。

备注

套接字 处理在对象的 m_hSocket 数据成员中存储。

有关更多信息,请参见 Windows套接字:使用套接字与存档

示例

class CSockThread : public CWinThread
{
public:
   SOCKET m_hConnected;

protected:
   CChatSocket m_sConnected;

   // remainder of class declaration omitted.
BOOL CSockThread::InitInstance()
{
   // Attach the socket object to the socket handle
   // in the context of this thread.
   m_sConnected.Attach(m_hConnected);
   m_hConnected = NULL;

   return TRUE;
}
// This listening socket has been constructed
// in the primary thread.
void CListeningSocket::OnAccept(int nErrorCode)
{
   UNREFERENCED_PARAMETER(nErrorCode);

   // This CSocket object is used just temporarily
   // to accept the incoming connection.
   CSocket sConnected;
   Accept(sConnected);

   // Start the other thread.
   CSockThread* pSockThread = (CSockThread*)AfxBeginThread(
      RUNTIME_CLASS(CSockThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
   if (NULL != pSockThread)
   {
      // Detach the newly accepted socket and save
      // the SOCKET handle in our new thread object.
      // After detaching it, it should no longer be
      // used in the context of this thread.
      pSockThread->m_hConnected = sConnected.Detach();
      pSockThread->ResumeThread();
   }
}

要求

Header: afxsock.h

请参见

参考

CSocket选件类

层次结构图

CAsyncSocket::Attach