正规dll使用共享MFC类库中的CSocket.Create莫名失败1.我没有调用AfxSocketInit(),导出的函数f()中,创建局部类CSocket cli,然后cli.Create(),应用第一次调用函数f()成功,从f()退出时,cli.Close(),第二次调用函数f()时(应用程序循环调用f()),运行到cli.Create(),立即崩溃。2.后来我尝试使用全局的CSocket cli(这样就只需要创建一次了),没想到,应用程序退出时,在析构函数中的cli.Close()处崩溃了。高手都说是AfxSocketInit()没有调用的原因,可是我不明白,
为啥1中的第一次调用时成功而在第二次崩溃了?局部的CSocket建立然后退出时销毁,我看没啥问题啊???
2中的cli.Close()崩溃的原因是啥呢?

解决方案 »

  1.   

    更正一下:
    VC6环境,自动生成了一个类,名字叫做CtestApp,然后定义了全局类theApp,CtestApp的instance成员函数调用了AfxSocketInit(),以上均是环境自动生成的代码。也就是说,我的程序中,已经调用了AfxSocketInit(),但是仍旧出现崩溃现象
      

  2.   

    函数f()很简单,就是为了测试的void f() {
        int len = 4;
        int request = 1, reply = 0;
        //网络相关
         int port = 16902;
        int sock_type = SOCK_STREAM;
        const char * IP = "127.0.0.1";
        CSocket client;    if (!client.Create()) {
            return -1;
        }    //连接到服务器
        if (!client.Connect(IP, port)) {
            return -2;
        }    //发送数据
        request = ntohl(request);
        if (client.Send(&request, len) !=len) {
            return -3;
        }    //接收数据
        if (client.Receive(&reply, len) != len){
            return EREAD;
        }    reply = htonl(reply);    //关闭连接
        client.Close();
    }
      

  3.   

    不好意思,connect函数的IP和port写反了,EREAD=-4,为啥没人回答呢
      

  4.   

    从一个csdn老兄的博客里看到的,仔细看英文提示就明白了在com中用CSocket类Create出现错误
    //debug mfc静态链接
    在Com的一个方法中用CSocket类
    CSocket  proxySK;
    proxySK.create();  //这一句会出错解决方法:
    在函数的开头加上以下代码来在CSocket所在的Thread中初始化CSocket和SOCKET做映射的链表:
    #define _afxSockThreadState AfxGetModuleThreadState() 
    #define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE 
     
     _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState; 
          if (pState->m_pmapSocketHandle == NULL) 
             pState->m_pmapSocketHandle = new CMapPtrToPtr; 
          if (pState->m_pmapDeadSockets == NULL) 
             pState->m_pmapDeadSockets = new CMapPtrToPtr; 
          if (pState->m_plistSocketNotifications == NULL) 
             pState->m_plistSocketNotifications = new CPtrList; 据说是一个Bug原文是这样说的
    Hi, When using MFC sockets in secondary threads in a statically linked MFC 
    Visual C++ 6.0 application, an unhandled exception occurs. The reason for 
    the unhandled exception is that an object of type CMapPtrToPtr pointer, 
    pointed to by m_pmapSocketHandle, is never created. 
    To resolve it the handle maps used by the sockets need to be created for 
    each thread.The following code shows a function to do this: 
    void SocketThreadInit() 

    #ifndef _AFXDLL 
    #define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE 
    #define _afxSockThreadState AfxGetModuleThreadState() _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState; 
    if (pState->m_pmapSocketHandle == NULL) 
    pState->m_pmapSocketHandle = new CMapPtrToPtr; 
    if (pState->m_pmapDeadSockets == NULL) 
    pState->m_pmapDeadSockets = new CMapPtrToPtr; 
    if (pState->m_plistSocketNotifications == NULL) 
    pState->m_plistSocketNotifications = new CPtrList; #endif 

    This function should be called once in each secondary thread before the 
    first socket is created in the new thread. IMP : This bug was corrected in Visual Studio 6.0 Service Pack 3 Please refer to Q193101 from MSDN
      

  5.   

    http://blog.csdn.net/karl_max/archive/2007/08/30/1764910.aspx
    就是这个博客
      

  6.   

    我用了楼上的那些代码,但是会报错:
        error C2679:binary '=':no operator found which takes a right-hand operand of type
    'CMapPtrToPtr *'(or there is no acceptable conversion)    三个 = 号 赋值的语句都会报这个错误。
      

  7.   

    还是没用啊...我把 英文的那部分 代码 拷过去,不会报错,但是生成的lib和dll  仍然 无法正常使用....救命啊~~~我都 做了两个月了,,,难道 要因为 这种问题 功亏一篑么,,,,