博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重叠IO
阅读量:5749 次
发布时间:2019-06-18

本文共 3868 字,大约阅读时间需要 12 分钟。

// 重叠IO网络模式.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include
#include
#include
using namespace std;#pragma comment (lib,"ws2_32.lib")#define PORT 6000#define MSGSIZE 1024typedef struct { WSAOVERLAPPED overlap; WSABUF Buffer; char szMessage[MSGSIZE]; DWORD NumberOfBytesRecvd; DWORD Flags;}PER_IO_OPERATION_DATA,*LPPER_IO_OPERATION_DATA;int g_iToalComn = 0;SOCKET g_CliSocketArr[MAXIMUM_WAIT_OBJECTS];WSAEVENT g_CliEventArr[MAXIMUM_WAIT_OBJECTS];LPPER_IO_OPERATION_DATA g_pPerIODataArr[MAXIMUM_WAIT_OBJECTS];DWORD WINAPI WorkerThread(LPVOID lparam);void Cleanup(int);bool InitNetEvn(){ WSADATA wsa; if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { cout << "网络环境失败" << endl; return false; } if (LOBYTE(wsa.wVersion)!=2||HIBYTE(wsa.wVersion)!=2) { cout << "版本号不对" << endl; return false; } return true;}int main(){ SOCKET sListen, sClient; SOCKADDR_IN local, client; DWORD dwThreadId; int iaddrSize = sizeof(SOCKADDR_IN); if (!InitNetEvn()) { return -1; } sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); local.sin_addr.S_un.S_addr = htonl(INADDR_ANY); local.sin_family = AF_INET; local.sin_port = htons(PORT); bind(sListen, (sockaddr *)&local, iaddrSize); listen(sListen, 3); CreateThread(NULL, NULL, WorkerThread, NULL, NULL, &dwThreadId); while (TRUE) { sClient = accept(sListen, (sockaddr *)&client, &iaddrSize); printf("Accept client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port)); g_CliSocketArr[g_iToalComn] = sClient; g_pPerIODataArr[g_iToalComn] = (LPPER_IO_OPERATION_DATA)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PER_IO_OPERATION_DATA)); g_pPerIODataArr[g_iToalComn]->Buffer.len = MSGSIZE; g_pPerIODataArr[g_iToalComn]->Buffer.buf = g_pPerIODataArr[g_iToalComn]->szMessage; g_CliEventArr[g_iToalComn] = g_pPerIODataArr[g_iToalComn]->overlap.hEvent = WSACreateEvent(); WSARecv( g_CliSocketArr[g_iToalComn], &g_pPerIODataArr[g_iToalComn]->Buffer, 1, &g_pPerIODataArr[g_iToalComn]->NumberOfBytesRecvd, &g_pPerIODataArr[g_iToalComn]->Flags, &g_pPerIODataArr[g_iToalComn]->overlap, NULL); g_iToalComn++; } closesocket(sListen); WSACleanup(); return 0;}DWORD WINAPI WorkerThread(LPVOID lparam){ int ret, index; DWORD cbTransferred; while (true) { //判断出一个重叠IO调用是否完成 ret = WSAWaitForMultipleEvents(g_iToalComn, g_CliEventArr, FALSE, 1000, FALSE); if (ret==WSA_WAIT_TIMEOUT||ret==WSA_WAIT_FAILED) { continue; } index = ret - WSA_WAIT_EVENT_0; WSAResetEvent(g_CliEventArr[index]); WSAGetOverlappedResult( g_CliSocketArr[index], &g_pPerIODataArr[index]->overlap, &cbTransferred, TRUE, &g_pPerIODataArr[index]->Flags); if (cbTransferred==0) { Cleanup(index); } else { g_pPerIODataArr[index]->szMessage[cbTransferred] = '\0'; send(g_CliSocketArr[index], g_pPerIODataArr[index]->szMessage, \ cbTransferred, 0); WSARecv( g_CliSocketArr[index], &g_pPerIODataArr[index]->Buffer, 1, &g_pPerIODataArr[index]->NumberOfBytesRecvd, &g_pPerIODataArr[index]->Flags, &g_pPerIODataArr[index]->overlap, NULL); } } return 0;}void Cleanup(int index){ closesocket(g_CliSocketArr[index]); WSACloseEvent(g_CliEventArr[index]); HeapFree(GetProcessHeap(), 0, g_pPerIODataArr[index]); if (index

 

转载于:https://www.cnblogs.com/chaguang/p/7131153.html

你可能感兴趣的文章
iPhone开发面试题--葵花宝典
查看>>
EdbMails Convert EDB to PST
查看>>
POJ 2184
查看>>
存储过程简单实例
查看>>
大话 程序猿 眼里的 接口
查看>>
struts2用了哪几种模式
查看>>
replace函数结合正则表达式实现转化成驼峰与转化成连接字符串的方法
查看>>
ubuntu 初学常用命令
查看>>
WCF客户端与服务端通信简单入门教程
查看>>
判断是否含有中文
查看>>
Byte[]、Image、Bitmap 之间的相互转换
查看>>
玩转 React【第02期】:恋上 React 模板 JSX
查看>>
MyEclipse Web开发教程:XML & XML架构(一)
查看>>
linux文件权限与属性的更改
查看>>
Juniper系列之密码恢复
查看>>
spring cloud(一):微服务架构开篇
查看>>
Centos7安装完毕后无法联网的解决方法
查看>>
iptables防火墙(for linux平台)部署文档二
查看>>
CentOS 5.5 安装MYSQL
查看>>
HTML元素属性测试总结(续篇)
查看>>