今天要用到非同步事件選擇模型, 發現demo原型不能正常退出.
除錯了一下, 用wsasetevent + waitforsingleobject + 全域性標記搞定.
如果以後要用到非同步事件選擇模型, 在這個demo上直接加業務邏輯.
srcasynceventselect.zip
//#include "stdafx.h"
#include
#include
#include "socketinit.h"
csocketinit g_init;
socket g_asyncselectclients[wsa_maximum_wait_events] = ;
wsaevent g_asyncselectevents[wsa_maximum_wait_events] = ;
int g_icount = 0;
bool g_bneedquit = true; ///< 標誌 : 退出程式
bool addtoasynceventselectary(socket socket);
void removefromasynceventselectary(int iindex);
unsigned
int __stdcall workthread(void *lpparam);
int main(int argc, char* argv)
addr.sin_family = af_inet;
addr.sin_port = htons(12345);
addr.sin_addr
.s_un
.s_addr = inet_addr("127.0.0.1");
irc = bind(socket, (sockaddr*)&addr, sizeof(addr));
if (socket_error == irc)
irc = listen(socket, somaxconn);
if (socket_error == irc)
puts("server start...");
//socket選擇事件物件
addtoasynceventselectary(socket);
//開啟執行緒
handle hthread = (handle)_beginthreadex(null, 0, workthread, null, 0, null);
system("pause");
wsasetevent(g_asyncselectevents[0]);
waitforsingleobject(hthread, -1);
closehandle(hthread);
} while (0);
/// 釋放資源
for (i = 0; i < g_icount; i++)
puts("server stop...");
system("pause"); ///< the end
/** rs
server start...
請按任意鍵繼續. . .
workthread end...
server stop...
請按任意鍵繼續. . .
*/return0;}
bool addtoasynceventselectary(socket socket)
return
false;
}void removefromasynceventselectary(int iindex)
g_asyncselectclients[iindex] = g_asyncselectclients[g_icount];
g_asyncselectevents[iindex] = g_asyncselectevents[g_icount];
g_icount--;
}}unsigned
int __stdcall workthread(void *lpparam)
; int irc = 0;
wsanetworkevents events;
dword dwrc = 0;
int iindex = 0;
getpeername(sclient, (sockaddr*)&addr, &ilen);
while (true)
iindex = dwrc - wsa_wait_event_0;
if (g_bneedquit)
//獲取事件
if (wsaenumnetworkevents(g_asyncselectclients[iindex], g_asyncselectevents[iindex], &events)
== socket_error)
switch(events.lnetworkevents)
break;
case fd_read:
break;
case fd_close:
break;}}
printf("workthread end...\r\n");
return0;}
// socketinit.h: inte***ce for the csocketinit class.
/////
//////
//////
//////
//////
//////
//////
//////
//////
//////
//////
///////
#if !defined(afx_socketinit_h__c22fe742_fe79_4a70_acc4_84fb39fb9118__included_)
#define afx_socketinit_h__c22fe742_fe79_4a70_acc4_84fb39fb9118__included_
#if _msc_ver > 1000
#pragma once
#endif // _msc_ver > 1000
#include
#include
#pragma comment(lib, "ws2_32.lib")
class csocketinit
virtual ~csocketinit()
};inline void showerrormsg()
#endif // !defined(afx_socketinit_h__c22fe742_fe79_4a70_acc4_84fb39fb9118__included_)
非同步選擇模型
非同步選擇 wsaasyncselect 模型是乙個有用的非同步 i o 模型。利用這個模型,應用程式可在乙個套接字上,接收以 windows 訊息為基礎的網路事件通知。具體 的做法是在建好乙個套接字後,呼叫wsaasyncselect函式。該模型的核心即是wsaasyncselect函式。wsaa...
非同步選擇模型
非同步選擇 wsaasyncselect 模型是乙個有用的非同步 i o 模型。利用這個模型,應用程式可在乙個套接字上,接收以 windows 訊息為基礎的網路事件通知。具體 的做法是在建好乙個套接字後,呼叫wsaasyncselect函式。該模型的核心即是wsaasyncselect函式。wsaa...
二 非同步選擇模型(WSAAsyncSelect)
非同步選擇 wsaasyncselect 模型是乙個有用的非同步 i o 模型。利用這個模型,應用程式可在乙個套接字上,接收以 windows 訊息為基礎的網路事件通知。具體的做法是在建好乙個套接字後,呼叫wsaasyncselect函式。該模型的核心即是wsaasyncselect函式。要想使用 ...