博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 管道
阅读量:6540 次
发布时间:2019-06-24

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

// PipeServer.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include 
#include
#include
using namespace std;#pragma warning(disable:4996)HANDLE hClose = NULL;unsigned int _stdcall ProcessPipe(void *lp){ HANDLE hPipeServer = (HANDLE)lp; BYTE btRead[MAX_PATH] = {0}; DWORD dwTime = GetTickCount(); while (GetTickCount() - dwTime < 10000){ DWORD dwRead = 0; BOOL bRet = ReadFile(hPipeServer, btRead, MAX_PATH, &dwRead, NULL); if (!bRet){ if (GetLastError() == ERROR_BROKEN_PIPE ){ break; } } else{ printf("%s\n", btRead); ZeroMemory(btRead, MAX_PATH); strcpy((char*)btRead, "RecvInfo"); DWORD dwWrite = 0; bRet = WriteFile(hPipeServer, btRead, strlen((char*)btRead), &dwWrite, NULL); } Sleep(20); } SetEvent(hClose); DWORD dwExitCode = 0; GetExitCodeThread(GetCurrentThread(), &dwExitCode); _endthreadex(dwExitCode); return 0;}int _tmain(int argc, _TCHAR* argv[]){ HANDLE hPipeServer = CreateNamedPipe(L"\\\\.\\pipe\\pipeServer", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, NULL ); if (hPipeServer == INVALID_HANDLE_VALUE){ printf("CreateNamedPipe failed with error %d", GetLastError()); return 0; } BOOL bRet = ConnectNamedPipe(hPipeServer, NULL); if (!bRet){ printf("ConnectNamedPipe failed with error %d", GetLastError()); return 0; } hClose = CreateEvent(NULL, true, false, NULL); HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ProcessPipe, (LPVOID)hPipeServer, 0, NULL); CloseHandle(hThread); WaitForSingleObject(hClose, INFINITE); CloseHandle(hPipeServer); return 0;}

.cpp

// PipeClient.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include 
#include
using namespace std;#pragma warning(disable:4996)int _tmain(int argc, _TCHAR* argv[]){ HANDLE hPipe = CreateFile( L"\\\\.\\pipe\\pipeServer", // pipe name GENERIC_READ | GENERIC_WRITE, 0, // no sharing NULL, // default security attributes OPEN_EXISTING, // opens existing pipe 0, // default attributes NULL); char strInfo[MAX_PATH] = {0}; strcpy(strInfo, "12sdfgsgdfg"); DWORD dwWrite = 0; WriteFile(hPipe, strInfo, strlen(strInfo), &dwWrite, NULL); Sleep(500); ZeroMemory(strInfo, MAX_PATH); DWORD dwRead = 0; ReadFile(hPipe, strInfo, MAX_PATH, &dwRead, NULL); printf("%s\n", strInfo); Sleep(8000); CloseHandle(hPipe); return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

转载于:https://www.cnblogs.com/qq76211822/p/4711992.html

你可能感兴趣的文章
windows磁盘扩容(动态磁盘)
查看>>
在jsp页面中添加富文本编译器(ueditor)+ 图片上传功能
查看>>
fedora12下安装oracle11客户端
查看>>
实现批量添加20个用户,用户名为user1-50,密码为user后面跟5个随机字符
查看>>
LVM磁盘管理
查看>>
Net命令详解
查看>>
CentOS linux 高可用集群之heartbeat
查看>>
用bat更改hosts文件批处理
查看>>
Logwatch日志分析工具
查看>>
docker 基本操作Ⅱ(关于镜像操作)
查看>>
分工與合作
查看>>
轻松设置站点对ASP危险组件的调用权限
查看>>
看懂“拜占庭容错”,也就看懂了区块链的核心技术
查看>>
APMServ 5.2.6 Win7 Apache启动失败,请检查相关配置
查看>>
了解痘痘起因才能彻底告别痘痘烦恼
查看>>
Zabbix安装
查看>>
Java 日志 详解
查看>>
openstack虚拟化技术和镜像制作
查看>>
一个超棒的jQuery通知栏插件 - jBar
查看>>
分享17个漂亮的电子商务网站
查看>>