Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自c+的Protobufer+;到插座上方的c# 我尝试将一个协议缓冲区从C++发送到C语言。我有个错误。让我给你解释一下我遵循的程序_C#_C++_Sockets_Protocol Buffers - Fatal编程技术网

来自c+的Protobufer+;到插座上方的c# 我尝试将一个协议缓冲区从C++发送到C语言。我有个错误。让我给你解释一下我遵循的程序

来自c+的Protobufer+;到插座上方的c# 我尝试将一个协议缓冲区从C++发送到C语言。我有个错误。让我给你解释一下我遵循的程序,c#,c++,sockets,protocol-buffers,C#,C++,Sockets,Protocol Buffers,C++端 1-我用2.6.1 protoc编译了我的.proto文件 message SensorsReport { required double speed = 1; required double rpm = 2;} 2-我得到了我的头文件,它是一个.h文件和.cc文件。我加入了我的项目,我开始填充我的结构 SensorsReport report; // My message with 2 double repo

C++端

1-我用2.6.1 protoc编译了我的.proto文件

message SensorsReport {
required double speed = 1;
required double rpm = 2;}
2-我得到了我的头文件,它是一个.h文件和.cc文件。我加入了我的项目,我开始填充我的结构

SensorsReport report;                                      // My message with 2 double
report.set_rpm(info.mEngineRPM);                           // I fill the first one
report.set_speed(speedcalculated);                         // I fill the second one
string buf;                                                // Create a string to put the data in
report.SerializeToString(&buf);                            // Serialize data
send(tcp_socket, buf.data(), (int)strlen(buf.data()), 0);  // Send sentece
C#Side

1-我将protobuf net dll添加到我的项目中,并用消息创建了我的类

using System;
using ProtoBuf;


[ProtoContract]
public class SensorsReport
{
    [ProtoMember(1)]
    double speed { get; set; }
    [ProtoMember(2)]
    double rpm { get; set; }
}
2-我创建一个NetworkStream来获取流并反序列化

NetworkStream stream = tcpClient.GetStream();
SensorsReport newdata = Serializer.Deserialize<SensorsReport>(stream);
NetworkStream=tcpClient.GetStream();
SensorsReport newdata=序列化程序。反序列化(流);
这是我所有的过程,我得到了下一个错误:无效的导线类型
我读过一些与创建信息的方式有关的东西,但我认为我一切都是对的。不是吗?

strlen不应用于确定缓冲区的长度

send(tcp_socket, buf.data(), (int)strlen(buf.data()), 0);

在二进制数据的情况下
buf.length()

send(tcp_socket, buf.data(), (int)strlen(buf.data()), 0);

在二进制数据的情况下
buf.length()
可能会更好。

我发现了问题。无法固定接收缓冲区的长度。您必须将缓冲区设置为与发送消息相同。例如,如果消息长度为32字节,则缓冲区将为:

byte[] data = new byte[32];
recv = newsock.ReceiveFrom(data, ref Remote);

现在的问题是,我不知道我的信息长度……无论如何,我找到了原因。

我找到了问题所在。无法固定接收缓冲区的长度。您必须将缓冲区设置为与发送消息相同。例如,如果消息长度为32字节,则缓冲区将为:

byte[] data = new byte[32];
recv = newsock.ReceiveFrom(data, ref Remote);

现在的问题是,我不知道我的信息长度是多少……无论如何,我找到了原因。

不,如果有什么不起作用,一切都不正常!!不,如果有些东西不起作用,一切都不对劲!!
IsRequired=true
[ProtoMember(1,IsRequired=true)]
[ProtoMember(2,IsRequired=true)]
中有什么区别吗?我试着用文件来做,效果很好。是和插座有关的东西。。。如果我发现了问题,我会发布解决方案。在
[ProtoMember(1,IsRequired=true)]
[ProtoMember(2,IsRequired=true)]
IsRequired=true
有什么不同吗?我试着用文件做,效果很好。是和插座有关的东西。。。如果我发现问题,我会发布解决方案。