簡單的iocp例子

2021-06-03 01:57:35 字數 1740 閱讀 7520

#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

#define opt_write 1

class cclient

~cclient()

socket m_client;

dword m_transize;

char* m_buffer;

string m_ip;

};class cperdata

~cperdata()

wsabuf m_buffer;

int m_opt;

char m_buf[dft_buffer_size];

};class ciocp

else

}else

if(0 == error)

}*/

if(0 == error)

else if (ikeepalive == 1)}}

} /* if(0==error)}*/

} void lisenwork()

; int asize = 0;

::getpeername(s, (sockaddr*)&addr,&asize);

client->m_ip = inet_ntoa(addr.sin_addr);

err = (int)::createiocompletionport((handle)client->m_client, m_hiocp,(ulong_ptr)client,0 );

dword flag=0; //must be set 0,or wsarecv return -1 and getlasterror is 10045

dword rsize = 0;

err = ::wsarecv(client->m_client,&pdata->m_buffer,1,&rsize,&flag,&pdata->m_overlp,null);

err =::getlasterror();

int i = 0;

}else if ((nwevnts.lnetworkevents & fd_close)&&(nwevnts.ierrorcode[fd_close_bit] == 0))

}} static unsigned int winapi listenproc(void* proc)

void removeclient(cclient* client)

void conwork()

if (pdata->m_opt==opt_read)

else if (pdata->m_opt==opt_write)

}else

}} static unsigned int winapi conproc(void* proc)

private:

socket m_slisten;

handle m_hiocp;

sockaddr_in m_addr;

handle m_hlisten;

handle m_hwork[2];

wsaevent m_wsaevent;

setm_client;

};

IOCP簡單實現

本人工作是服務端效能測試,因工作需要開發機械人框架,選用底層的時候看到網上滿大街的iocp介紹,還有說iocp比wsaasyncselect複雜等等,所以只好轉wsaasyncselect實現。因併發客戶端需要進行除錯測試,壓力測試工具作為客戶端來說效能十分重要,沒有經過測試的鬼知道秒併發多少請求,...

簡單的使用 IOCP 模型的 TCP 伺服器例子

例子中有兩種型別的執行緒 主線程和它建立的執行緒。主線程建立監聽套接字,建立額外的工作執行緒,關聯 iocp 負責等待和接受到來的連線等。由主線程建立的執行緒負責處理 i o事件,這些執行緒呼叫 getqueuedcompletionstatus 函式在完成埠物件上等待完成的 i o操作。getqu...

iocp之簡單實現

1 標頭檔案 include include ifdef windows pragma comment lib,ws2 32.lib socket程式設計需用的動態鏈結庫 pragma comment lib,kernel32.lib iocp需要用到的動態鏈結庫 endif log view lo...