msgpack可以在C#中打包用户定义的对象,然后将其发送到C ++应用程序中,然后在其中解压缩吗? [英] Can msgpack pack a user-defined object in C#, send the pack to a C++ app where it is then unpacked?

查看:150
本文介绍了msgpack可以在C#中打包用户定义的对象,然后将其发送到C ++应用程序中,然后在其中解压缩吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#回复服务器,可以打包一个对象并将其发送给请求者C#客户端. 我可以做同样的事情吗,但是使用C#答复服务器与C ++请求者客户端进行通信吗?

I have a C# reply server that can pack an object and send it to a requester C# client. Can I do the same thing, but with a C# reply server communicating with a C++ requester client?

这是我的C#回复服务器的示例:

Here's an example of my C# reply server:

using System;
using System.Text;
using ZMQ;
using MsgPack;

namespace zmqMpRep
{
    public class Weather
    {
        public int zipcode;
        public int temperature;
        public int humidity;
    }
    public class zmqMpRep
    {
        public static void Main(string[] args)
        {

            Socket replier  = new Socket( SocketType.REP );
            replier.Bind( "tcp://127.0.0.1:9293" );
            while( true ) {
                Weather weather = new Weather { zipcode = 60645, temperature = 67, humidity = 50 };
                CompiledPacker packer   = new CompiledPacker( false );
                byte[] packWeather  = packer.Pack<Weather> ( weather );
                string request  = replier.Recv(Encoding.Unicode);
                Console.WriteLine( request.ToString() );
                replier.Send( packWeather );
                Weather test    = packer.Unpack<Weather>( packWeather );
                Console.WriteLine( "The temp of zip {0} is {1}", test.zipcode, test.temperature );
            }
        }
    }
}

这是我的C#请求客户端:

Here's my C# request client:

using System;
using System.Text;
using ZMQ;
using MsgPack;

namespace zmqMpReq
{
    public class Weather
    {
        public int zipcode;
        public int temperature;
        public int humidity;
    }
    public class zmqMpReq
    {
        public static void Main(string[] args)
        {
            CompiledPacker packer   = new CompiledPacker( false );
            Socket requester        = new Socket( SocketType.REQ );
            requester.Connect( "tcp://127.0.0.1:9293" );
            string request          = "Hello";
            requester.Send( request, Encoding.Unicode );
            byte[] reply            = null;
            try {
                reply        = requester.Recv();
            }
            catch( ZMQ.Exception e) {
                Console.WriteLine( "Exception: {0}", e.Message );
            }
            Weather weather     = packer.Unpack<Weather>( reply );
            Console.WriteLine( "The temp of zip {0} is {1}", weather.zipcode, weather.temperature );
            System.Threading.Thread.Sleep( 5000 );
        }
    }
}

推荐答案

使用任何语言编写的大多数程序都可以通过套接字进行通信.因此,C#套接字侦听器可以侦听C ++发送器.它们通过交换字节序列来完成(非常简化)

Most programs written in any language can communicate over sockets. So a C# socket listener can listen to C++ sender. They do by exchanging a sequence of bytes (Very simplified)

您在这里所做的是使用 MsgPack byte数组中序列化C#对象并将其发送过来.另一方面,相同的 MsgPack 用于反序列化C#对象.

What you are doing here is serializing a C# object using MsgPack in a byte array and sending it over. On the other end the same MsgPack is used to deserialize the C# object.

除非您的序列化/反序列化库支持,否则这在所有编程语言中均不起作用,在您的情况下, MsgPack 不支持.

This will not work across programming languages unless your serializing/deserializing library supports it, which in your case MsgPack doesn't.

参加C#课

public class Weather
{
    public int zipcode;
    public int temperature;
    public int humidity;
}

等效的C ++类

  class Weather
    {
    public:
       int zipcode;
       int temperature;
       int humidity;
    }

取决于您的操作系统.C ++中的sizeof(int)将为4个字节(字符). C#中的sizeof(int)也是4个字节.

Depending on your OS.. sizeof(int) in C++ will be 4 bytes(chars). sizeof(int) in C# is 4 bytes as well.

所以从理论上讲,您可以在C ++和C#程序之间的套接字连接上交换12个(4 * 3)字节,这样双方的天气对象几乎相同

So it theory you can exchange 12 ( 4 * 3 ) bytes over a socket connection between a C++ and C# program so that weather object is pretty much the same on both sides

这篇关于msgpack可以在C#中打包用户定义的对象,然后将其发送到C ++应用程序中,然后在其中解压缩吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆