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;
}
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;
}
相关文章推荐
- 使用异步选择函数WSAAsyncSelect
- WSAAsyncSelect()
- Windows套接字WSAAsyncSelect
- 4 The WSAEventSelect Model
- WSAAsyncSelect阻塞与非阻塞
- Winsock异步事件通知模型WSAAsyncSelect
- vc++注册异步事件WSAAsyncSelect函数中的窗体句柄的获得
- 消息机制-WSAAsyncSelect
- 异步选择模型WSAAsyncSelect
- 套接字的select、WsaAsyncSelect、WsaEventSelect模型
- error C2375: 'WSAAsyncSelect' : redefinition; different linkage 等错误解决方法(gsoap调用WebService)
- WSAAsyncselect 模型中,服务器与客户端消息的收发
- 二、异步选择模型(WSAAsyncSelect)
- WSAAsyncSelect消息编程
- WSAAsyncSelect阻塞与非阻塞
- Winsock I/O模型之WSAAsyncSelect
- Windows Socket编程TCP范例之三(WSAAsyncSelect)
- 使用WSAAsyncSelect的Socket通信
- WSAAsyncSelect之win32示例模型
- 消息机制-WSAAsyncSelect