函数WSARecvFrom接收数据的问题
WSARecvFrom这个函数其中一个参数是lpBuffers:一个指向WSABUF结构数组的指针。
我在接收时只能接收到第一个WSABUF结构体发送过来的内容。
第二个WSABUF结构体的内容没有接收到。
其实也不能说没有接收到,应该是接收到了,而没有保存到第二个WSABUF结构体中。
发送接收都没有提示错误,而且发送字节和接收字节都一样。
本人小菜,研究了好久都不明白,下面是代码,大家帮忙看看哪里有问题。
谢谢大家
发送代码:
WSABUF buf[2];
DWORD dwSend;
SOCKADDR_IN addrSend;
addrSend.sin_addr.S_un.S_addr=inet_addr("192.168.1.188");
addrSend.sin_family=AF_INET;
addrSend.sin_port=htons(4567);
CString strBuf1;
CString strBuf2;
int len;
GetDlgItemText(IDC_EDIT_SEND1,strBuf1);
len=strBuf1.GetLength();
buf[0].buf=strBuf1.GetBuffer(len);
buf[0].len=len+1;
GetDlgItemText(IDC_EDIT_SEND2,strBuf2);
len=strBuf2.GetLength();
buf[1].buf=strBuf2.GetBuffer(len);
buf[1].len=len+1;
SetDlgItemText(IDC_EDIT_SEND1,"");
SetDlgItemText(IDC_EDIT_SEND2,"");
if(SOCKET_ERROR==WSASendTo(m_socket,buf,2,&dwSend,0,(SOCKADDR*)&addrSend,sizeof(SOCKADDR),NULL,NULL))
{
MessageBox("发送数据失败!");
return;
}
CString str;
str.Format("发送了%d个字节",dwSend);
MessageBox(str);
接收代码:
WSABUF buf[2];
buf[0].buf=new char[1024];
buf[0].len=1024;
buf[1].buf=new char[1024];
buf[1].len=1024;
DWORD dwRecv;
DWORD dwFlg=0;
int len=sizeof(SOCKADDR);
SOCKADDR_IN addrFrom;
if(SOCKET_ERROR==WSARecvFrom(m_socket,buf,2,&dwRecv,&dwFlg,(SOCKADDR*)&addrFrom,&len,NULL,NULL))
{
MessageBox("接收数据失败!");
return;
}
SetDlgItemText(IDC_EDIT_RECV1,buf[0].buf);
SetDlgItemText(IDC_EDIT_RECV2,buf[1].buf);
CString str;
str.Format("接收了%d个字节",dwRecv);
MessageBox(str);