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指定了要发送的通知码,可以是如下取值的组合:
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);
成功调用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
wParam参数指定了发生网络事件的套接字句柄,lParam参数的低字位指定了发生的网络事件,高字位包含了任何可能出现的错误代码,可以使用宏WSAGETSELECTERROR和WSAGETSELECTEVENT将这些消息取出,这两个宏定义在Winsock2.h文件中。如果没有错误发生,出错代码为0.程序可以继续检查通知码,以确定发生的网络事件。
- 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.
应用举例:
下面的例子说明了使用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;
}
- WSAAsyncSelect模型
- WSAAsyncSelect 模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WSAAsyncSelect模型
- WsaAsyncSelect模型
- WSAAsyncSelect模型
- 动作栏——ActionBar(一)
- office2007新建 Microsoft Office Word 文档.docx,打开后弹出转换文件对话框的解决办法
- javascript匿名函数
- 百度 腾讯 等名企多线程面试题目
- java中的类!
- WSAAsyncSelect模型
- libevent入门教程:Echo Server based on libevent
- 1.对象序列化可以实现
- (JavaCard) 技术入门 1 -- 名词普及
- Ubuntu12.04 开启ssh+win7 ssh登录
- 本机php环境搭建教程:windows环境下wampserver的配置教程——超级详细
- 文件操作成绩处理
- (JavaCard) 技术入门 2 -- 搭建JCDK开发环境
- Node.js v0.10.18中英文手册