C++ <-> C# 命名管道通信
C++ <-> C# Named Pipe Communication
使用命名管道在窗口上实现IPC。
C# 命名管道服务器和C++命名管道客户端。将数据从C++客户端发送到 C# 服务器时丢失第一个字符。
例如,如果从C++客户端发送"这是测试消息",服务器将收到"他的是测试消息"。下面是启示代码。
C++客户端代码
success = WriteFile(
pipe,
guiMsg,
PIPE_BUFFER_SIZE,
&bytesWritten,
NULL);
C# 服务器代码
main()
{
NamedPipeServerStream pipeServer =
new NamedPipeServerStream(@"ZVMonitorPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
StreamString ss = new StreamString(pipeServer);
...
...
string input = ss.ReadString();
Console.WriteLine(input);
...
...
}
public class StreamString
{
private Stream ioStream;
private UnicodeEncoding streamEncoding;
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int len = 0;
len = ioStream.ReadByte() * 64;
len += ioStream.ReadByte();
byte[] inBuffer = new byte[len];
ioStream.Read(inBuffer, 0, len);
return streamEncoding.GetString(inBuffer);
}
...
...
...
}
我错过了什么?
IO 看起来功能正常。您忘记了服务器中的WaitForConnection
,并且忽略了所有返回代码,例如,每个ReadByte()
都可以返回 -1 而不是数据。
很可能,问题在于准备要发送的缓冲区的代码,您尚未将其包含在问题中。
len = ioStream.ReadByte() * 64;
len += ioStream.ReadByte();
这是发送/接收缓冲区长度的非常非正统的方式。
如果您希望在每条消息之前有 16 位长度值,请在数组中读取两个字节,调用 BitConverter.ToUInt16
将它们变成一个数字,并且不要忘记在C++端应用相同的约定。
相关文章:
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- IPC使用多个管道和分支进程来运行Python程序
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- 混淆了如何使用IDL与Ethovision进行通信
- 使用命名管道在 C# 和C++应用程序之间进行持续通信
- 通过管道与 PuTTY 通信
- C++ <-> C# 命名管道通信
- MySQL Windows c api 命名管道错误:"在'等待初始通信数据包'时丢失与 MySQL 服务器的连接,系统错误:0
- 我可以使用管道与现有应用程序通信吗?
- Unix如何双向管道通信
- 使用stdout/stderr以外的管道与子流程进行通信
- 使用匿名管道是否为线程间通信引入了内存屏障
- C# 命名管道双向通信
- 使用命名管道的双向C++到 C# 通信
- 如何使用户级应用程序通过管道与根级应用程序通信
- 程序可以使用管道来回通信吗
- 分支子程序中与启动程序进行管道通信的要求
- 此管道通信代码有什么问题?
- 子进程和父进程之间的交互式双向通信(使用管道)