次の方法で共有


CSocket::Attach

更新 : 2007 年 11 月

hSocket ハンドルを CSocket オブジェクトに関連付けます。

BOOL Attach(
   SOCKET hSocket 
);

パラメータ

  • hSocket
    ソケットのハンドル。

戻り値

関数が正常終了したときは 0 以外を返します。

解説

SOCKET ハンドルは、オブジェクトの 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();
   }
}

必要条件

ヘッダー : afxsock.h

参照

参照

CSocket クラス

階層図

CAsyncSocket::Attach

その他の技術情報

CSocket のメンバ