在服务器端accept(*pSocket)以后,detach句柄,然后放到clist里,然后在一个发送线程里创建一个 Csocket ,从clist里取出一个socket句柄,然后attach,接着用创建的csocket发送数据,但是为什么不能触发客户端的onreceive()函数呢?

解决方案 »

  1.   

    CSocket不支持多线程,多线程用CSocket会出现意想不到的结果,比如:ASSERT失败、访问非法页面错误、不响应事件等问题。多线程就不要用CSocket了。可以试一试单线程CAsyncSocket或者socket api。
      

  2.   

    可以试一试单线程CAsyncSocket或者socket api,socket api单线程多线程都可以。
      

  3.   

    detach句柄
    为什么要这样做?onreceive()函数
    有数据来的才发生。
      

  4.   

    因为在线程间不能传递csocket指针,这是微软的kb里面提到的,所以只能传递socket结构,我想要使用cfile和carchive和多线程,怎么办?
      

  5.   

    前面说过了,csocket不能多thread,放弃吧,用其他的。
      

  6.   

    csocket在多线程时出的错,莫名其妙了。不过如果CSOCKET变量不在线程间传送的话
    可以的,
    多线程建议用WINSOCKET来处理,简单明了
      

  7.   

    CSocket内部用CSocketWnd中handle的,这个handle不能跨线程。只能放弃CSocket+多线程。
      

  8.   

    参考PRB: Assertion Failed on Line 837 - Sockcore.cpp Q140527
    CAUSE
    Most frequently, this problem is due to the sharing of CSocket objects between multiple threads.A CSocket object should be used only in the context of a single thread because the SOCKET handle encapsulated by a CAsyncSocket object is stored in a per-thread handle map. (CSocket is derived from CAsyncSocket.) Other information is stored on a per-thread basis, including a hidden notification window that MFC uses for socket notifications.The assertion failure line, which can be found in Sockcore.cpp in the \Msvc20\Mfc\Src directory, is: 
       ASSERT(pThreadState->m_hSocketWindow != NULL); 
    This assertion failure occurs because the CSocket object was either created or accepted in the context of another thread. The socket notification window was created in a different thread, and the m_hSocketWindow for the current thread is NULL, thus the assertion failure. RESOLUTION
    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 SOCKET 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.
    ...
      

  9.   

    你做的一切都没必要,
    既然你把连接的Socket已经加到LIST当中,detach有必要吗?