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 ...
随机推荐
- Python-变量、变量作用域、垃圾回收机制原理-global nonlocal
变量实现原理决定了Python使用的垃圾回收机制为变量引用计数,当这个对象引用计数为0时候,则会自动执行__del__函数回收资源, del方法只是把变量指向的对象引用计数减一而已并删除这个变量 表达 ...
- Centos-退出抽取设备-eject
eject 退出抽取设备,如光驱或磁带,如果设备已经挂载,则卸载设备 相关选项 -q 退出磁盘 -r 退出光盘 -d 显示默认设备
- linux内核 idr机制
idr机制解决了什么问题?为什么需要idr机制(或者说,idr机制这种解决方案,相对已有的其他方案,有什么优势所在) ? idr在linux内核中指的就是整数ID管理机制,从本质上来说,这就是一种将整 ...
- matplotlib绘图教程,设置标签与图例
大家好,欢迎大家阅读周四数据处理专题,我们继续介绍matplotlib作图工具. 在上一篇文章当中我们介绍了matplotlib这个包当中颜色.标记和线条这三种画图的设置,今天我们同样也介绍三种新的设 ...
- Java (一)下载APACHE Commons IO
一,进入APACHE的官网,在首页往下找APACHE PROJECT LIST > Commons,点击进入. 二,进入Apache Commons页面,找IO组件,点击进入. 三.点击链接Do ...
- PADS Layout VX.2.3 将PCB中的元器件封装保存到库
工具1:PADS Layout VX.2.3 菜单File > Library...,打开Library Manager,点击Create New Lib...新建一个库. 使用快捷键Ctrl ...
- DM8数据库备份还原的原理及应用
(本文部分内容摘自DM产品技术支持培训文档,如需要更详细的文档,请查询官方操作手册,谢谢) 一.原理 1.DM8备份还原简介 1.1.基本概念 (1)表空间与数据文件 ▷ DM8表空间类型: ▷ SY ...
- Vue路由History模式分析
Vue路由History模式分析 Vue-router是Vue的核心组件,主要是作为Vue的路由管理器,Vue-router默认hash模式,通过引入Vue-router对象模块时配置mode属性可以 ...
- NOI 2012 【迷失游乐园】
这道题,额,反正我是刚了2天,然后就萎了......(是不是觉得我很菜) 题目描述: 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩. 进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐 ...
- (数据科学学习手札97)掌握pandas中的transform
本文示例文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 开门见山,在pandas中,transform是 ...