例子中有兩種型別的執行緒
-主線程和它建立的執行緒。主線程建立監聽套接字,建立額外的工作執行緒,關聯
iocp
,負責等待和接受到來的連線等。由主線程建立的執行緒負責處理
i/o事件,這些執行緒呼叫
getqueuedcompletionstatus
函式在完成埠物件上等待完成的
i/o操作。
getqueuedcompletionstatus
函式返回後,說明發生了如下事件之一。(1
)getqueuedcompletionstatus
呼叫失敗,說明在此套接字上有錯誤發生。(2
)bytestransferred為0
說明套接字被對方關閉。注意,
per-handle
資料用來引用與
i/o操作相關的套接字。
(3)i/o請求成功完成。通過
per-i/o
資料(這是程式自定義的結構)中的
operationtype
域檢視哪個
i/o請求完成了。
/
// iocpdemo.cpp檔案
#include "../common/initsock.h"
#include #include cinitsock thesock; // 初始化winsock庫
#define buffer_size 1024
typedef struct _per_handle_data // per-handle(控制代碼惟一)資料
per_handle_data, *pper_handle_data;
typedef struct _per_io_data // per-i/o(i/o惟一)資料
per_io_data, *pper_io_data;
dword winapi serverthread(lpvoid lpparam)
// 套節字被對方關閉
if (dwtrans == 0 &&
(pperio->noperationtype = op_read || pperio->noperationtype == op_write))
// 通過per-i/o資料中的noperationtype域檢視什麼i/o請求完成了
switch(pperio->noperationtype)
break;
case op_write: // 本例中沒有投遞這些型別的i/o請求
case op_accpt:
break;
} }return 0;
}void main()
}
IOCP模型TCP伺服器
主線程建立監聽套接字,建立額外工作執行緒,關聯iocp,負責等待和接受到來的連線。呼叫getqueuedcompletionstatus函數,函式返回 1 呼叫失敗 2 套接字被對方關閉 3 請求成功完成 程式首先定義per handleper io的運算元據的結構型別 define buffer ...
簡單的iocp例子
include include include include include using namespace std define i port 1666 define i addr 0.0.0.0 define dft buffer size 4096 define opt read 0 def...
IOCP模型與EPOLL模型的比較
一 iocp和epoll之間的異同。異 1 iocp是windows系統下使用。epoll是linux系統下使用。2 iocp是io操作完畢之後,通過get函式獲得乙個完成的事件通知。epoll是當你希望進行乙個io操作時,向epoll查詢是否可讀或者可寫,若處於可讀或可寫狀態後,epoll會通過e...