我在别人的工程项目上添加了网络接收和网络发送后,调试通过,但是运行报错“Debug Assertion Failed,......,wincore.cpp, Line 980”。在后来的调试过程中发现只有网络接收或者只有网络发送时,程序就可以正常运行。后来一行一行的调试程序,发现创建一个Socket后,再创建另一个Socket就发生异常,创建不起来。不知道怎么回事,请各位高手指点!我是在别人的项目上添加代码的,功能扩展的需要!

解决方案 »

  1.   

    UINT _ListenTcpThread(LPVOID lparam)
    {
    CBackupServerDlg *pDlg=(CBackupServerDlg *)lparam;
    if(pDlg->StopServer==true) return -1;

    CSocket sockSrvr;
    pDlg->m_Potr=PORT+pDlg->m_server;
    int createSucceed=sockSrvr.Create(pDlg->m_Potr)
    if(createSucceed==0)
    {
    AfxMessageBox("_ListenTcpThread Create错误!"+pDlg->GetError(GetLastError()));
    return -1;
    }

    int listenSucceed=sockSrvr.Listen();
    if(listenSucceed==0)
    {
    AfxMessageBox("_ListenTcpThread Listen错误!"+pDlg->GetError(GetLastError()));
    return -1;
    }

    CSocket recSo;
    SOCKADDR_IN client;
    int iAddrSize=sizeof(client);

    int acceptSucceed=sockSrvr.Accept(recSo,(SOCKADDR *)&client,&iAddrSize);
    if(acceptSucceed==0)
    {
    AfxMessageBox("_ListenTcpThread Accept错误!"+pDlg->GetError(GetLastError()));
    return -1;
    }
    sockSrvr.Close();
    char flag[FLAG]={0};
    if(recSo.Receive(flag,FLAG)!=2) 
    {
    return -1;
    }
    pDlg->m_type=flag[0];
    if(pDlg->m_type=='D') return 0;

    pThreadLisen=::AfxBeginThread(_ListenTcpThread,pDlg);
    pDlg->ReceiveFileMsg(recSo,client);
    return 0;

    }
      

  2.   

    UINT _SendMsgThread(LPVOID lparam)
    {

    CBackupServerDlg *pDlg=(CBackupServerDlg *)lparam;
    if(pDlg->StopServer==true) return -1;

    CSocket sockClient;
    sockClient.Create();
    CString ip,strError;
    pDlg->m_You_IP.GetWindowText(ip);
    int conn=sockClient.Connect(ip, PORT+pDlg->m_client);
    if(conn==0)
    {
    AfxMessageBox("_SendMsgThread Connect错误!"+pDlg->GetError(GetLastError()));
    sockClient.ShutDown(2);
    sockClient.Close();
    AfxEndThread(1L);
    return 0;

    }
    int end=0;
    end=sockClient.Send("M",FLAG); 
    if(end==SOCKET_ERROR)
    {
    AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
    return -1;
    }
    else if(end!=2)
    {
    AfxMessageBox("消息头错误");
    return -1;
    }
    CString strMsg=pDlg->m_MsgSend;
    end=sockClient.Send(strMsg,strMsg.GetLength()); 
    if(end==SOCKET_ERROR)
    {
    AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
    return -1;
    }
    CString strLocalName;
    pDlg->GetLocalHostName(strLocalName);
    CString strLocalIP;
    pDlg->GetIpAddress(strLocalName,strLocalIP);
    pDlg->AddMsgList(strLocalIP+"->"+strLocalName,strMsg);

    int i=0;
    sockClient.Close(); return 0;
    }
      

  3.   

    不可能的问题,肯定是代码组织问题
    一个socket,你想怎么发送都行