可以实现多客户端对一服务端,服务端为客户端提供服务。

其实一服务端对应每一个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 客户端 服务器的更多相关文章

  1. docker-compose up Windows named pipe error:(code: 2)

    执行docker-compose up启动项目时,报如下错误: ERRORERROR: Windows named pipe error: 膸碌脥艂艕艊藳禄碌藵脰赂露篓碌脛脦脛慕牛藝艁 (code: ...

  2. 简单通讯聊天 群聊功能 Windows下的客户端 Linux下的epoll服务器

    1 服务器代码  Linux eclipse C++ //======================================================================= ...

  3. 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)

    搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...

  4. svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录

    svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录 Windows下,svn服务器IP本来是内网一台服务器上 ...

  5. 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK

    LEADTOOLS Video Streaming Module控件为您创建一个自定义的视频会议应用程序和工具提供所有需要的功能.软件开发人员可以使用Video Streaming Module SD ...

  6. Windows 平台下Git 服务器搭建

    由于项目中一直在使用git作为版本管理,自己对git的理解.使用都还不是怎么的熟悉,所以准备深入了解一下git及一些常用命令的使用,于是干脆把服务端架上,通过自己的PC作为服务端同时作为客户端的角色进 ...

  7. windows下架设SVN服务器并设置开机启动

    原文:windows下架设SVN服务器并设置开机启动 1.安装SVN服务器,到http://subversion.apache.org/packages.html上下载windows版的SVN,并安装 ...

  8. 大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建

    大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建 大数据应用之Windows平台Hbase客户端Eclipse环境搭建-Java版 作者:张子良 版权所有,转载请注明出处 引子 ...

  9. Windows Server 2016-DNS客户端新增功能

    域名系统(DNS)是包含TCP / IP的行业标准协议套件之一,DNS客户端和DNS服务器一起为计算机和用户提供计算机名称到IP地址映射名称解析服务. 在Windows Server 2016中,DN ...

随机推荐

  1. 【TYVJ 五月图论专项有奖比赛】

    最短路+TSP+最小生成树+倍增LCA+TreeDP 第一题 其实是个TSP问题(然而我没发现),但是关键点很少,只有5个,所以用dij+heap分别预处理出来这五个点为源的最短路…… 然后枚举起点 ...

  2. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】

    公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...

  3. 【Type】类型 ParameterizedType

    Type 接口[重要] Type接口完整的定义: public interface java.lang.reflect.Type { /** * Returns a string describing ...

  4. IOS之导航控制器传值

    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显 ...

  5. (转)[unity3d]easytouch的使用

    对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...

  6. Postfix接收邮件后转向运行特定的脚本

    本文主要參考:http://serverfault.com/questions/258469/how-to-configure-postfix-to-pipe-all-incoming-email-t ...

  7. Jacoco覆盖率工具使用调研

    JaCoCo Java Code Coverage Library Jacoco是一个开源的覆盖率工具.Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以 ...

  8. 破解无线网络密码-BT3如何使用3

    BT3 虚拟机 SNOOPWEP2 破解无线网络WEP密钥图解 1.下载BT3 光盘映像文件(ISO格式),比如:bt3-final.iso: 用WinISO 或 UltraISO(这个还支持DVD ...

  9. Websocket——Websocket原理

    偶然在知乎上看到一篇回帖,瞬间认为之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比較喜欢看这样的博客,读起来非常轻松.不枯燥,没有布道师的 ...

  10. Junit和Spring

    @ContextConfiguration 用来指定加载的Spring配置文件的位置,会加载默认配置文件 例如下例会加载:classpath:/com/example/MyTest-context.x ...