您的位置:首页 > 其它

3 The WSAAsyncSelect Model

2013-03-07 09:45 127 查看
3 The WSAAsyncSelect Model

3.1 APIs

1) int WSAAsyncSelect(

SOCKET s,

HWND hWnd,

unsigned int wMsg,

long lEvent

);

you can use it as:

2) WSAAsyncSelect(s,hwnd,WM_SOCKET,FD_CONNECT│FD_READ│FD_WRITE│ FD_CLOSE);

LRESULT CALLBACK WindowProc(

HWND hWnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

);

3) LRESULT CALLBACK WindowProc(

HWND hWnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

);

Note: The wParam parameter identifies the socket on which a network event has occurred. The lParam parameter contains two important pieces of information—the low word of lParam specifies the network event that has occurred, and the high word of lParam contains
any error code.

When network event messages arrive at a window procedure, the application should first check the lParam high-word bits to determine whether a network error has occurred on the socket. There is a special macro, WSAGETSELECTERROR, that returns the value of the
high-word bits error information. After the application has verified that no error occurred on the socket, the application should determine which network event type caused the Windows message to fire by reading the low-word bits of lParam. Another special
macro, WSAGETSELECTEVENT, returns the value of the low-word portion of lParam.

3.2 Samples

The following example demonstrates how to manage window messages when using the WSAAsyncSelect I/O model. The code highlights the steps needed to develop a basic server application and removes the programming details of developing a fully featured Windows application.

#define WM_SOCKET WM_USER + 1

#include <winsock2.h>

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance, LPSTR lpCmdLine,

int nCmdShow)

{

WSADATA wsd;

SOCKET Listen;

SOCKADDR_IN InternetAddr;

HWND Window;

// Create a window and assign the ServerWinProc

// below to it

Window = CreateWindow();

// Start Winsock and create a socket

WSAStartup(MAKEWORD(2,2), &wsd);

Listen = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

// Bind the socket to port 5150

// and begin listening for connections

InternetAddr.sin_family = AF_INET;

InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);

InternetAddr.sin_port = htons(5150);

bind(Listen, (PSOCKADDR) &InternetAddr,

sizeof(InternetAddr));

// Set up window message notification on

// the new socket using the WM_SOCKET define

// above

WSAAsyncSelect(Listen, Window, WM_SOCKET,

FD_ACCEPT │ FD_CLOSE);

listen(Listen, 5);

// Translate and dispatch window messages

// until the application terminates

while (1) {

// ...

}

}

BOOL CALLBACK ServerWinProc(HWND hDlg,UINT wMsg,

WPARAM wParam, LPARAM lParam)

{

SOCKET Accept;

switch(wMsg)

{

case WM_PAINT:

// Process window paint messages

break;

case WM_SOCKET:

// Determine whether an error occurred on the

// socket by using the WSAGETSELECTERROR() macro

if (WSAGETSELECTERROR(lParam))

{

// Display the error and close the socket

closesocket( (SOCKET) wParam);

break;

}

// Determine what event occurred on the

// socket

switch(WSAGETSELECTEVENT(lParam))

{

case FD_ACCEPT:

// Accept an incoming connection

Accept = accept(wParam, NULL, NULL);

// Prepare accepted socket for read,

// write, and close notification

WSAAsyncSelect(Accept, hDlg, WM_SOCKET,

FD_READ │ FD_WRITE │ FD_CLOSE);

break;

case FD_READ:

// Receive data from the socket in

// wParam

break;

case FD_WRITE:

// The socket in wParam is ready

// for sending data

break;

case FD_CLOSE:

// The connection is now closed

closesocket( (SOCKET)wParam);

break;

}

break;

}

return TRUE;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: