WSAAsyncSelect是服务器端的六种I/O模型之一,他的主要思想是运用了windows窗口的消息机制,用函数WSAAsyncSelect()将监听端口感兴趣的网络消息注册到窗口,然后在窗口的消息过程中处理,该模型只提供异步通知,并不提供异步数据传送,只适用于系统开销不大的情况。
使用该模型编程,需要基于窗口,以下CreateServerWindow提供了创建窗口的过程。
#include <cassert>
#include <iostream>
#include "ServerWindowCreator.h"
#include "SocketUtil.h"
HWND ServerWindowCreator::CreateServerWindow(HINSTANCE hInstance, LPCTSTR wndClass, WNDPROC wndProc)
{
assert(hInstance);
assert(wndClass);
assert(wndProc);
if(!hInstance || !wndClass || !wndProc)
return NULL;
HWND hWnd = NULL;
if (MyRegisterClass(hInstance, wndClass, wndProc))//注册窗口类
hWnd = InitInstance(hInstance, wndClass);//创建窗口
return hWnd;
}
bool ServerWindowCreator::MyRegisterClass(HINSTANCE hInstance, LPCTSTR wndClass, WNDPROC wndProc)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;