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

其实一服务端对应每一个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. 为 Tomcat 安装 apr

    apr 官方介绍: Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTT ...

  2. 经典数独游戏+数独求解器—纯C语言实现

    "心常乐数独小游戏"(下面简称"本软件")是一款windows平台下的数独游戏软件. 本软件是开源.免费软件. 本软件使用纯C语言编写,MinGW编译,NSIS ...

  3. MATLAB SVM

    clc;clear;close all; traindata = [1,0; 3,10; 2,2; 2,3; -1,-1; -6,-4; -4,-1; -1.5, -3];group = [1 1 1 ...

  4. springboot 注入 restTemplate

    手动实例化,这个我基本不用 RestTemplate restTemplate = new RestTemplate(); 依赖注入,通常情况下我使用 java.net 包下的类构建的 SimpleC ...

  5. iOS:NSBundle的具体介绍

    NSBundle介绍:它是一个单例类,用来加载资源 bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-i ...

  6. 求两个数中的较大值max(a,b)。(不用if,>)

    题目:求两个数的较大值,不能使用if.>. 1.不使用if.>,还要比较大小,貌似就只能使用条件表达式: x=<表达式1>?<表达式2>:<表达式3>; ...

  7. C#操作AD及Exchange Server总结(一)

    这篇博客的目的:根据亲身项目经历,总结对AD及Exchange Server的操作,包括新建AD用户,设置密码,为AD用户创建邮箱等. 本文完全原创,转载请说明出处,希望对大家有用. 文档目录: 测试 ...

  8. NSURLSession下载和断点续传

    NSURLSession是iOS7之后新的网络接口,和经常用到NSURLConnection是类似的.在程序在前台时,NSURLSession与NSURLConnection可以相互的替代.但是当用户 ...

  9. (转)在ios android设备上使用 Protobuf (使用dll方式)

    自:http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http:/ ...

  10. 使用grep进行文本查找

    命令模式: grep "文本" -rl 路径 例子: grep "w3.the.abc.com" -rl /home/hy/fluent3 有时候需要排除掉一些 ...