本人想用UDP协议接收数据,并用WSAAsyncSelect进行消息响应,在初始化的时候如下开了一个socket:

SOCKADDR_IN server;
memset(&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("192.168.0.1");
server.sin_port = htons(3550); int aa=SOCKET_ERROR;
SOCKET sk=socket(AF_INET,SOCK_DGRAM,0); long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE;
m_hWnd=CReceiveView::GetSafeHwnd();
  if (WSAAsyncSelect(sk,m_hWnd,UDPSocket, lEvent) == SOCKET_ERROR)
{

     int err=GetLastError();
AfxMessageBox("bind message error!");
}其中:
#define UDPSocket WM_USER + 0x13
在 receiveView.h 中加上:
protected:
//{{AFX_MSG(CReceiveView)
// NOTE - the ClassWizard will add and remove member functions here.
//    DO NOT EDIT what you see in these blocks of generated code !
afx_msg LRESULT OnUDPSocket(WPARAM wParam, LPARAM lParam);  //sun
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};在 receiveView.cpp 中加上:
BEGIN_MESSAGE_MAP(CReceiveView, CView)
...
ON_MESSAGE(UDPSocket, OnUDPSocket)  //sun
END_MESSAGE_MAP()为什么执行WSAAsyncSelect语句总是出现10022号错误(err=10022,即“提供了一个无效的参数。 ”)?
是什么参数无效,郁闷了好几天,望各位大虾帮忙看看,50分相送!!

解决方案 »

  1.   

    WSAAsyncSelect之前,
    你的socket 没有bind端口
      

  2.   

    飘人兄,我改成:
             SOCKADDR_IN server;
             memset(&server,0,sizeof(server));
             server.sin_family = AF_INET;
             server.sin_addr.s_addr = inet_addr("192.168.0.1");
             server.sin_port = htons(3550); SOCKET sk=socket(AF_INET,SOCK_DGRAM,0);
    int aa=SOCKET_ERROR;
    aa=bind(sk,(SOCKADDR*)&server,sizeof(server)); if(aa==SOCKET_ERROR)
    {
    TRACE(" bind error \n");
    } long lEvent=FD_CONNECT|FD_READ|FD_WRITE|FD_CLOSE;
    m_hWnd=CReceiveView::GetSafeHwnd();
      if (WSAAsyncSelect(sk,m_hWnd,UDPSocket, lEvent) == SOCKET_ERROR)//
    {

         int err=GetLastError();
    AfxMessageBox("bind message error!");
    }
      

  3.   

    不是吧?这是UDP通信,怎么还用listen呢?今天把程序嵌入另外的程序中,居然Select成功了,不知为何,哎
      

  4.   

    WSAStartup调用了没?
    if(0 != WSAStartup(MAKEWORD(2,2), &wsaData)) 
    {
       AfxMessageBox("WSAStartup error!"); 
    }
      

  5.   

    long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE;
    UDP FD_ACCEPT | FD_CONNECT ????
    10022 WSAEINVAL 提供了一个无效的参数。在一个目前没有监听的Socket上使用accept也会产生