mfc的socket编程总是接收到乱码
switch(LOWORD(lParam)){caseFD_READ:WSABUFwsabuf;wsabuf.buf=newchar[200];wsabuf.len=20...
switch(LOWORD(lParam))
{
case FD_READ:
WSABUF wsabuf;
wsabuf.buf=new char[200];
wsabuf.len=200;
DWORD dwread;
DWORD dwFlag=0;
SOCKADDR_IN addrFrom;
int len=sizeof(SOCKADDR);
CString str,strTemp;
WSARecvFrom(m_socket,&wsabuf,1,&dwread,&dwFlag,(SOCKADDR*)&addrFrom,&len,NULL,NULL);
str.Format("%s,%s",inet_ntoa(addrFrom.sin_addr),wsabuf.buf);
SetDlgItemText(IDC_E_C,str);
break;
}
我只发了一个整数 1 而已 展开
{
case FD_READ:
WSABUF wsabuf;
wsabuf.buf=new char[200];
wsabuf.len=200;
DWORD dwread;
DWORD dwFlag=0;
SOCKADDR_IN addrFrom;
int len=sizeof(SOCKADDR);
CString str,strTemp;
WSARecvFrom(m_socket,&wsabuf,1,&dwread,&dwFlag,(SOCKADDR*)&addrFrom,&len,NULL,NULL);
str.Format("%s,%s",inet_ntoa(addrFrom.sin_addr),wsabuf.buf);
SetDlgItemText(IDC_E_C,str);
break;
}
我只发了一个整数 1 而已 展开
3个回答
展开全部
new char[200] 不清零里面的数据就是屯屯屯;
wsabuf.buf 需要ZeroMemory后,再使用的。
wsabuf.buf 需要ZeroMemory后,再使用的。
更多追问追答
追问
能说下具体怎么做吗?
追答
switch(LOWORD(lParam))
{
case FD_READ:
WSABUF wsabuf;
wsabuf.buf=new char[200];
ZeroMemory(wsabuf.buf,200)//就加一行,试试看
wsabuf.len=200;
DWORD dwread;
DWORD dwFlag=0;
SOCKADDR_IN addrFrom;
int len=sizeof(SOCKADDR);
CString str,strTemp;
WSARecvFrom(m_socket,&wsabuf,1,&dwread,&dwFlag,(SOCKADDR*)&addrFrom,&len,NULL,NULL);
str.Format("%s,%s",inet_ntoa(addrFrom.sin_addr),wsabuf.buf);
SetDlgItemText(IDC_E_C,str);
break;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询