首先介绍一下,开发平台:WINCE4.2 (WINCE 5.0也适用,因为使用了静态编译MFC动态链接库);开发工具:VS2005。
接下来开始修改代码:
view plaincopy to clipboardprint?
1.修改UDP_CE.CPP的SendData函数为:
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;
}
当然,不要忘记修改UDP_CE.h的SendData函数声明为:bool SendData(const char *buf, int len);
2.修改UDP_CE.CPP的RecvThread函数为:
。..。..。..。..。..
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
{
//此处添加解析程序,将接收到的数据解析后,
pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,recvBuf,recvedBufLen,(SOCKADDR*)&tmpAddr);
delete []recvBuf;
recvBuf = NULL;
delete []recvedBuf;
recvedBuf = NULL;
}
}
3.修改UDPDlg.cpp.CPP的OnUdpCERecv函数为:
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,不是必须,但可以节省多余的代码
typedef struct CUDPData
{
char FrameHead[4]; //帧头
DWORD DataPackageLen; //帧长度
char * Buf; //缓冲区数据
char FrameTail[4]; //帧尾
}UDPData;
1.修改UDP_CE.CPP的SendData函数为:
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;
}
当然,不要忘记修改UDP_CE.h的SendData函数声明为:bool SendData(const char *buf, int len);
2.修改UDP_CE.CPP的RecvThread函数为:
。..。..。..。..。..
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
{
//此处添加解析程序,将接收到的数据解析后,
pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,recvBuf,recvedBufLen,(SOCKADDR*)&tmpAddr);
delete []recvBuf;
recvBuf = NULL;
delete []recvedBuf;
recvedBuf = NULL;
}
}
3.修改UDPDlg.cpp.CPP的OnUdpCERecv函数为:
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,不是必须,但可以节省多余的代码
typedef struct CUDPData
{
char FrameHead[4]; //帧头
DWORD DataPackageLen; //帧长度
char * Buf; //缓冲区数据
char FrameTail[4]; //帧尾
}UDPData;还可以删除窗体上发送、接收数字的编辑框等,因为实际应用中用不上。