MFC UDP服务器接收数据乱码问题。
各位大神,急求大家帮助啊,我在做一个UDP服务器,用来接收我的一个设备发送的数据,设备发送的是十六进制数据0xaa;我想在把数据显示在服务器端,显示为aa。下面上我的服务器端接收数据的函数,可是我接收到的是乱码,请各位大神帮我改改代码吧。UINT ServerRecvThread(LPVOID lpParm )
{
CUDP_ServerDlg *dlg = (CUDP_ServerDlg*)lpParm;
char gcInBuffer[1027];
int lenth;
CByteArray hexdata;
int size = sizeof(sockaddr_in);
CString strReceive, tempStr;
char*gcClientIP;
bool bSendEnable = false;
while(!dlg->m_bTerminateThread)
{
if ((lenth = recvfrom(dlg->m_sockServer, gcInBuffer, 1024, 0, (struct sockaddr *)&dlg->m_clientAddr, &size) )>0)
{
if (!bSendEnable)
{
CWnd *cwnd = dlg->GetDlgItem(IDC_BUTTON_SEND);
cwnd->EnableWindow(TRUE);
bSendEnable = true;
}
gcClientIP = inet_ntoa((dlg->m_clientAddr).sin_addr);
tempStr.Format("%s", gcClientIP);
dlg->SetDlgItemText(IDC_STATIC_CLINET_ADDRESS, tempStr);
tempStr.Format("%hu", dlg->m_clientAddr.sin_port);
dlg->SetDlgItemText(IDC_STATIC_CLIENT_PORTNO, tempStr);
tempStr.Format("%s:%hu: ", gcClientIP, dlg->m_clientAddr.sin_port);
strReceive += tempStr;
gcInBuffer[lenth] = '\r';
gcInBuffer[lenth+1] = '\n';
gcInBuffer[lenth+2] = '\0';
strReceive += gcInBuffer;
dlg->m_editReceive.SetWindowText(strReceive);
}
}
return 0;
}