我有一个 C# 回复服务器,它可以打包一个对象并将其发送到请求者 C# 客户端。我可以做同样的事情,但使用 C# 回复服务器与 C++ 请求者客户端通信吗?
这是我的 C# 回复服务器的示例:
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# 请求客户端:
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 );
}
}
}