다음을 통해 공유


CSocket::Attach

첨부 하려면이 멤버 함수를 호출의 hSocket 처리 하는 CSocket 개체입니다.

BOOL Attach( 
   SOCKET hSocket  
);

매개 변수

  • hSocket
    소켓에 대 한 핸들을 포함 합니다.

반환 값

함수가 성공 하면 0이 아닌.

설명

소켓 핸들 개체에 저장 된 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