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

其实一服务端对应每一个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. 如何搭建 LNMP环境

    和LAMP不同的是LNMP中的N指的是是Nginx(类似于Apache的一种web服务软件)其他都一样.目前这种环境应用的也是非常之多. Nginx设计的初衷是提供一种快速高效多并发的web服务软件. ...

  2. 页面元素定位 XPath 简介

    页面元素定位 XPath 简介 本文所说的 Xpath 是用于 Selenium 自动化测试所使用到的,是针对XHTML网页而言的一种页面元素的定位表示法. XPath 背景 XPath即为XML路径 ...

  3. 使用Topshelf 5步创建Windows 服务

    使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...

  4. (转)看懂UML类图和时序图

    从一个示例开始 请看以下这个类图,类之间的关系是我们需要关注的: 车的类图结构为<<abstract>>,表示车是一个抽象类: 它有两个继承类:小汽车和自行车:它们之间的关系为 ...

  5. Singleton 单例模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 在asp.net中使用jQuery实现类似QQ网站的图片切割效果

    今天要给大家介绍一个asp.net结合jQuery来切割图片的小程序,原理很简单,大致流程是: 加载原图 --> 用矩形框在原图上选取区域并将选取的顶点坐标和矩形尺寸发送至服务器 --> ...

  7. Element table使用技巧详解

    1.控制table某些行数不显示 下载附件的需求,有些行有附件,有些没有,所以需要过滤,重点是:Array.filter()使用 <el-card :body-style="{ pad ...

  8. windows10下部署IIS10+Django web服务

    下面不是一系列过程. 1.下载软件 Python: 2.78 Django: 1.10.3 wfastcgi-2.2.1 pywin32-219.win32-py2.7.exe 下载并安装以上软件 2 ...

  9. ASM下裸设备的路径更改是否会影响数据库的执行

    通过asm来存储数据库文件,在linux下能够通过asmlib的方式来管理块设备,也能够直接使用裸设备来建立asm磁盘.在asmlib方式下,磁盘设备启动顺序和名称的改变不会影响到asm的使用.但假设 ...

  10. 给 TextBlock 加 ToolTip

    <TextBlock ToolTip="{Binding RelativeSource={RelativeSource Self},Path=Text}" Text=&quo ...