请教大家 一个问题栈遭到破坏的问题
本帖最后由 explorer114 于 2013-09-14 13:54:04 编辑 MFC界面程序,针对3G设备下发配置的命令,在这个线程里执行配置并接受设备上报,但是在执行后出现错误,代码如下:
//下发3G配置的命令
UINT config_netset_thread(LPVOID lParam)
{
char header[32] ;
CString m_transmac ;
unsigned char pMac[100] ;
m_transmac = CPublic::markmac ;//在SetParameters中初始化
CNetSet *pNetset = (CNetSet *)lParam ;
UINT uni = PROTOCOLHEAD_VER;
memcpy(header,&uni,4);
sscanf(m_transmac.GetBuffer(),"%2x%2x%2x%2x%2x%2x",pMac,pMac+1,pMac+2,pMac+3,pMac+4,pMac+5);
memcpy(header+4,pMac,6);//目的mac地址
memcpy(header+10,CPublic::g_ucLocalMac,6);//原mac地址
memcpy(header+16,"temp",4);//会话标示
uni = 0x80000000 ;
memcpy(header+20, &uni ,4);//数据包编号
CAsyncSocket CbroadcastSocket;
if(CbroadcastSocket.Create(0,SOCK_DGRAM) == SOCKET_ERROR)//创建套接字
{
TRACE("fail to create socket\n");
return -1;
}
BOOL b = TRUE;
if(!CbroadcastSocket.SetSockOpt(SO_BROADCAST,&b,sizeof(b),SOL_SOCKET))
{
TRACE("fail to SetSockOpt\n");
CbroadcastSocket.Close();
return -1;
}
int nOpt = 1000;
if(!CbroadcastSocket.SetSockOpt(SO_RCVTIMEO,(char *)&nOpt,sizeof(int),SOL_SOCKET))
{
TRACE("fail to SetSockOpt\n");
CbroadcastSocket.Close();
return -1;
}
b = true;
if(!CbroadcastSocket.SetSockOpt(SO_REUSEADDR,(char *)&b,sizeof(BOOL),SOL_SOCKET))
{
TRACE("fail to SetSockOpt\n");
CbroadcastSocket.Close();
return -1;
}
uni = CMD_UDP_REQUEST_CONFIG_RADIONETWORK_ALL /*703*/ ;
memcpy(header+24,&uni,4);
//填写相关的数据
BVCU::PUConfig::RadioNetwork * pRadioNetwork = &(pNetset->radionetwork);
pRadioNetwork->Clear();
//-----------------------------------------卡1---------------------------------------------
pRadioNetwork->set_benable(pNetset->m_netset[CardOne].cardenable);
//高级中的设置
if (0 == pNetset->m_netset[CardOne].seniordlg.m_defaultapn)//默认
{
pNetset->m_netset[CardOne].seniordlg.m_apn = "" ;
}
pRadioNetwork->set_szapn(pNetset->m_netset[CardOne].seniordlg.m_apn) ;//设置APN
if(0 == pNetset->m_netset[CardOne].seniordlg.m_defaultvpdn)
{
pNetset->m_netset[CardOne].seniordlg.m_username = "" ;
}
pRadioNetwork->set_szusername(pNetset->m_netset[CardOne].seniordlg.m_username) ;//设置vpdn
pRadioNetwork->set_szpassword(pNetset->m_netset[CardOne].seniordlg.m_password) ;//设置密码
pRadioNetwork->set_szaccessnum(pNetset->m_netset[CardOne].seniordlg.m_accessnum) ;//设置接入号
pRadioNetwork->set_szcardnum(pNetset->m_netset[CardOne].seniordlg.m_cardnum) ;//设置卡号
std::string buf_str_cardone;
pNetset->radionetwork.SerializeToString(&buf_str_cardone);
int uni_cardone = buf_str_cardone.size();//卡1负载长度
//-----------------------------------------卡2---------------------------------------------
pRadioNetwork->Clear();
pRadioNetwork->set_benable(pNetset->m_netset[CardTwo].cardenable);
if (0 == pNetset->m_netset[CardTwo].seniordlg.m_defaultapn)//默认
{
pNetset->m_netset[CardTwo].seniordlg.m_apn = "" ;
}
pRadioNetwork->set_szapn(pNetset->m_netset[CardTwo].seniordlg.m_apn) ;//设置APN
if(0 == pNetset->m_netset[CardTwo].seniordlg.m_defaultvpdn)
{
pNetset->m_netset[CardTwo].seniordlg.m_username = "" ;
}
pRadioNetwork->set_szusername(pNetset->m_netset[CardTwo].seniordlg.m_username) ;//设置vpdn
pRadioNetwork->set_szpassword(pNetset->m_netset[CardTwo].seniordlg.m_password) ;
pRadioNetwork->set_szaccessnum(pNetset->m_netset[CardTwo].seniordlg.m_accessnum) ;//设置接入号
pRadioNetwork->set_szcardnum(pNetset->m_netset[CardTwo].seniordlg.m_cardnum) ;//设置卡号
std::string buf_str_cardtwo;
pNetset->radionetwork.SerializeToString(&buf_str_cardtwo);
int uni_cardtwo = buf_str_cardtwo.size();//卡2负载长度
uni = uni_cardone + uni_cardtwo + 8;// len后面的总负载长度
memcpy(header+28, &uni,4);//数据包长度
memcpy(header+32, &uni_cardone,4);
memcpy(header+36, &buf_str_cardone, uni_cardone);
memcpy(header+36+uni_cardone, &uni_cardtwo, 4);
memcpy(header+36+uni_cardone+4, &buf_str_cardtwo, uni_cardtwo);
int done = CbroadcastSocket.SendTo((string(header,32) + string(header+32,4) + buf_str_cardone +
string(header+36+uni_cardone,4) + buf_str_cardtwo).c_str(),
uni + 32, PORT_UDP_PU,NULL,0); //wangfengyong on 20130907
TRACE("done = %d to config pu net through UDP\n", done);
if(done < 0)
{
AfxMessageBox("fail to send broadcast to config pu net\n");
CbroadcastSocket.Close();
return -1;
}
//检查配置的结果
char buf[1500] ;
DWORD dwTick1 = ::GetTickCount();
while (WaitForSingleObject(CPublic::m_event,100)!=WAIT_OBJECT_0)
{
if(g_bConfigQuit) return 0xff;//huangning add on 20130308
Sleep(100) ;
DWORD dwNow1 = ::GetTickCount() ;
if (dwNow1 - dwTick1 > 10000)//大于10秒表示接收超时
{
break ;
}
done = CbroadcastSocket.ReceiveFrom(buf,1500,NULL,0);//将数据接收在buf中
if (done < 0)
continue ;
int iCmdID = *(int*)(buf + 24) ;
int m_state = *(int*)(buf + 32) ;
if (iCmdID == CMD_UDP_ANSWER_CONFIG_RADIONETWORK_ALL)
{
if (m_state == CONFIG_STAT_SUCCESS)
{
pNetset->m_staticnetset= "设置成功!" ;
}
else
{
pNetset->m_staticnetset = "设置失败!" ;
}
break ;
}
}
::PostMessage(pNetset->GetSafeHwnd(), WM_NET_CONFIG_STATE, 0, 0) ;
return 0 ;
}