windows named pipe 客户端 服务器
可以实现多客户端对一服务端,服务端为客户端提供服务。
其实一服务端对应每一个client pipe都新建立了一个pipe。windows允许建立多个同名pipe
效果:

服务端代码:
#define BUFSIZE 2048 unsigned __stdcall MsgProcessThread ( void * pParam)
{
HANDLE hPipe = (HANDLE)pParam;
while()
{
char szBufRecv[] = {};
DWORD dwReadSize = ;
cout<<"服务端准备读消息.."<<endl;
BOOL bRet = ::ReadFile(hPipe,szBufRecv,,&dwReadSize,NULL);
if(!bRet || dwReadSize == )
{
DWORD dwLastError = ::GetLastError();
if(dwLastError == ERROR_BROKEN_PIPE)
cout<<"断开连接!"<<endl;
else
cout<<"ReadFile Error:"<<dwLastError<<endl;
break;
}
else
{
cout<<"服务器收到"<<dwReadSize<<"字节:"<<szBufRecv<<endl;
string srouce_str,a1,a2,str_sum;
srouce_str = szBufRecv;
[srouce_str,&a1,&a2,&str_sum](){
auto pos_flag = srouce_str.find("+");
if(pos_flag != string::npos)
{
a1 = srouce_str.substr(,pos_flag);
a2 = srouce_str.substr(pos_flag+);
int add_value1 = atoi(a1.c_str());
int add_value2 = atoi(a2.c_str());
int sum = add_value1 + add_value2;
char szTemp[];
_itoa_s(sum,szTemp,,);
str_sum = szTemp;
}
}(); DWORD dwWritten = ;
bRet = WriteFile(hPipe,str_sum.c_str(),str_sum.length() + ,&dwWritten,NULL);
if(!bRet)
{
int nError = ::GetLastError();
cout<<"服务器WriteFile失败,errorid:"<<nError<<endl;
break;
}
else if(dwWritten == )
{
cout<<"服务器WriteFile失败,发送字节为0"<<endl;
break;
}
}
}
CloseHandle(hPipe);
return ;
} int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
const char * lpszPipename = ("\\\\.\\pipe\\namedpipe_td");
for (;;)
{ hPipe = CreateNamedPipeA( lpszPipename, PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,,NULL);
if (hPipe == INVALID_HANDLE_VALUE)
return -;
BOOL fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if(fConnected)
{
CloseHandle((HANDLE)_beginthreadex(NULL,,MsgProcessThread,(void*)hPipe,,NULL));
}
else
CloseHandle(hPipe);
}
system("puase");
return ;
}
客户端代码
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <windows.h>
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hPipe = []()->HANDLE
{
while()
{
HANDLE hPipe = CreateFileA( "\\\\.\\pipe\\namedpipe_td",GENERIC_READ | GENERIC_WRITE, ,NULL,OPEN_EXISTING,, NULL);
if(hPipe != INVALID_HANDLE_VALUE)
{
cout<<"open pipe success!"<<endl;
return hPipe;
}
int nErrorId = GetLastError();
if(nErrorId != ERROR_PIPE_BUSY)
{
cout<<"client createfile error :"<<nErrorId<<endl;
return NULL;
} cout<<"WaitNamedPipeA ..."<<endl;
if(!WaitNamedPipeA("\\\\.\\pipe\\namedpipe_td",))
{
if(GetLastError() == ERROR_SEM_TIMEOUT)
cout<<"WaitNamePipeA timeOut!"<<endl;
else
{
cout<<"WaitNamePipeA Failed:"<<GetLastError()<<endl;
break;
}
}
else
{
cout<<"waitNamedPipe success!"<<endl;
continue;
}
}
return NULL;
}(); if(hPipe == INVALID_HANDLE_VALUE || !hPipe)
{
cout<<"connect server failed!"<<endl;
system("pause");
return ;
} cout<<"连接成功!"<<endl;
DWORD dwMode = PIPE_READMODE_MESSAGE;
if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL))
{
cout<<"SetNamedPipeHandleState failed!"<<endl;
system("pause");
return ;
} while (true)
{
char send_buff[] = {};
int a1 = rand() % ;
int a2 = rand() % ;
sprintf_s(send_buff,"%d+%d",a1,a2);
DWORD dwWritten = ;
if(!WriteFile(hPipe,send_buff,strlen(send_buff)+,&dwWritten,NULL))
{
int nLastError = ::GetLastError();
if(ERROR_NO_DATA == nLastError)
cout<<"pipi already closeed!"<<endl;
else
cout<<"client writefile failed:"<<nLastError<<endl;
system("pause");
return ;
} if(dwWritten == )
{
cout<<"client writefile failed dwWritten = 0"<<endl;
system("pause");
return ;
} char buffer_readed[] = {};
DWORD dwReaded = ;
Sleep();
if(!ReadFile(hPipe,buffer_readed,,&dwReaded,NULL))
{
int nLastError = ::GetLastError();
if(ERROR_NO_DATA == nLastError)
cout<<"pipi already closeed!"<<endl;
else
cout<<"client ReadFile failed:"<<nLastError<<endl;
system("pause");
return ;
}
if(dwReaded == )
{
cout<<"client ReadFile failed:dwReaded == 0"<<endl;
system("pause");
return ;
}
char szBuff[] = {};
int nSum = atoi(buffer_readed);
if(nSum != a1 + a2)
cout<<"!!错误"<<endl;
sprintf_s(szBuff,"%d+%d=%s",a1,a2,buffer_readed);
cout<<szBuff<<endl;
}
return ;
}
windows named pipe 客户端 服务器的更多相关文章
- docker-compose up Windows named pipe error:(code: 2)
执行docker-compose up启动项目时,报如下错误: ERRORERROR: Windows named pipe error: 膸碌脥艂艕艊藳禄碌藵脰赂露篓碌脛脦脛慕牛藝艁 (code: ...
- 简单通讯聊天 群聊功能 Windows下的客户端 Linux下的epoll服务器
1 服务器代码 Linux eclipse C++ //======================================================================= ...
- 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)
搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...
- svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录
svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录 Windows下,svn服务器IP本来是内网一台服务器上 ...
- 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK
LEADTOOLS Video Streaming Module控件为您创建一个自定义的视频会议应用程序和工具提供所有需要的功能.软件开发人员可以使用Video Streaming Module SD ...
- Windows 平台下Git 服务器搭建
由于项目中一直在使用git作为版本管理,自己对git的理解.使用都还不是怎么的熟悉,所以准备深入了解一下git及一些常用命令的使用,于是干脆把服务端架上,通过自己的PC作为服务端同时作为客户端的角色进 ...
- windows下架设SVN服务器并设置开机启动
原文:windows下架设SVN服务器并设置开机启动 1.安装SVN服务器,到http://subversion.apache.org/packages.html上下载windows版的SVN,并安装 ...
- 大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建
大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建 大数据应用之Windows平台Hbase客户端Eclipse环境搭建-Java版 作者:张子良 版权所有,转载请注明出处 引子 ...
- Windows Server 2016-DNS客户端新增功能
域名系统(DNS)是包含TCP / IP的行业标准协议套件之一,DNS客户端和DNS服务器一起为计算机和用户提供计算机名称到IP地址映射名称解析服务. 在Windows Server 2016中,DN ...
随机推荐
- RuntimeError: Working outside of application context.
flask执行错误: 问题:RuntimeError: Working outside of application context. 方法: from flask import Flask, cur ...
- centos6.8安装具有ngx_cache_purge模块的nginx1.10.3
CentOS-6.8 安装 Nginx1.10.3Nginx 环境准备:安装Nginx需要完成以下依赖的安装 1.gcc 安装:yum install gcc-c++ 2.PCRE pcre-deve ...
- Objective-C:NSDectionary字典的常见操作
NSDectionary字典:它是一个存储键值的容器,每一个键key都对应着一个值value,可以通过键key一次性找到目标值value,这是一个比较好的存储器,相比于数组而言,它明显提高了查询效率. ...
- 第一章 Java常用的并发类
注:本系列博客主要参考于<分布式Java应用:基础与实践>,林昊 著 1.常用的并发集合类 ConcurrentHashMap:线程安全的HashMap的实现 CopyOnWriteArr ...
- [leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...
- iOS开发-图片查看(ScrollView+UIPageControl)
上周没事写了一个简单的图片查看,上次的查看只用到了一个UIImageView,不断的替换背景图片,实现图片之间的切换.通过ScrollView可以很简单的是实现图片之间的查看,设置setPagingE ...
- C#_文件读写常用类介绍
首先要熟悉.NET中处理文件和文件夹的操作.File类和Directory类是其中最主要的两个类.了解它们将对后面功能的实现提供很大的便利. 本节先对和文件系统相关的两个.NET类进行简要介 ...
- Cognos中新建SQLserver数据源的步骤
1:配置-数据源连接-新建数据源-指定数据源名称 2:选择数据库类型,暂时不配置jdbc 3:指定服务器,数据库名称,登陆用户名和密码 4:测试 5:测试OK(OLE DB类型的) 6:返回上一步 , ...
- [Node.js]26. Level 5 : Route rendering
Instead of just writing out the quote to the response, instead render the quote.ejs template, passin ...
- 你需要来自system的权限才能对此文件夹进行更
删除Adobe安装文件时,报错没有权限. 两种解决方案: 1. 设置权限 Win7的安全性提高的同时,对不懂的人来说觉得有些麻烦. 2. PE系统删除 进入PE系统删除即可.