// PipeServer.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
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 <windows.h>
#include <process.h>
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;
}

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

最新文章

  1. bzoj3631树链剖分
  2. 解密H264、AAC硬件解码的关键扩展数据处理
  3. linux内核分析作业3:跟踪分析Linux内核的启动过程
  4. selenium遇到readonly元素的输入
  5. 修改Chrome临时文件位置
  6. PyCharm 5 破解注册方法
  7. SQLServer创建维护计划失败 错误c001f011
  8. spring 启动流程
  9. Android应用与系统安全防御
  10. [mysql]支持emoji(字符集问题)!
  11. 服务器环境搭建系列(一)-Apache篇
  12. eclipse(myEclipse) 配置maven项目
  13. objective-c 加号 减号 - +
  14. solr主从复制
  15. GameUnity 2.0 文档(一) 事件机制
  16. [ext4]05 磁盘布局 - 延迟块组初始化
  17. echarts实现中国地图数据展示
  18. jmeter循环控制器加jdbc req结果配合组合参数遍历
  19. android ui更新
  20. easyUI-layout布局

热门文章

  1. JavaScript中的变量在内存中的具体存储形式
  2. luogu P2765 魔术球问题
  3. CSS-lineheight
  4. Django创建完全独立的APP
  5. mysql批量插入更新操作
  6. 006.CI4框架CodeIgniter, 加载框架的helper辅助类,调用helper类中的各种函数
  7. 017、MySQL取第4本季度开始和结束日期
  8. Spring MVC RedirectAttributes取值方法
  9. jquery动态选中radio,获取radio选中值
  10. [题解] UVA11426 GCD - Extreme (II)