本人写了如下的收发程序:
但每次接收的字数不对,只能收到四个字符?是何缘故?{
CBroadCastDlg* dlg=(CBroadCastDlg*)AfxGetApp()->GetMainWnd();
char buf[4096];
int len=sizeof(dlg->client);
while(1)
{
memset(buf,0,sizeof(buf));
int result=recvfrom(dlg->sock,buf,sizeof(buf)-1,0,(sockaddr*)&(dlg->client),&len);
if(result==SOCKET_ERROR)
{
AfxMessageBox("receive");
return -1;
}
dlg->m_revc.Format("%s",buf);
AfxMessageBox(dlg->m_revc);
}
return 0;
}
{
int length;
char* buf;
CString str;
GetDlgItemText(IDC_EDIT1,str);
buf=str.GetBuffer(0);
sockaddr_in to;
to.sin_family=PF_INET;
to.sin_addr.s_addr=INADDR_BROADCAST; to.sin_port=htons(PortNum);
buf[str.GetLength()]=0;
if((length=sendto(sock,buf,sizeof(buf),0,(LPSOCKADDR)&to,sizeof(SOCKADDR)))==SOCKET_ERROR)
{
AfxMessageBox("发送出错");
closesocket(sock);
return;
}
AfxMessageBox(buf);
}
但每次接收的字数不对,只能收到四个字符?是何缘故?{
CBroadCastDlg* dlg=(CBroadCastDlg*)AfxGetApp()->GetMainWnd();
char buf[4096];
int len=sizeof(dlg->client);
while(1)
{
memset(buf,0,sizeof(buf));
int result=recvfrom(dlg->sock,buf,sizeof(buf)-1,0,(sockaddr*)&(dlg->client),&len);
if(result==SOCKET_ERROR)
{
AfxMessageBox("receive");
return -1;
}
dlg->m_revc.Format("%s",buf);
AfxMessageBox(dlg->m_revc);
}
return 0;
}
{
int length;
char* buf;
CString str;
GetDlgItemText(IDC_EDIT1,str);
buf=str.GetBuffer(0);
sockaddr_in to;
to.sin_family=PF_INET;
to.sin_addr.s_addr=INADDR_BROADCAST; to.sin_port=htons(PortNum);
buf[str.GetLength()]=0;
if((length=sendto(sock,buf,sizeof(buf),0,(LPSOCKADDR)&to,sizeof(SOCKADDR)))==SOCKET_ERROR)
{
AfxMessageBox("发送出错");
closesocket(sock);
return;
}
AfxMessageBox(buf);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货