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架构已经开放了此接口 ...
随机推荐
- artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...
- JQuery高性能优化
使用JQuery时,你可以使用多种选择器,选择同一个元素,各种方法之间的性能是不一样的,有时候差异会特别大. 通常比较常用的选择器有以下几个: ID选择器 $("#id") 标签选 ...
- linux php环境搭建以及magento安装教程
听朋友用magento在搭建电商系统,我好奇.遂自己下载了一个包部署了一套. 主机采用linux center os6.5,安装程序非常顺利,部署一套大概费时一个小时左右. ########### ...
- django with mysql (part-4)
step01: write the ( views.py ) again .. vim views.py step02: configure your (urls.py) step03: check ...
- spider autohome (1)
Code: #!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib import time def getHtml(url) ...
- 匿名管道读取CMD回显信息
之前用了很坑爹的做法去读取了cmd命令的回显信息,现在发现了用匿名管道的实现方法,由于楼主没有学过Windows核心编程,找了一个代码来凑数 存下来以后研究 #include <windows. ...
- hud2544dijkstra+堆优化
该算法对于稠密图更加有效: 对于给出具有非负权重的边和源顶点S的图G,算法可在O(mlogn)时间内找出从s点到其他每一个顶点的距离. 如果图是稠密的,即对于某个ε>0,m>=n^(1+ε ...
- php 3种常见设计模式
1.工厂模式 <?php namespace Facebab; class Factory { static function createDatabase () { return new Da ...
- STM32 USB CAN 学习笔记 - 共享RAM的用法
USB 时钟可以一直使能. 如果CAN时钟没有使能,RAM 能被软件读写.(CANBus 不能发送和接受Message) 如果CAN时钟使能,RAM不能软件被写. CANBus Core 控制此RAM ...
- Win7下VS2010使用“ASP.Net 3.5 Claims-aware Template”创建ClaimsAwareWebSite报"HRESULT: 0x80041FEB"错误的解决办法
问题描述: 使用VS2010的WIF开发模板创建“Claims-aware ASP.NET Site”.“Claims-aware WCF Service”,下载安装后,创建网站时,报错"H ...