使用命名管道将图像帧从服务器发送到 C# 客户端C++
Send Image Frames from C++ Server to C# Client using Named Pipe
我是IPC
菜鸟。我正在尝试将图像帧从我的C++ Server
发送到C# Client
。我已经开始了解这一点,并做了一个小Client
,Server
我的C++ Server
发送Hello
。我看到一个相关的问题,有人告诉他们先将Image
转换为Byte Array
,然后以与Hello Message
相同的方式发送,但我无法做到这一点。
我的基本客户端服务器代码
C++代码:
Mat image = imread("IMG_0_10_34_45_2018_1.bmp");
uchar buffer[500][500];
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
buffer[i][j] = image.at<unsigned char>(i, j);
}
}
cout << "Server Creating Pipen";
HANDLE hPipe = ::CreateNamedPipe(_T("\\.\pipe\HyperPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
0,
NULL);
cout << "Server Created Succesfully";
ConnectNamedPipe(hPipe, NULL);
cout << "Sending Message to Client";
DWORD bytesWritten = 0;
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
CloseHandle(hPipe);
return 0;
和 C# 代码:
static void Main(string[] args)
{
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
{
System.Console.WriteLine("Message from Server: " + rdr.ReadToEnd());
}
Console.ReadKey();
}
我还注意到,在我的C++ Server
中,我必须更改PIPE_TYPE to BYTE
,并且还必须更改READMODE to BYTE
.我正在使用OpenCV library
进行Image Processing
,所以我可以轻松地Byte Array
没有问题。
所以,任何人都可以告诉我如何将该Byte Array
从C++
发送到C#
。 或者如果可能的话,任何人都可以为我提供代码
提前致谢
更新:没有错误,但在客户端,即C# Side
来自服务器的消息输出?????
。
要将字节数组从Server
发送到Client
即buffer
只需要对WriteFile
函数进行微小的更改。
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
此方法会将整个Byte Array
发送到Client
并且还改变了buffer
int _count = 0;
UINT8 _imageBuffer[110592];
for (int _imageRow = 0; _imageRow < _image.rows; _imageRow++)
{
for (int _imageCol = 0; _imageCol < _image.cols; _imageCol++)
{
buffer[_count] = image.at<uchar>(_imageRow, _imageCol);
_count++;
}
}
我对缓冲区数组进行了硬编码,因为我知道我的相机只会发送110592
字节来创建一帧。
在客户端,只需使用Read
功能。
int _imageRowSize = 288;
int _imageColSize = 384;
int _count = 0;
byte[] buffer = new byte[_imageColSize * _imageRowSize];
Image<Gray, UInt16> image = new Image<Gray, UInt16>(_imageColSize,_imageRowSize);
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
_count = 0;
int read = pipe.Read(buffer, 0, buffer.Length);
for (int _imageRow = 0; _imageRow < 288; _imageRow++)
{
for (int _imageCol = 0; _imageCol < 384; _imageCol++)
{
try
{
image.Data[_imageRow, _imageCol, 0] = (UInt16)(buffer[_count] * 255);
}catch(Exception exception)
{
Console.WriteLine(exception);
}
_count++;
}
}
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
}
CvInvoke.Imshow("Image", image);
}
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 从服务器传输到客户端的消息不会出现
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- OpenSSL TLS服务器-使用客户端证书白名单
- 我正在开发服务器,ip作为参数传递不起作用
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 提升 Asio TCP 服务器 处理多个客户端
- 如何在 Minecraft 服务器上执行命令
- 调试编译的服务器在数据库打开时崩溃
- C++套接字客户端到 Python 服务器未创建连接
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 将相机数据从服务器实时流式传输到客户端
- 为什么创建服务器后C++ httplib 库阻塞了我的主线程?
- 每个服务器请求的内存预算
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 使用嵌入式猫鼬服务器托管应用程序
- NodeJs 服务器充斥着 UDP 广播,不发送响应