首先介紹一下,,開(kāi)發(fā)平臺(tái):WINCE 4.2 (WINCE 5.0也適用,因?yàn)槭褂昧遂o態(tài)編譯MFC動(dòng)態(tài)鏈接庫(kù));開(kāi)發(fā)工具:VS2005,。
接下來(lái)開(kāi)始修改代碼:
view plaincopy to clipboardprint,?
1.修改UDP_CE.CPP的SendData函數(shù)為:
bool CUDP_CE::SendData(const char *buf, int len)
{
int nBytes = 0;
int nErrorCode;
nBytes = sendto(m_UDPSocket,,buf,,len,0,,(sockaddr*)&m_RemoteAddr,,sizeof(m_RemoteAddr));
if (nBytes==SOCKET_ERROR )
{
nErrorCode = WSAGetLastError();
m_OnUdpError(m_pOwnerWnd,nErrorCode);
return false;
}
return true;
}
當(dāng)然,,不要忘記修改UDP_CE.h的SendData函數(shù)聲明為:bool SendData(const char *buf,, int len);
2.修改UDP_CE.CPP的RecvThread函數(shù)為:
。..,。..,。..。..,。..
if (ret 》 0)
{
if (FD_ISSET(pSocket-》m_UDPSocket,,&fdRead))
{
tmpAddr.sin_family=AF_INET;
tmpAddr.sin_port = htons(pSocket-》m_RemoteAddr.sin_port);
tmpAddr.sin_addr.s_addr =INADDR_ANY;
tmpRecvLen = sizeof(tmpAddr);
recvBuf = new char[1024];
recvedBuf = new char[1024];
ZeroMemory(recvBuf,1024);
ZeroMemory(recvedBuf,,1024);
recvLen = recvfrom(pSocket-》m_UDPSocket,,recvBuf, 1024,,0,,(SOCKADDR*)&tmpAddr,&tmpRecvLen);
if (recvLen == SOCKET_ERROR)
{
iErrorCode = WSAGetLastError();
pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,,iErrorCode);
break;
}
else if (recvLen == 0)
{
iErrorCode = WSAGetLastError();
pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,,iErrorCode);
break;
}
else
{
//此處添加解析程序,將接收到的數(shù)據(jù)解析后,,
pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,,recvBuf,recvedBufLen,,(SOCKADDR*)&tmpAddr);
delete []recvBuf;
recvBuf = NULL;
delete []recvedBuf;
recvedBuf = NULL;
}
}
3.修改UDPDlg.cpp.CPP的OnUdpCERecv函數(shù)為:
CUDPDlg * pDlg;
pDlg = (CUDPDlg*)pWnd;
CEdit *pRecvStrEdit = (CEdit*)pDlg-》GetDlgItem(IDC_EDIT_RECVSTR);
ASSERT(pRecvStrEdit ,!= NULL);
CString strRecv (buf);
pRecvStrEdit-》SetWindowText(strRecv);
4.刪除UDP_CE.H,不是必須,,但可以節(jié)省多余的代碼
typedef struct CUDPData
{
char FrameHead[4]; //幀頭
DWORD DataPackageLen; //幀長(zhǎng)度
char * Buf; //緩沖區(qū)數(shù)據(jù)
char FrameTail[4]; //幀尾
}UDPData;
1.修改UDP_CE.CPP的SendData函數(shù)為:
bool CUDP_CE::SendData(const char *buf,, int len)
{
int nBytes = 0;
int nErrorCode;
nBytes = sendto(m_UDPSocket,buf,,len,,0,,(sockaddr*)&m_RemoteAddr,sizeof(m_RemoteAddr));
if (nBytes==SOCKET_ERROR )
{
nErrorCode = WSAGetLastError();
m_OnUdpError(m_pOwnerWnd,,nErrorCode);
return false;
}
return true;
}
當(dāng)然,,不要忘記修改UDP_CE.h的SendData函數(shù)聲明為:bool SendData(const char *buf, int len);
2.修改UDP_CE.CPP的RecvThread函數(shù)為:
,。..,。..。..,。..,。..
if (ret 》 0)
{
if (FD_ISSET(pSocket-》m_UDPSocket,&fdRead))
{
tmpAddr.sin_family=AF_INET;
tmpAddr.sin_port = htons(pSocket-》m_RemoteAddr.sin_port);
tmpAddr.sin_addr.s_addr =INADDR_ANY;
tmpRecvLen = sizeof(tmpAddr);
recvBuf = new char[1024];
recvedBuf = new char[1024];
ZeroMemory(recvBuf,,1024);
ZeroMemory(recvedBuf,,1024);
recvLen = recvfrom(pSocket-》m_UDPSocket,recvBuf,, 1024,,0,,(SOCKADDR*)&tmpAddr,,&tmpRecvLen);
if (recvLen == SOCKET_ERROR)
{
iErrorCode = WSAGetLastError();
pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,iErrorCode);
break;
}
else if (recvLen == 0)
{
iErrorCode = WSAGetLastError();
pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,,iErrorCode);
break;
}
else
{
//此處添加解析程序,,將接收到的數(shù)據(jù)解析后,
pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,,recvBuf,,recvedBufLen,(SOCKADDR*)&tmpAddr);
delete []recvBuf;
recvBuf = NULL;
delete []recvedBuf;
recvedBuf = NULL;
}
}
3.修改UDPDlg.cpp.CPP的OnUdpCERecv函數(shù)為:
CUDPDlg * pDlg;
pDlg = (CUDPDlg*)pWnd;
CEdit *pRecvStrEdit = (CEdit*)pDlg-》GetDlgItem(IDC_EDIT_RECVSTR);
ASSERT(pRecvStrEdit ,!= NULL);
CString strRecv (buf);
pRecvStrEdit-》SetWindowText(strRecv);
4.刪除UDP_CE.H,,不是必須,但可以節(jié)省多余的代碼
typedef struct CUDPData
{
char FrameHead[4]; //幀頭
DWORD DataPackageLen; //幀長(zhǎng)度
char * Buf; //緩沖區(qū)數(shù)據(jù)
char FrameTail[4]; //幀尾
}UDPData;還可以刪除窗體上發(fā)送,、接收數(shù)字的編輯框等,,因?yàn)閷?shí)際應(yīng)用中用不上。