珞珈山水BBS电脑网络程序人生 → 单文区文章阅读

单文区文章阅读 [返回]
发信人: oyljerry (勇敢的心→㊣开始习惯冬天~㊣), 信区: Programm
标  题: Re: 求助求助,MFC
发信站: BBS 珞珈山水站 (Mon Dec 12 15:08:40 2005)

As already mentioned, a CAsyncSocket object should be used only in the context
 of a single thread. If you need to switch the thread that is accessing a SOCK
ET connection with another thread, then you should use a separate CAsyncSocket
 object in each thread, and use the Detach and Attach functions to attach the 
CAsyncSocket object to the SOCKET handle in the thread that is about to use the socket. Use this sequence: 


Use Detach() to detach the CAsyncSocket object from the SOCKET handle in the thread that is currently using the CAsyncSocket object. 

Use Attach() to attach a different CAsyncSocket object to the SOCKET handle while in the context of the MFC UI thread in which you wish to begin accessing the SOCKET connection. 

The code shown in the "Code Sample" section of this article shows how to handle the moment when a listening socket accepts a connection request and then begins a new thread to handle the new connection. 
NOTE: One concern often arises that socket notification messages might be lost
 between the time the call to Detach() is made and the subsequent call to Attach() is made. This is not a concern because of the way socket notifications are handled. The implementation of CAsyncSocket::Attach() makes a call to WSAAsyncSelect to enable notifications. As mentioned in the documentation for WSAAsyncSelect, if any socket noficiations were already pending for the SOCKET, they will be re-posted. 



MORE INFORMATION
To Accept a socket in the context of one thread and then begin using it in the
 context of another thread, you need to be sure to detach the CAsyncSocket object in the first thread and attach a different CAsyncSocket object in the second thread. The following code snippet shows how to do it. 


Code Sample

// ...

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));




REFERENCES
MFC Technical Note #2 - Although this technical note does not directly address the CAsyncSocket class, it does discuss the mapping of handles to objects. The relationship between a SOCKET handle and a CAsyncSocket object is maintained in much the same way. 


Keywords          : MfcSockets kbcode kbprb kbprg
Technology        : kbMfc
Version           : 2.10 2.20 4.00
Platform          : NT WINDOWS
Issue type        : kbprb
Solution Type     : kbcode

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" W
ITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRES
S OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PA
RTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE 
LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONS
EQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORP
ORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQU
ENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

--
     百事可乐,万事七喜,心情雪碧,学习芬达,工作红牛,生活茹梦
     
爱情鲜橙多,天天娃哈哈,日日乐百氏,年年高乐高,永远都醒目
         
←≮ ┌──────────┐≯→                                
         
┓⿻ 工作,我一直努力    │⿻┛ 
         
┓ │    对你,我从未放弃┛⿻                                
         
☆〾 └──────────┘〾☆                                                     


※ 修改:·oyljerry 於 Dec 12 15:10:15 2005 修改本文·[FROM: 202.114.71.*]
※ 来源:·珞珈山水BBS站 http://bbs.whu.edu.cn·[FROM: 202.114.71.*]
[返回单文区目录]

武汉大学BBS 珞珈山水站 All rights reserved.
wForum , 页面执行时间:7.307毫秒