IOCP 模型1
// IOCP.cpp : Defines the entry point for the console application.
//
// #include "stdafx.h" #include <WinSock2.h>
#include <Windows.h>
#include <process.h>
#pragma comment(lib, "WS2_32.lib") #define MAX_BUFFER 256
#define MAX_TIMEOUT 1000
#define MAX_SOCKET 1024
#define MAX_THREAD 64 typedef enum _OPERATION_INFO_
{
OP_NULL,
OP_READ,
OP_WRITE
}OPERATIONINFO; typedef struct _PER_HANDLE_DATA_
{
public:
_PER_HANDLE_DATA_()
{
clean();
}
~_PER_HANDLE_DATA_()
{
clean();
}
protected:
void clean()
{
sock = INVALID_SOCKET;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_port = htons(0);
addr.sin_family = AF_INET;
}
public:
SOCKET sock;
SOCKADDR_IN addr; }PERHANDLEDATA, *PPERHANDLEDATA; typedef struct _PER_IO_DTATA_
{
public:
_PER_IO_DTATA_()
{
clean();
}
~_PER_IO_DTATA_()
{
clean();
}
private:
void clean()
{
ZeroMemory(&ol, sizeof(ol));
memset(buf, 0, sizeof(buf));
wsaBuf.buf = buf;
wsaBuf.len = MAX_BUFFER;
opType = OP_NULL;
}
public:
WSAOVERLAPPED ol;
WSABUF wsaBuf;
char buf[MAX_BUFFER];
OPERATIONINFO opType;
}PERIODATA, *PPERIODATA; HANDLE hThread[MAX_THREAD] = {0};
int g_nThread = 0;
BOOL g_bExitThread = FALSE; unsigned __stdcall ThreadProc(LPVOID lParam); int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if(0 != WSAStartup(MAKEWORD(2, 2), &wsaData))
{
printf("WSAStartup failed with error code: %d/n", GetLastError());
return EXIT_FAILURE;
}
if(2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion))
{
printf("Socket version not supported./n");
WSACleanup();
return EXIT_FAILURE;
} // Create I/O Completion Port
HANDLE hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 0);
if(NULL == hIOCP)
{
printf("CreateIoCompletionPort failed with error code: %d/n", GetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// Create worker thread
SYSTEM_INFO si = {0};
GetSystemInfo(&si);
for(int i = 0; i < (int)si.dwNumberOfProcessors+2; i++)
{
hThread[g_nThread] = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, (LPVOID)hIOCP, 0, NULL);
if(NULL == hThread[g_nThread])
{
printf("_beginthreadex failed with error code: %d/n", GetLastError());
continue;
}
++g_nThread; if(g_nThread > MAX_THREAD)
{
break;
}
} // Create socket
SOCKET sListen = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
if(INVALID_SOCKET == sListen)
{
printf("WSASocket failed with error code: %d/n", WSAGetLastError());
goto EXIT_CODE;
}
SOCKADDR_IN addr;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons(5050);
if(SOCKET_ERROR == bind(sListen, (LPSOCKADDR)&addr, sizeof(addr)))
{
printf("bind failed with error code: %d/n", WSAGetLastError());
closesocket(sListen);
sListen = INVALID_SOCKET;
goto EXIT_CODE;
}
if(SOCKET_ERROR == listen(sListen, 5))
{
printf("listen failed with error code: %d/n", WSAGetLastError());
closesocket(sListen);
sListen = INVALID_SOCKET;
goto EXIT_CODE;
} printf("Server start, wait for client to connect .../n");
while(TRUE)
{
SOCKADDR_IN remote;
memset(&remote, 0, sizeof(remote));
int len = sizeof(remote); SOCKET sNew = WSAAccept(sListen, (LPSOCKADDR)&remote, &len, NULL, NULL);
if(INVALID_SOCKET == sNew)
{
printf("WSAAccept failed with error code: %d/n", WSAGetLastError());
continue;
}
printf("Client <%s : %d> come in./n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
PERHANDLEDATA* pPerHandleData = new PERHANDLEDATA;
pPerHandleData->sock = sNew;
memcpy(&(pPerHandleData->addr), &remote, sizeof(remote));
// Associate with IOCP
if(NULL == CreateIoCompletionPort((HANDLE)(pPerHandleData->sock), hIOCP, (ULONG_PTR)pPerHandleData, 0))
{
printf("CreateIoCompletionPort failed with error code: %d/n", GetLastError());
closesocket(pPerHandleData->sock);
delete pPerHandleData;
continue;
}
// Post Receive
PERIODATA* pPerIoData = new PERIODATA;
pPerIoData->opType = OP_READ;
DWORD dwTrans = pPerIoData->wsaBuf.len;
DWORD dwFlags = 0;
if(SOCKET_ERROR == WSARecv(pPerHandleData->sock, &(pPerIoData->wsaBuf), 1,
&dwTrans, &dwFlags, &(pPerIoData->ol), NULL))
{
if(WSA_IO_PENDING != WSAGetLastError())
{
printf("WSARecv failed with error code: %d/n", WSAGetLastError());
closesocket(pPerHandleData->sock);
delete pPerHandleData;
delete pPerIoData;
continue;
}
}
}
closesocket(sListen);
sListen = INVALID_SOCKET; EXIT_CODE:
g_bExitThread = TRUE;
PostQueuedCompletionStatus(hIOCP, 0, NULL, NULL);
WaitForMultipleObjects(g_nThread, hThread, TRUE, INFINITE);
for(int i = 0; i < g_nThread; i++)
{
CloseHandle(hThread[g_nThread]);
}
CloseHandle(hIOCP); // Close IOCP
WSACleanup();
return 0;
} unsigned __stdcall ThreadProc(LPVOID lParam)
{
HANDLE hIOCP = (HANDLE)lParam; PERHANDLEDATA* pPerHandleData = NULL;
PERIODATA* pPerIoData = NULL;
WSAOVERLAPPED* lpOverlapped = NULL;
DWORD dwTrans = 0;
DWORD dwFlags = 0;
while(!g_bExitThread)
{
BOOL bRet = GetQueuedCompletionStatus(hIOCP, &dwTrans, (PULONG_PTR)&pPerHandleData, &lpOverlapped, INFINITE);
if(!bRet)
{
printf("GetQueuedCompletionStatus failed with error: %d/n", WSAGetLastError());
continue;
}
else
{
pPerIoData = CONTAINING_RECORD(lpOverlapped, PERIODATA, ol);
if(0 == dwTrans)
{
printf("Client: <%s : %d> leave./n", inet_ntoa(pPerHandleData->addr.sin_addr), ntohs(pPerHandleData->addr.sin_port));
closesocket(pPerHandleData->sock);
delete pPerHandleData;
delete pPerIoData;
continue;
}
else
{
switch(pPerIoData->opType)
{
case OP_READ:
printf("recv client <%s : %d> data: %s/n", inet_ntoa(pPerHandleData->addr.sin_addr), ntohs(pPerHandleData->addr.sin_port), pPerIoData->buf);
pPerIoData->opType = OP_WRITE;
memset(&(pPerIoData->ol), 0, sizeof(pPerIoData->ol));
if(SOCKET_ERROR == WSASend(pPerHandleData->sock, &(pPerIoData->wsaBuf), 1, &dwTrans, dwFlags, &(pPerIoData->ol), NULL))
{
if(WSA_IO_PENDING != WSAGetLastError())
{
printf("WSASend failed with error code: %d./n", WSAGetLastError());
continue;
}
}
break; case OP_WRITE:
{
pPerIoData->opType = OP_READ;
dwFlags = 0;
memset(&(pPerIoData->ol), 0, sizeof(pPerIoData->ol));
memset(pPerIoData->buf, 0, sizeof(pPerIoData->buf));
pPerIoData->wsaBuf.buf = pPerIoData->buf;
dwTrans = pPerIoData->wsaBuf.len = MAX_BUFFER;
if(SOCKET_ERROR == WSARecv(pPerHandleData->sock, &(pPerIoData->wsaBuf), 1, &dwTrans, &dwFlags, &(pPerIoData->ol), NULL))
{
if(WSA_IO_PENDING != WSAGetLastError())
{
printf("WSARecv failed with error code: %d./n", WSAGetLastError());
continue;
}
}
}
break; default:
break;
}
}
}
}
return 0;
}
IOCP 模型1的更多相关文章
- Server Develop (八) IOCP模型
IOCP模型 IOCP全称I/O Completion Port,中文译为I/O完成端口.IOCP是一个异步I/O的Windows API,它可以高效地将I/O事件通知给应用程序,类似于Linux中的 ...
- IOCP模型
IOCP http://blog.csdn.net/zhongguoren666/article/details/7386592 Winsock IO模型之IOCP模型 http://blog.csd ...
- IOCP模型总结(转)
IOCP模型总结(转) IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请求 ...
- 【IOCP】 IOCP模型属于一种通讯模型- 较难
http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...
- IOCP模型与网络编程
IOCP模型与网络编程 一.前言: 在老师分配任务(“尝试利用IOCP模型写出服务端和客户端的代码”)给我时,脑子一片空白,并不知道什么是IOCP模型,会不会是像软件设计模式里面的工厂模 ...
- winsock编程IOCP模型实现代码
winsock编程IOCP模型实现代码 话不多说,上代码.借鉴<windows核心编程>部分源码和CSDN小猪部分代码. stdafx.h依赖头文件: #include <iostr ...
- IOCP模型与网络编
一.前言: 在老师分配任务(“尝试利用IOCP模型写出服务端和客户端的代码”)给我时,脑子一片空白,并不知道什么是IOCP模型,会不会是像软件设计模式里面的工厂模式,装饰模式之类的那些呢 ...
- [转载]IOCP模型的总结
原文:IOCP模型的总结 IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请 ...
- IOCP模型总结(总结回想)
IOCP旧代码重提.近期一直在玩其它方面的东东.时不时回想一下,收益多多. IOCP(I/O Completion Port,I/O完毕port)是性能最好的一种I/O模型.它是应用程序使用线程池处理 ...
- 很幽默的讲解六种Socket IO模型 Delphi版本(自己Select查看,WM_SOCKET消息通知,WSAEventSelect自动收取,Overlapped I/O 事件通知模型,Overlapped I/O 完成例程模型,IOCP模型机器人)
很幽默的讲解六种Socket IO模型(转)本文简单介绍了当前Windows支持的各种Socket I/O模型,如果你发现其中存在什么错误请务必赐教. 一:select模型 二:WSAAsyncSel ...
随机推荐
- TTL和CMOS电平
参考: 1.https://baike.baidu.com/item/TTL%E7%94%B5%E5%B9%B3/5904345 2.https://baike.baidu.com/item/CMOS ...
- Linux下clock子系统
常用API: 1.struct clk *clk_get(struct device *dev, const char *id):从一个时钟list链表中以dev或者字符id名称查找一个时钟clk结构 ...
- Ztree树节点应用
树节点增删改查: 前台jsp页面: <% String root=request.getContextPath();//获取项目目录 %> <SCRIPT type="te ...
- 多测师讲解selenium _assert断言_高级讲师肖sir
assert断言 # # 断言:最常用的断言方法if判断# assert Python语法中自带的断言from selenium import webdriverfrom time import sl ...
- 多测师肖sir_pdf转word方法
1.百度搜索 my love pdf 在线转换 2.输入wps 下载软件
- 多测师讲解python_安装001.1
Python+pycharm 一.Python安装 python现在主要就是python2和python3,目前python3最新是3.8,考虑稳定性我们用3.6,3.7等 第一步:下载Python安 ...
- 晋城6397.7539(薇)xiaojie:晋城哪里有xiaomei
晋城哪里有小姐服务大保健[微信:6397.7539倩儿小妹[晋城叫小姐服务√o服务微信:6397.7539倩儿小妹[晋城叫小姐服务][十微信:6397.7539倩儿小妹][晋城叫小姐包夜服务][十微信 ...
- SpringBoot整合Elasticsearch游标查询(scroll)
游标查询(scroll)简介 scroll 查询 可以用来对 Elasticsearch 有效地执行大批量的文档查询,而又不用付出深度分页那种代价. 游标查询会取某个时间点的快照数据. 查询初始化之后 ...
- 用python you-get下载视频
安装python3后 安装you-get包: pip3 install you-get 下载视频: 打开windows终端:运行 you-get url 查看视频信息: you-get -i url ...
- 扫描仪扫描文件处理-Python批量处理
多进程处理扫描出来的图片,参见: https://github.com/barrer/scan-helper bug问题反馈github提Issues