C++ CompletionPort(完成端口)示例
ECHO客户端
#include <WINSOCK2.H>
#include <stdio.h>
#define SERVER_ADDRESS "127.0.0.1" //服务器地址
#define PORT 5150 //端口
#define MSGSIZE 1024 //信息缓冲大小 #pragma comment(lib, "ws2_32.lib") int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
SOCKET sClient;
SOCKADDR_IN server;
char szMessage[MSGSIZE];
int ret; // Initialize Windows socket library
WSAStartup(0x0202, &wsaData); // Create client socket
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Connect to server
memset(&server, 0, sizeof(SOCKADDR_IN));
server.sin_family = AF_INET;
server.sin_addr.S_un.S_addr = inet_addr(SERVER_ADDRESS);
server.sin_port = htons(PORT); connect(sClient, (struct sockaddr *)&server, sizeof(SOCKADDR_IN));
printf("client is contect\r\n");
while (TRUE)
{
printf("Send:");
gets(szMessage); // Send message
send(sClient, szMessage, strlen(szMessage), 0); // Receive message
ret = recv(sClient, szMessage, MSGSIZE, 0);
szMessage[ret] = '\0'; printf("Received [%d bytes]: '%s'\n", ret, szMessage);
} // Clean up
closesocket(sClient);
WSACleanup();
return 0;
}
Echo服务端代码:
#include <WINSOCK2.H>
#include <stdio.h> #define PORT 5150 //端口
#define MSGSIZE 1024 //信息缓冲大小
#pragma comment(lib, "ws2_32.lib") typedef enum
{
RECV_POSTED
}OPERATION_TYPE; typedef struct
{
WSAOVERLAPPED overlap;
WSABUF Buffer;
char szMessage[MSGSIZE];
DWORD NumberOfBytesRecvd;
DWORD Flags;
OPERATION_TYPE OperationType;
}COverLapped, *p_COverLapped; DWORD WINAPI WorkerThread(LPVOID); int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
SOCKET m_wListen = INVALID_SOCKET; //监听句柄
SOCKET m_wSocket = INVALID_SOCKET; //
SOCKADDR_IN local, client;
DWORD i, dwThreadId;
int iaddrSize = sizeof(SOCKADDR_IN);
HANDLE CompletionPort = INVALID_HANDLE_VALUE;
SYSTEM_INFO systeminfo;
p_COverLapped pOverLapped = NULL; // Initialize Windows Socket library
WSAStartup(0x0202, &wsaData); // Create completion port
CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); // Create worker thread
GetSystemInfo(&systeminfo);
for (i = 0; i < systeminfo.dwNumberOfProcessors; i++)
{
CreateThread(NULL, 0, WorkerThread, CompletionPort, 0, &dwThreadId);
} // Create listening socket
m_wListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(m_wListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN)); // Listen
listen(m_wListen, 3); while (TRUE)
{
// Accept a connection
m_wSocket = accept(m_wListen, (struct sockaddr *)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port)); //初始化对象
//CSocketItem * pSocketItem = new CSocketItem();
//pSocketItem->Attach(m_wSocket, client.sin_addr.S_un.S_addr); // Associate the newly arrived client socket with completion port
CreateIoCompletionPort((HANDLE)m_wSocket, CompletionPort, (DWORD)m_wSocket, 0); // Launch an asynchronous operation for new arrived connection
pOverLapped = (p_COverLapped)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(COverLapped));
pOverLapped->Buffer.len = MSGSIZE;
pOverLapped->Buffer.buf = pOverLapped->szMessage;
pOverLapped->OperationType = RECV_POSTED;
WSARecv(m_wSocket, &pOverLapped->Buffer, 1, &pOverLapped->NumberOfBytesRecvd, &pOverLapped->Flags, &pOverLapped->overlap, NULL);
} PostQueuedCompletionStatus(CompletionPort, 0xFFFFFFFF, 0, NULL);
CloseHandle(CompletionPort);
closesocket(m_wListen);
WSACleanup(); return 0;
} DWORD WINAPI WorkerThread(LPVOID CompletionPortID)
{
HANDLE CompletionPort = (HANDLE)CompletionPortID;
DWORD dwBytesTransferred;
SOCKET sClient;
p_COverLapped pOverLapped = NULL; while (TRUE)
{
GetQueuedCompletionStatus(CompletionPort, &dwBytesTransferred, (PULONG_PTR)&sClient, (LPOVERLAPPED *)&pOverLapped, INFINITE);
if (dwBytesTransferred == 0xFFFFFFFF)
{
return 0;
} if (pOverLapped->OperationType == RECV_POSTED)
{
if (dwBytesTransferred == 0)
{
// Connection was closed by client
closesocket(sClient);
HeapFree(GetProcessHeap(), 0, pOverLapped);
}
else
{
pOverLapped->szMessage[dwBytesTransferred] = '\0';
send(sClient, pOverLapped->szMessage, dwBytesTransferred, 0); // Launch another asynchronous operation for sClient
memset(pOverLapped, 0, sizeof(COverLapped));
pOverLapped->Buffer.len = MSGSIZE;
pOverLapped->Buffer.buf = pOverLapped->szMessage;
pOverLapped->OperationType = RECV_POSTED;
WSARecv(sClient, &pOverLapped->Buffer, 1, &pOverLapped->NumberOfBytesRecvd, &pOverLapped->Flags, &pOverLapped->overlap, NULL);
}
}
}
return 0;
}
C++ CompletionPort(完成端口)示例的更多相关文章
- CentOS 7.x 防火墙开放端口相关用法记录
前言 防火墙对服务器起到一定的保护作用,所以了解一些相关的操作是很有必要的. 在CentOS 7.x中,有了一种新的防火墙策略,FireWall , 还记得在6.x中用的还是iptables. 这几天 ...
- 【Docker笔记】-开启TCP管理端口
如果我们通过docker来整合spring cloud项目,可以通过maven-docker插件将构建好的镜像直接推送到docker服务器上,但是生产环境建议关闭该功能,为了安全考虑.开启tcp远程监 ...
- 测试TCP 和 UDP 端口的方法
测试 TCP 端口: telnel IP PORT nc -vz IP PORT 测试 UDP 端口: nc -vuz IP PORT 其中 -u 表示使用 udp 协议来进行测试. -u, --ud ...
- IOCP IO完成端口
一. IO完成端口概念 IO完成端口的出现是为了解决并发模型中可运行线程上下文切换开销过大而出现的. 在<Windows核心编程>的描述中,IO完成端口是Wnidows系统提供的最复杂的内 ...
- Linux - Port 端口检测方式
ss 和 netstat 区别 netstat是遍历/proc下面每个PID目录: ss直接读/proc/net下面的统计信息. 所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多 ne ...
- 修改已经创建的docker容器的端口映射
原始链接:https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-dock ...
- 查域名对应ip,测试端口是否可访问通
根据命令查询软件包名称 yum provides */nslookup 根据域名解析ip nslookup 域名 示例:nslookup smtp.163.com 测试端口 telnet ip 端口 ...
- Windows之IOCP
IOCP全称I/O Completion Port,中文译为I/O完成端口.IOCP是一个异步I/O的Windows API,它可以高效地将I/O事件通知给应用程序,类似于Linux中的Epoll,关 ...
- BizTalk开发系列(十一) 在Orchestration中执行Pipeline
由于开发需要有时要在流程中执行Pipeline.比如从DB的某个字段中取消息的字符串并在流程中构造消息.该需要通过pipeline进行升级 属性字段,验证消息等处理.BizTalk架构已经开放了此接口 ...
随机推荐
- Memcached分布式缓存初体验
1 Memcached简介/下载/安装 Memcached是一个高性能的不是内存对象缓存系统,用于动态Web应用以减轻数据库负载.Memcached基于一个存储键/值对的HashMap.其客户端可以使 ...
- lucene join解决父子关系索引
http://www.cnblogs.com/LBSer/p/4417074.html 1 背景 以商家(Poi)维度来展示各种服务(比如团购(deal).直连)正变得越来越流行(图1a), 比如目前 ...
- mac下缺乏make wget怎么办?
1.下载command Line tools for Xcode http://www.mkyong.com/mac/how-to-install-gcc-compiler-on-mac-os-x/ ...
- Spring对象绑定与类型转换
Spring对象绑定与类型转换 Spring的框架体系中,到处充斥着对象绑定从bean的初始化autowired属性,SpringMvc 中对对象的绑定等.Spring对象绑定和类型转换在Spring ...
- gulp学习笔记2
gulp系列学习笔记: 1.gulp学习笔记1 2.gulp学习笔记2 3.gulp学习笔记3 4.gulp学习笔记4 1. 压缩 CSS 压缩 css 代码可降低 css 文件大小,提高页面打开速度 ...
- 通过Ruby On Rails 框架来更好的理解MVC框架
通过Ruby On Rails 框架来更好的理解MVC框架 1.背景 因为我在学习软件工程课程的时候,对于 MVC 框架理解不太深入,只是在理论层面上掌握,但是不知道如何在开发中使用 MVC ...
- css之属性及剩余的选择符
今天的课程加速了,比平时快了些,但觉得很不错.nice~ 属性选择符 E[att] 选择具有att属性的E元素. input[type]{color: #red;} <input t ...
- 用非管理员权限启动主程序,并用管理员权限启动子程序,导致WM_COPYDATA消息发送失败的问题
问题描述 : 用非管理员权限启动dzh,dzh再启动dtssm,由于dtssm的配置文件app.manifest 中设置了requireAdministrator,导致dtssm总是以管理员权 ...
- Android运行时异常“Binary XML file line # : Error inflating class”
http://blog.csdn.net/huangxiaohu_coder/article/details/8497286 在原生Android下编译APK,编译没有问题,但是在运行的时候经常出现如 ...
- oprofile
一.原理 在关注事件发生一定次数时,进行一次采样,记录下需要的信息(比如指令寄存器或栈寄存器信息). 二.参数 项 说明 eventname 要关注的事件名称,常用的事件名称及功能如下: CP ...