winsock IO模型WSAAsynSelect

WSAAsynSelect模型: WSAAsynSelect模型也是一个常用的异步I/O模型。应用程序可以在一个套接字上接收以 WINDOWS消息为基础的网络事件通知。该模型的实现方法是通过调用WSAAsynSelect函 数 自动将套接字设置为非阻塞模式,并向WINDOWS注册一个或多个网络时间,并提供一 个通知时使用的窗口句柄。当注册的事件发生时,对应的窗口将收到一个基于消息的通知。

int  WSAAsyncSelect( SOCKET s, HWND hWnd, u_int wMsg, long lEvent);

s为需要事件通知的套接字 hWnd为接收消息的窗口句柄 wMsg为要接收的消息 lEvent为掩码,指定应用程序感兴趣的网络事件组合,主要如下:

#define FD_READ_BIT 0
#define FD_READ (1 << FD_READ_BIT)
#define FD_WRITE_BIT 1
#define FD_WRITE (1 << FD_WRITE_BIT)
#define FD_OOB_BIT 2
#define FD_OOB (1 << FD_OOB_BIT)
#define FD_ACCEPT_BIT 3
#define FD_ACCEPT (1 << FD_ACCEPT_BIT)

#define FD_CONNECT_BIT 4
#define FD_CONNECT (1 << FD_CONNECT_BIT)
#define FD_CLOSE_BIT 5
#define FD_CLOSE (1 << FD_CLOSE_BIT)

WSAAsynSelect实例:
#include <winsock.h>
 #include <tchar.h>
 
 #define PORT         5150
 #define MSGSIZE      1024
 #define WM_SOCKET     WM_USER+1 
 #pragma comment(lib, "ws2_32.lib")
 
 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
 {
      static TCHAR szAppName[] = _T("AsyncSelect Model");
      HWND            hwnd ;
      MSG             msg ;
      WNDCLASS        wndclass ;
 
      wndclass.style            = CS_HREDRAW | CS_VREDRAW ;
      wndclass.lpfnWndProc      = WndProc ;
      wndclass.cbClsExtra       = 0 ;
      wndclass.cbWndExtra       = 0 ;
      wndclass.hInstance        = hInstance ;
      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION) ;
      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW) ;
      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
      wndclass.lpszMenuName     = NULL ;
      wndclass.lpszClassName = szAppName ;
 
      if (!RegisterClass(&wndclass))
      {
        MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
        return 0 ;
      }
 
      hwnd = CreateWindow (szAppName,                     // window class name
                           TEXT ("AsyncSelect Model"), // window caption
                           WS_OVERLAPPEDWINDOW,           // window style
                           CW_USEDEFAULT,                 // initial x position
                           CW_USEDEFAULT,                 // initial y position
                           CW_USEDEFAULT,                 // initial x size
                           CW_USEDEFAULT,                 // initial y size
                           NULL,                          // parent window handle
                           NULL,                          // window menu handle
                           hInstance,                     // program instance handle
                           NULL) ;                        // creation parameters
 
      ShowWindow(hwnd, iCmdShow);
      UpdateWindow(hwnd);
 
      while (GetMessage(&msg, NULL, 0, 0))
      {
        TranslateMessage(&msg) ;
        DispatchMessage(&msg) ;
      }
   
      return msg.wParam;
 }
 
 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
      WSADATA          wsd;
      static SOCKET sListen;
      SOCKET           sClient;
      SOCKADDR_IN      local, client;
      int              ret, iAddrSize = sizeof(client);
      char             szMessage[MSGSIZE];
 
      switch (message)
      {
 case WM_CREATE:
        // Initialize Windows Socket library
      WSAStartup(0x0202, &wsd);
   
      // Create listening socket
        sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     
      // Bind
      local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
      local.sin_family = AF_INET;
      local.sin_port = htons(PORT);
      bind(sListen, (struct sockaddr *)&local, sizeof(local));
   
      // Listen
       listen(sListen, 3);
 
        // Associate listening socket with FD_ACCEPT event
      WSAAsyncSelect(sListen, hwnd, WM_SOCKET, FD_ACCEPT);
      return 0;
 
      case WM_DESTROY:
        closesocket(sListen);
        WSACleanup();
        PostQuitMessage(0);
        return 0;
   
      case WM_SOCKET:
        if (WSAGETSELECTERROR(lParam))//lParam的高字节包含了可能出现的任何的错误代码
        {
          closesocket(wParam);
          break;
        }
     
        switch (WSAGETSELECTEVENT(lParam)) //lParam的低字节指定已经发生的网络事件
        {
        case FD_ACCEPT:
          // Accept a connection from client
          sClient = accept(wParam, (struct sockaddr *)&client, &iAddrSize);
       
          // Associate client socket with FD_READ and FD_CLOSE event
          WSAAsyncSelect(sClient, hwnd, WM_SOCKET, FD_READ | FD_CLOSE);
          break;
 
        case FD_READ:
          ret = recv(wParam, szMessage, MSGSIZE, 0);
 
          if (ret == 0 || ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET)
          {
            closesocket(wParam);
          }
          else
          {
            szMessage[ret] = '\0';
            send(wParam, szMessage, strlen(szMessage), 0);
          }
          break;
       
        case FD_CLOSE:
          closesocket(wParam);      
          break;
        }
        return 0;
      }
   
      return DefWindowProc(hwnd, message, wParam, lParam);
 }

 

当应用程序窗口hWnd收到消息时,wMsg.wParam参数标识了套接字,lParam的低字标明 了网络事件,高字则包含错误代码。

 

posted @ 2012-08-11 17:02  azraelly  阅读(1889)  评论(0编辑  收藏  举报