带有c#服务器的命名管道客户端(c++)
Named pipe client (c++) with c# server
我想做的是有一个用c++编写的命名管道客户端,以便能够与用c#编写的命名管道服务器通信。到目前为止,我还没能完成这件事。
CreateFile给了我一个无效的句柄值,GetLastError返回2。
下面是c++部分(客户端)
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#define PIPE_NAME L"\\.\pipe\TestPipe"
#define BUFF_SIZE 512
int main()
{
HANDLE hPipe;
hPipe = CreateFile(PIPE_NAME, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << "INVALID_HANDLE_VALUE" << GetLastError() << endl;
cin.get();
return -1;
}
cout << hPipe << endl;
DWORD mode = PIPE_READMODE_MESSAGE;
SetNamedPipeHandleState(hPipe, &mode, nullptr, nullptr);
bool success = false;
DWORD read;
while(true)
{
TCHAR chBuff[BUFF_SIZE];
do
{
success = ReadFile(hPipe, chBuff, BUFF_SIZE*sizeof(TCHAR), &read, nullptr);
} while (!success);
_tprintf(TEXT(""%s"n"), chBuff);
}
}
这里是服务器
using System;
using System.IO.Pipes;
using System.Text;
namespace BasicServer
{
public static class Program
{
private static NamedPipeServerStream _server;
static void Main(string[] args)
{
_server = new NamedPipeServerStream(@"\.pipeTestPipe", PipeDirection.Out, 1, PipeTransmissionMode.Message);
_server.WaitForConnection();
Console.WriteLine(_server.IsConnected);
Console.WriteLine("Client connectedn Sending message");
byte[] buff = Encoding.UTF8.GetBytes("Test message");
_server.Write(buff, 0, buff.Length);
while (true)
{
Console.ReadKey();
Console.Write("b b");
}
}
}
}
我已经能够与用c#编写的客户端连接,但据我所知,c++ -> c#通信应该是可能的。
这是我用c#写的测试客户端,它可以工作
using System;
using System.IO.Pipes;
using System.Text;
namespace BasicClientC
{
public class Program
{
private static NamedPipeClientStream client;
static void Main(string[] args)
{
client = new NamedPipeClientStream(@".", @"\.pipeTestPipe", PipeDirection.In);
client.Connect();
byte[] buffer = new byte[512];
client.Read(buffer, 0, 512);
Console.WriteLine(Encoding.UTF8.GetString(buffer));
Console.ReadKey();
}
}
}
那么我的错误在哪里?
我知道为什么。
-
在c#中创建管道时,只使用
"TestPipe"
作为管道的名称,不要将\.pipe
作为该管道名称的前缀。 -
在c++中,使用管道的全路径:
"\.pipeTestPipe"
。您的c++逻辑不需要为此更改,因为您已经定义了它:L"\\.\pipe\TestPipe"
这也可能有帮助。有趣的是,我在三年前或更早的时候就遇到了这个,现在它又回来了。
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- 使用调试/崩溃报告将应用程序部署到客户端
- 如何在本地机器上运行c++和javascript客户端代码(hackerbank风格)
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 从服务器传输到客户端的消息不会出现
- OpenSSL TLS服务器-使用客户端证书白名单
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 提升 Asio TCP 服务器 处理多个客户端
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 如何绑定 C++ gRPC 客户端的网络接口
- C++套接字客户端到 Python 服务器未创建连接
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 将相机数据从服务器实时流式传输到客户端
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- GRPC C++ TLS 客户端 grpc::SslCredentials() 方法不返回
- 计算出有多少客户端可以连接到我正在使用的一些tcp服务器代码
- 如何暂停插孔音频客户端
- 不将数据 socket.io c++(客户端)发送到 nodejs(服务器)socket.io