WSAAsyncSelect模型

来源:互联网 发布:js arrayindexof 编辑:程序博客网 时间:2024/05/16 14:02

WSAAsyncSelect模型允许应用程序以Windows消息的形式接收网络事件的通知,这个模型是为了适应Windows的消息驱动环境而设置的,现在许多对性能要求不高的应用程序都采用WSAAsyncSelect模型,MFC中的CSocket类也使用了这种模型

1   消息通知和WSAAsyncSelect函数

           WSAAsyncSelect函数自动把套接字设为非阻塞模式,并且为套接字绑定一个窗口句柄,当有网络事件发生时,便向这个窗口发送消息。函数用法如下:

  

The WSAAsyncSelect function requests Windows message-based notification of network events for a socket.

int WSAAsyncSelect(  __in          SOCKET s,     //需要设置的套接字句柄  __in          HWND hWnd,    //指定的一个窗口句柄  __in          unsigned int wMsg,//套接字的通知消息将被发送到与其对应的窗口过程中,网络事件到来时接收到得消息ID,可以在WM_USER以上的数值中任意选择一个用作ID  __in          long lEvent  //指定哪些通知码需要发送);
最后一个参数LEvent指定了要发送的通知码,可以是如下取值的组合:

ValueMeaningFD_READSet to receive notification of readiness for reading.FD_WRITEWants to receive notification of readiness for writing.FD_OOBWants to receive notification of the arrival of OOB data.FD_ACCEPTWants to receive notification of incoming connections.FD_CONNECTWants to receive notification of completed connection or multipoint join operation.FD_CLOSEWants to receive notification of socket closure.FD_QOSWants to receive notification of socket Quality of Service (QOS) changes.FD_GROUP_QOSWants to receive notification of socket group Quality of Service (QOS) changes (reserved for future use with socket groups). Reserved.FD_ROUTING_INTERFACE_CHANGEWants to receive notification of routing interface changes for the specified destination(s).FD_ADDRESS_LIST_CHANGEWants to receive notification of local address list changes for the socket protocol family.


例如,在监听套接字上可以这样调用WSAAsyncSelect函数:

Issuing a WSAAsyncSelect for a socket cancels any previous WSAAsyncSelect or WSAEventSelect for the same socket. For example, to receive notification for both reading and writing, the application must callWSAAsyncSelect with bothFD_READ and FD_WRITE, as follows:

rc = WSAAsyncSelect(s, hWnd, wMsg, FD_READ|FD_WRITE);
上述代码将套接字s设为窗口通知消息类型当有客户读或写时,Winsock接口将向指定的窗口发送WM_SOCKET消息。

成功调用WSAAsyncSelect之后,应用程序便开始以Windows消息的形式在窗口函数接收网络事件通知。下面是窗口函数的定义:

The WindowProc function is an application-defined function that processes messages sent to a window. TheWNDPROC type defines a pointer to this callback function.WindowProc is a placeholder for the application-defined function name.

Syntax

LRESULT CALLBACK WindowProc(          HWND hwnd,    UINT uMsg,    WPARAM wParam,    LPARAM lParam);

Parameters

hwnd
[in] Handle to the window.
uMsg
[in] Specifies the message.
wParam
[in] Specifies additional message information. The contents of this parameter depend on the value of theuMsg parameter.
lParam
[in] Specifies additional message information. The contents of this parameter depend on the value of theuMsg parameter.
     wParam参数指定了发生网络事件的套接字句柄,lParam参数的低字位指定了发生的网络事件,高字位包含了任何可能出现的错误代码,可以使用宏WSAGETSELECTERROR和WSAGETSELECTEVENT将这些消息取出,这两个宏定义在Winsock2.h文件中。如果没有错误发生,出错代码为0.程序可以继续检查通知码,以确定发生的网络事件。

       应用举例:

   下面的例子说明了使用WSAAsyncSelect  I/O模型的方法,例子仅是一个简单的TCP服务器程序,接受客户端的连接请求,打印出接收到得数据。主要的程序代码如下:

   //为了使用WSAAsyncSelect I/O模型,程序创建了一个隐藏的窗口,窗口的函数是WindowProc

  

LRESULT CALLBACK WindowProc(          HWND hwnd,    UINT uMsg,    WPARAM wParam,    LPARAM lParam);
int  main()

{

   char  szClassName[]  ="MainWClass";

   WNDCLASSEX   wndclass;

/*

The WNDCLASSEX structure contains window class information. It is used with the RegisterClassEx and GetClassInfoEx  functions.

The WNDCLASSEX structure is similar to the WNDCLASS structure. There are two differences.WNDCLASSEX includes thecbSize member, which specifies the size of the structure, and thehIconSm member, which contains a handle to a small icon associated with the window class.

Syntax

typedef struct {    UINT cbSize;    UINT style;    WNDPROC lpfnWndProc;    int cbClsExtra;    int cbWndExtra;    HINSTANCE hInstance;    HICON hIcon;    HCURSOR hCursor;    HBRUSH hbrBackground;    LPCTSTR lpszMenuName;    LPCTSTR lpszClassName;    HICON hIconSm;} WNDCLASSEX, *PWNDCLASSEX;
*/

            wndclass.cbSize=sizeof(wndclass);

            wndclass.style=CS_HREDRAW|CS_VREDRAW;

             wndclass.lpfnWndProc=WindowProc;

}

0 0
原创粉丝点击