newman1982 2005年03月21日
winsock编程的问题
我使用UDP数据包发送信息,接受方能受到FD_READ的信息,但是使用WSARecvFrom()后,总是会得到代号为10014的错误代码,既地址无效。请各位大侠指点迷津!!
程序代码如下:

发送方:
void CSendDlg::OnConnect()
{
// TODO: Add your control notification handler code here
if(!m_bConnected)//m_bConneted 用来判断连接状态
{
UpdateData(true);

WSADATA sockData;
int ret;
ret=WSAStartup(MAKEWORD(2,2),&sockData);


m_addr.sin_addr.s_addr=inet_addr(m_faraddr);
m_addr.sin_family=AF_INET;
m_addr.sin_port=htons(10002);

m_localaddr.sin_addr.s_addr=INADDR_ANY;
m_localaddr.sin_port=htons(10001);
m_localaddr.sin_family=AF_INET;


m_socket=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED|WSA_FLAG_MULTIPOINT_C_LEAF|WSA_FLAG_MULTIPOINT_D_LEAF);

if(m_socket==INVALID_SOCKET)
{
AfxMessageBox("SPY socket error");
return;
}
int iRet;
iRet=bind(m_socket,(SOCKADDR*)&m_localaddr,sizeof(m_localaddr));
if (iRet==SOCKET_ERROR)
{
AfxMessageBox("SPY bind error");
return;
}


HWND hWnd;
hWnd=this->GetSafeHwnd();

iRet=WSAAsyncSelect(m_socket,hWnd,WM_NETWORKEVENT,FD_WRITE);
if (iRet==SOCKET_ERROR)
{
//
ret=WSAGetLastError();
//10022

closesocket(m_socket);
AfxMessageBox("事件设置出错");
return;
}

m_bConnected=true;

}
else
{
AfxMessageBox("已连接");
}



}

void CSendDlg::OnSend()
{
// TODO: Add your control notification handler code here
int iRetCode=0;
if (!m_bConnected||m_socket==INVALID_SOCKET)
{
AfxMessageBox("please establish connect!");
return;
}
UpdateData(true);
strMsg=m_text;
WSABUF wsaBuf;
wsaBuf.len=strMsg.GetLength();
wsaBuf.buf=strMsg.GetBuffer(wsaBuf.len);

iRetCode=WSASendTo(m_socket,&wsaBuf,1,(DWORD*)&wsaBuf.len,0,(SOCKADDR*)&m_addr,sizeof(SOCKADDR),NULL,NULL);
int j;
j=WSAGetLastError();
if (iRetCode==SOCKET_ERROR)
{

AfxMessageBox("error");
return;
}
strMsg.ReleaseBuffer();
return;
}
LONG CSendDlg::OnNetworkEvent(WPARAM wParam,LPARAM lParam)
{
if(WSAGETSELECTERROR(lParam)!=0)
{
AfxMessageBox("error");
return 1L;
}
switch(WSAGETSELECTEVENT(lParam))
{
case FD_WRITE:
AfxMessageBox("ready");
break;
default:
break;
}
return 0L;
}


接收方:

void CReceiveDlg::OnConnect()
{
// TODO: Add your control notification handler code here
if(!m_bConnected)
{
WSADATA sockData;
int ret;
ret=WSAStartup(MAKEWORD(2,2),&sockData);

UpdateData(true);

m_faraddr.sin_addr.s_addr=inet_addr(m_addr);
m_faraddr.sin_family=AF_INET;
//
m_faraddr.sin_port=htons(10001);
//

m_localaddr.sin_addr.s_addr=INADDR_ANY;
m_localaddr.sin_port=htons(10002);
m_localaddr.sin_family=AF_INET;

m_socket=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED|WSA_FLAG_MULTIPOINT_C_LEAF|WSA_FLAG_MULTIPOINT_D_LEAF);

if(m_socket==INVALID_SOCKET)
{
AfxMessageBox("SPY socket error");
return;
}

int iRet;
iRet=bind(m_socket,(SOCKADDR*)&m_localaddr,sizeof(m_localaddr));
if (iRet==SOCKET_ERROR)
{
AfxMessageBox("SPY bind error");
return;
}

HWND hWnd;
hWnd=this->GetSafeHwnd();

iRet=WSAAsyncSelect(m_socket,hWnd,WM_NETWORKEVENT,FD_READ);
if (iRet==SOCKET_ERROR)
{

ret=WSAGetLastError();

closesocket(m_socket);
AfxMessageBox("事件设置出错");
return;
}

m_bConnected=true;
}
else
{
AfxMessageBox("已连接");
}
}
LONG CReceiveDlg::OnNetworkEvent(WPARAM wParam,LPARAM lParam)
{
if(WSAGETSELECTERROR(lParam)!=0)
{
AfxMessageBox("error");
return 1L;
}
switch(WSAGETSELECTEVENT(lParam))
{

case FD_READ:
OnReceive();
break;
default:
break;
}
return 0L;
}
void CReceiveDlg::OnReceive()
{
char buffer[0x1000];
int iRet=0;
unsigned long ret=0;
int iLen=sizeof(m_addr);
int iFlag=0;
WSABUF wsaRecvBuf;
wsaRecvBuf.len=0x1000;
wsaRecvBuf.buf=buffer;

iRet=WSARecvFrom(m_socket,&wsaRecvBuf,1,&ret,(unsigned long*)&iFlag,(SOCKADDR*)&m_faraddr,&iLen,NULL,NULL);
if (iRet==SOCKET_ERROR)
{
int iret;
iret=WSAGetLastError();//////程序运行到这里就会死机,不知道为什么??///显示10014
AfxMessageBox("error");
return;
}
}




请各位高手帮忙解释一下,小弟不胜感激!!!!!!
...全文
124 点赞 收藏 10
写回复
10 条回复

还没有回复,快来抢沙发~

发动态
发帖子
网络编程
创建于2007-09-28

7878

社区成员

6.4w+

社区内容

VC/MFC 网络编程
社区公告
暂无公告